Skip to content

Commit 0fb1439

Browse files
pvgndfabpot
authored andcommitted
Use composition instead of inheritance in HttpCodeActivationStrategy
1 parent 8d45013 commit 0fb1439

File tree

8 files changed

+126
-21
lines changed

8 files changed

+126
-21
lines changed

UPGRADE-5.2.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ Mime
4848

4949
* Deprecated `Address::fromString()`, use `Address::create()` instead
5050

51+
Monolog
52+
-------
53+
54+
* The `$actionLevel` constructor argument of `Symfony\Bridge\Monolog\Handler\FingersCrossed\NotFoundActivationStrategy` has been deprecated and replaced by the `$inner` one which expects an ActivationStrategyInterface to decorate instead. `Symfony\Bridge\Monolog\Handler\FingersCrossed\NotFoundActivationStrategy` will become final in 6.0.
55+
* The `$actionLevel` constructor argument of `Symfony\Bridge\Monolog\Handler\FingersCrossed\HttpCodeActivationStrategy` has been deprecated and replaced by the `$inner` one which expects an ActivationStrategyInterface to decorate instead. `Symfony\Bridge\Monolog\Handler\FingersCrossed\HttpCodeActivationStrategy` will become final in 6.0
56+
5157
PropertyAccess
5258
--------------
5359

UPGRADE-6.0.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ Mime
9898

9999
* Removed `Address::fromString()`, use `Address::create()` instead
100100

101+
Monolog
102+
-------
103+
104+
* The `$actionLevel` constructor argument of `Symfony\Bridge\Monolog\Handler\FingersCrossed\NotFoundActivationStrategy` has been replaced by the `$inner` one which expects an ActivationStrategyInterface to decorate instead. `Symfony\Bridge\Monolog\Handler\FingersCrossed\NotFoundActivationStrategy` is now final.
105+
* The `$actionLevel` constructor argument of `Symfony\Bridge\Monolog\Handler\FingersCrossed\HttpCodeActivationStrategy` has been replaced by the `$inner` one which expects an ActivationStrategyInterface to decorate instead. `Symfony\Bridge\Monolog\Handler\FingersCrossed\HttpCodeActivationStrategy` is now final.
106+
101107
OptionsResolver
102108
---------------
103109

src/Symfony/Bridge/Monolog/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
CHANGELOG
22
=========
33

4+
5.2.0
5+
-----
6+
7+
* The `$actionLevel` constructor argument of `Symfony\Bridge\Monolog\Handler\FingersCrossed\NotFoundActivationStrategy` has been deprecated and replaced by the `$inner` one which expects an ActivationStrategyInterface to decorate instead. `Symfony\Bridge\Monolog\Handler\FingersCrossed\NotFoundActivationStrategy` will become final in 6.0.
8+
* The `$actionLevel` constructor argument of `Symfony\Bridge\Monolog\Handler\FingersCrossed\HttpCodeActivationStrategy` has been deprecated and replaced by the `$inner` one which expects an ActivationStrategyInterface to decorate instead. `Symfony\Bridge\Monolog\Handler\FingersCrossed\HttpCodeActivationStrategy` will become final in 6.0
9+
410
5.1.0
511
-----
12+
613
* Added `MailerHandler`
714

815
5.0.0

src/Symfony/Bridge/Monolog/Handler/FingersCrossed/HttpCodeActivationStrategy.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bridge\Monolog\Handler\FingersCrossed;
1313

14+
use Monolog\Handler\FingersCrossed\ActivationStrategyInterface;
1415
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
1516
use Symfony\Component\HttpFoundation\RequestStack;
1617
use Symfony\Component\HttpKernel\Exception\HttpException;
@@ -19,17 +20,29 @@
1920
* Activation strategy that ignores certain HTTP codes.
2021
*
2122
* @author Shaun Simmons <shaun@envysphere.com>
23+
* @author Pierrick Vignand <pierrick.vignand@gmail.com>
24+
*
25+
* @final
2226
*/
23-
class HttpCodeActivationStrategy extends ErrorLevelActivationStrategy
27+
class HttpCodeActivationStrategy extends ErrorLevelActivationStrategy implements ActivationStrategyInterface
2428
{
29+
private $inner;
2530
private $exclusions;
2631
private $requestStack;
2732

2833
/**
29-
* @param array $exclusions each exclusion must have a "code" and "urls" keys
34+
* @param array $exclusions each exclusion must have a "code" and "urls" keys
35+
* @param ActivationStrategyInterface|int|string $inner an ActivationStrategyInterface to decorate
3036
*/
31-
public function __construct(RequestStack $requestStack, array $exclusions, $actionLevel)
37+
public function __construct(RequestStack $requestStack, array $exclusions, $inner)
3238
{
39+
if (!$inner instanceof ActivationStrategyInterface) {
40+
trigger_deprecation('symfony/monolog-bridge', '5.2', 'Passing an actionLevel (int|string) as constructor\'s 3rd argument of "%s" is deprecated, "%s" expected.', __CLASS__, ActivationStrategyInterface::class);
41+
42+
$actionLevel = $inner;
43+
$inner = new ErrorLevelActivationStrategy($actionLevel);
44+
}
45+
3346
foreach ($exclusions as $exclusion) {
3447
if (!\array_key_exists('code', $exclusion)) {
3548
throw new \LogicException(sprintf('An exclusion must have a "code" key.'));
@@ -39,15 +52,14 @@ public function __construct(RequestStack $requestStack, array $exclusions, $acti
3952
}
4053
}
4154

42-
parent::__construct($actionLevel);
43-
55+
$this->inner = $inner;
4456
$this->requestStack = $requestStack;
4557
$this->exclusions = $exclusions;
4658
}
4759

4860
public function isHandlerActivated(array $record): bool
4961
{
50-
$isActivated = parent::isHandlerActivated($record);
62+
$isActivated = $this->inner->isHandlerActivated($record);
5163

5264
if (
5365
$isActivated

src/Symfony/Bridge/Monolog/Handler/FingersCrossed/NotFoundActivationStrategy.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bridge\Monolog\Handler\FingersCrossed;
1313

14+
use Monolog\Handler\FingersCrossed\ActivationStrategyInterface;
1415
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
1516
use Symfony\Component\HttpFoundation\RequestStack;
1617
use Symfony\Component\HttpKernel\Exception\HttpException;
@@ -20,23 +21,36 @@
2021
*
2122
* @author Jordi Boggiano <j.boggiano@seld.be>
2223
* @author Fabien Potencier <fabien@symfony.com>
24+
* @author Pierrick Vignand <pierrick.vignand@gmail.com>
25+
*
26+
* @final
2327
*/
24-
class NotFoundActivationStrategy extends ErrorLevelActivationStrategy
28+
class NotFoundActivationStrategy extends ErrorLevelActivationStrategy implements ActivationStrategyInterface
2529
{
30+
private $inner;
2631
private $exclude;
2732
private $requestStack;
2833

29-
public function __construct(RequestStack $requestStack, array $excludedUrls, $actionLevel)
34+
/**
35+
* @param ActivationStrategyInterface|int|string $inner an ActivationStrategyInterface to decorate
36+
*/
37+
public function __construct(RequestStack $requestStack, array $excludedUrls, $inner)
3038
{
31-
parent::__construct($actionLevel);
39+
if (!$inner instanceof ActivationStrategyInterface) {
40+
trigger_deprecation('symfony/monolog-bridge', '5.2', 'Passing an actionLevel (int|string) as constructor\'s 3rd argument of "%s" is deprecated, "%s" expected.', __CLASS__, ActivationStrategyInterface::class);
41+
42+
$actionLevel = $inner;
43+
$inner = new ErrorLevelActivationStrategy($actionLevel);
44+
}
3245

46+
$this->inner = $inner;
3347
$this->requestStack = $requestStack;
3448
$this->exclude = '{('.implode('|', $excludedUrls).')}i';
3549
}
3650

3751
public function isHandlerActivated(array $record): bool
3852
{
39-
$isActivated = parent::isHandlerActivated($record);
53+
$isActivated = $this->inner->isHandlerActivated($record);
4054

4155
if (
4256
$isActivated

src/Symfony/Bridge/Monolog/Tests/Handler/FingersCrossed/HttpCodeActivationStrategyTest.php

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bridge\Monolog\Tests\Handler\FingersCrossed;
1313

14+
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
1415
use Monolog\Logger;
1516
use PHPUnit\Framework\TestCase;
1617
use Symfony\Bridge\Monolog\Handler\FingersCrossed\HttpCodeActivationStrategy;
@@ -20,22 +21,30 @@
2021

2122
class HttpCodeActivationStrategyTest extends TestCase
2223
{
23-
public function testExclusionsWithoutCode()
24+
/**
25+
* @group legacy
26+
*/
27+
public function testExclusionsWithoutCodeLegacy(): void
2428
{
2529
$this->expectException('LogicException');
2630
new HttpCodeActivationStrategy(new RequestStack(), [['urls' => []]], Logger::WARNING);
2731
}
2832

29-
public function testExclusionsWithoutUrls()
33+
/**
34+
* @group legacy
35+
*/
36+
public function testExclusionsWithoutUrlsLegacy(): void
3037
{
3138
$this->expectException('LogicException');
3239
new HttpCodeActivationStrategy(new RequestStack(), [['code' => 404]], Logger::WARNING);
3340
}
3441

3542
/**
3643
* @dataProvider isActivatedProvider
44+
*
45+
* @group legacy
3746
*/
38-
public function testIsActivated($url, $record, $expected)
47+
public function testIsActivatedLegacy($url, $record, $expected): void
3948
{
4049
$requestStack = new RequestStack();
4150
$requestStack->push(Request::create($url));
@@ -51,10 +60,44 @@ public function testIsActivated($url, $record, $expected)
5160
Logger::WARNING
5261
);
5362

54-
$this->assertEquals($expected, $strategy->isHandlerActivated($record));
63+
self::assertEquals($expected, $strategy->isHandlerActivated($record));
64+
}
65+
66+
public function testExclusionsWithoutCode(): void
67+
{
68+
$this->expectException('LogicException');
69+
new HttpCodeActivationStrategy(new RequestStack(), [['urls' => []]], new ErrorLevelActivationStrategy(Logger::WARNING));
70+
}
71+
72+
public function testExclusionsWithoutUrls(): void
73+
{
74+
$this->expectException('LogicException');
75+
new HttpCodeActivationStrategy(new RequestStack(), [['code' => 404]], new ErrorLevelActivationStrategy(Logger::WARNING));
76+
}
77+
78+
/**
79+
* @dataProvider isActivatedProvider
80+
*/
81+
public function testIsActivated($url, $record, $expected)
82+
{
83+
$requestStack = new RequestStack();
84+
$requestStack->push(Request::create($url));
85+
86+
$strategy = new HttpCodeActivationStrategy(
87+
$requestStack,
88+
[
89+
['code' => 403, 'urls' => []],
90+
['code' => 404, 'urls' => []],
91+
['code' => 405, 'urls' => []],
92+
['code' => 400, 'urls' => ['^/400/a', '^/400/b']],
93+
],
94+
new ErrorLevelActivationStrategy(Logger::WARNING)
95+
);
96+
97+
self::assertEquals($expected, $strategy->isHandlerActivated($record));
5598
}
5699

57-
public function isActivatedProvider()
100+
public function isActivatedProvider(): array
58101
{
59102
return [
60103
['/test', ['level' => Logger::ERROR], true],
@@ -70,7 +113,7 @@ public function isActivatedProvider()
70113
];
71114
}
72115

73-
protected function getContextException($code)
116+
private function getContextException(int $code): array
74117
{
75118
return ['exception' => new HttpException($code)];
76119
}

src/Symfony/Bridge/Monolog/Tests/Handler/FingersCrossed/NotFoundActivationStrategyTest.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bridge\Monolog\Tests\Handler\FingersCrossed;
1313

14+
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
1415
use Monolog\Logger;
1516
use PHPUnit\Framework\TestCase;
1617
use Symfony\Bridge\Monolog\Handler\FingersCrossed\NotFoundActivationStrategy;
@@ -22,18 +23,33 @@ class NotFoundActivationStrategyTest extends TestCase
2223
{
2324
/**
2425
* @dataProvider isActivatedProvider
26+
*
27+
* @group legacy
2528
*/
26-
public function testIsActivated($url, $record, $expected)
29+
public function testIsActivatedLegacy(string $url, array $record, bool $expected): void
2730
{
2831
$requestStack = new RequestStack();
2932
$requestStack->push(Request::create($url));
3033

3134
$strategy = new NotFoundActivationStrategy($requestStack, ['^/foo', 'bar'], Logger::WARNING);
3235

33-
$this->assertEquals($expected, $strategy->isHandlerActivated($record));
36+
self::assertEquals($expected, $strategy->isHandlerActivated($record));
3437
}
3538

36-
public function isActivatedProvider()
39+
/**
40+
* @dataProvider isActivatedProvider
41+
*/
42+
public function testIsActivated(string $url, array $record, bool $expected): void
43+
{
44+
$requestStack = new RequestStack();
45+
$requestStack->push(Request::create($url));
46+
47+
$strategy = new NotFoundActivationStrategy($requestStack, ['^/foo', 'bar'], new ErrorLevelActivationStrategy(Logger::WARNING));
48+
49+
self::assertEquals($expected, $strategy->isHandlerActivated($record));
50+
}
51+
52+
public function isActivatedProvider(): array
3753
{
3854
return [
3955
['/test', ['level' => Logger::DEBUG], false],
@@ -48,7 +64,7 @@ public function isActivatedProvider()
4864
];
4965
}
5066

51-
protected function getContextException($code)
67+
protected function getContextException(int $code): array
5268
{
5369
return ['exception' => new HttpException($code)];
5470
}

src/Symfony/Bridge/Monolog/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"php": ">=7.2.5",
2020
"monolog/monolog": "^1.25.1|^2",
2121
"symfony/service-contracts": "^1.1|^2",
22-
"symfony/http-kernel": "^4.4|^5.0"
22+
"symfony/http-kernel": "^4.4|^5.0",
23+
"symfony/deprecation-contracts": "^2.1"
2324
},
2425
"require-dev": {
2526
"symfony/console": "^4.4|^5.0",

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy