Skip to content

Commit 886bc80

Browse files
committed
Use composition instead of inheritance in NotFoundActivationStrategy
1 parent 38ff0b0 commit 886bc80

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@
1515
use Symfony\Component\HttpFoundation\RequestStack;
1616
use Symfony\Component\HttpKernel\Exception\HttpException;
1717

18+
trigger_deprecation('symfony/monolog-bridge', '5.1', 'The "%s" class is deprecated, use "%s" instead.', NotFoundActivationStrategy::class, NotFoundActivationStrategyDecorator::class);
19+
1820
/**
1921
* Activation strategy that ignores 404s for certain URLs.
2022
*
2123
* @author Jordi Boggiano <j.boggiano@seld.be>
2224
* @author Fabien Potencier <fabien@symfony.com>
25+
*
26+
* @deprecated since Symfony 5.1, Symfony\Bridge\Monolog\Handler\FingersCrossed\NotFoundActivationStrategyDecorator instead.
2327
*/
2428
class NotFoundActivationStrategy extends ErrorLevelActivationStrategy
2529
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Monolog\Handler\FingersCrossed;
13+
14+
use Monolog\Handler\FingersCrossed\ActivationStrategyInterface;
15+
use Symfony\Component\HttpFoundation\RequestStack;
16+
use Symfony\Component\HttpKernel\Exception\HttpException;
17+
18+
/**
19+
* Activation strategy that ignores 404s for certain URLs.
20+
*
21+
* @author Jordi Boggiano <j.boggiano@seld.be>
22+
* @author Fabien Potencier <fabien@symfony.com>
23+
* @author Pierrick Vignand <pierrick.vignand@gmail.com>
24+
*/
25+
final class NotFoundActivationStrategyDecorator implements ActivationStrategyInterface
26+
{
27+
private $inner;
28+
private $blacklist;
29+
private $requestStack;
30+
31+
public function __construct(ActivationStrategyInterface $inner, RequestStack $requestStack, array $excludedUrls)
32+
{
33+
$this->inner = $inner;
34+
35+
$this->requestStack = $requestStack;
36+
$this->blacklist = '{('.implode('|', $excludedUrls).')}i';
37+
}
38+
39+
public function isHandlerActivated(array $record): bool
40+
{
41+
$isActivated = $this->inner->isHandlerActivated($record);
42+
43+
if (
44+
$isActivated
45+
&& isset($record['context']['exception'])
46+
&& $record['context']['exception'] instanceof HttpException
47+
&& 404 == $record['context']['exception']->getStatusCode()
48+
&& ($request = $this->requestStack->getMasterRequest())
49+
) {
50+
return !preg_match($this->blacklist, $request->getPathInfo());
51+
}
52+
53+
return $isActivated;
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Monolog\Tests\Handler\FingersCrossed;
13+
14+
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
15+
use Monolog\Logger;
16+
use PHPUnit\Framework\TestCase;
17+
use Symfony\Bridge\Monolog\Handler\FingersCrossed\NotFoundActivationStrategyDecorator;
18+
use Symfony\Component\HttpFoundation\Request;
19+
use Symfony\Component\HttpFoundation\RequestStack;
20+
use Symfony\Component\HttpKernel\Exception\HttpException;
21+
22+
class NotFoundActivationStrategyDecoratorTest extends TestCase
23+
{
24+
/**
25+
* @dataProvider isActivatedProvider
26+
*/
27+
public function testIsActivated($url, $record, $expected)
28+
{
29+
$requestStack = new RequestStack();
30+
$requestStack->push(Request::create($url));
31+
32+
$strategy = new NotFoundActivationStrategyDecorator(new ErrorLevelActivationStrategy(Logger::WARNING), $requestStack, ['^/foo', 'bar']);
33+
34+
$this->assertEquals($expected, $strategy->isHandlerActivated($record));
35+
}
36+
37+
public function isActivatedProvider()
38+
{
39+
return [
40+
['/test', ['level' => Logger::DEBUG], false],
41+
['/foo', ['level' => Logger::DEBUG, 'context' => $this->getContextException(404)], false],
42+
['/baz/bar', ['level' => Logger::ERROR, 'context' => $this->getContextException(404)], false],
43+
['/foo', ['level' => Logger::ERROR, 'context' => $this->getContextException(404)], false],
44+
['/foo', ['level' => Logger::ERROR, 'context' => $this->getContextException(500)], true],
45+
46+
['/test', ['level' => Logger::ERROR], true],
47+
['/baz', ['level' => Logger::ERROR, 'context' => $this->getContextException(404)], true],
48+
['/baz', ['level' => Logger::ERROR, 'context' => $this->getContextException(500)], true],
49+
];
50+
}
51+
52+
private function getContextException(int $code): array
53+
{
54+
return ['exception' => new HttpException($code)];
55+
}
56+
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class NotFoundActivationStrategyTest extends TestCase
2222
{
2323
/**
2424
* @dataProvider isActivatedProvider
25+
*
26+
* @group legacy
2527
*/
2628
public function testIsActivated($url, $record, $expected)
2729
{

0 commit comments

Comments
 (0)