Skip to content

Commit 20611cc

Browse files
committed
feature #388 Use ActivationStrategy instead of actionLevel when available (jderusse)
This PR was merged into the 3.x-dev branch. Discussion ---------- Use `ActivationStrategy` instead of `actionLevel` when available Take over #336 Fixes #369 Commits ------- 7eaabac Use `ActivationStrategy` instead of `actionLevel` when available
2 parents b9b322d + 7eaabac commit 20611cc

File tree

4 files changed

+95
-9
lines changed

4 files changed

+95
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## 3.7.0 (xxxx-xx-xx)
22

3+
* Use `ActivationStrategy` instead of `actionLevel` when available
34
* Register resettable processors (`ResettableInterface`) for autoconfiguration (tag: `kernel.reset`)
45
* Drop support for Symfony 3.4
56

DependencyInjection/MonologExtension.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111

1212
namespace Symfony\Bundle\MonologBundle\DependencyInjection;
1313

14+
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
1415
use Monolog\Logger;
1516
use Monolog\Processor\ProcessorInterface;
1617
use Monolog\Handler\HandlerInterface;
1718
use Monolog\ResettableInterface;
1819
use Symfony\Bridge\Monolog\Handler\FingersCrossed\HttpCodeActivationStrategy;
20+
use Symfony\Bridge\Monolog\Processor\SwitchUserTokenProcessor;
1921
use Symfony\Bridge\Monolog\Processor\TokenProcessor;
2022
use Symfony\Bridge\Monolog\Processor\WebProcessor;
2123
use Symfony\Bundle\FullStack;
@@ -399,6 +401,11 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
399401
$nestedHandlerId = $this->getHandlerId($handler['handler']);
400402
$this->markNestedHandler($nestedHandlerId);
401403

404+
$activation = $handler['action_level'];
405+
if (class_exists(SwitchUserTokenProcessor::class)) {
406+
$activation = new Definition(ErrorLevelActivationStrategy::class, [$activation]);
407+
}
408+
402409
if (isset($handler['activation_strategy'])) {
403410
$activation = new Reference($handler['activation_strategy']);
404411
} elseif (!empty($handler['excluded_404s'])) {
@@ -408,7 +415,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
408415
$activationDef = new Definition('Symfony\Bridge\Monolog\Handler\FingersCrossed\NotFoundActivationStrategy', [
409416
new Reference('request_stack'),
410417
$handler['excluded_404s'],
411-
$handler['action_level']
418+
$activation
412419
]);
413420
$container->setDefinition($handlerId.'.not_found_strategy', $activationDef);
414421
$activation = new Reference($handlerId.'.not_found_strategy');
@@ -419,12 +426,10 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
419426
$activationDef = new Definition('Symfony\Bridge\Monolog\Handler\FingersCrossed\HttpCodeActivationStrategy', [
420427
new Reference('request_stack'),
421428
$handler['excluded_http_codes'],
422-
$handler['action_level']
429+
$activation
423430
]);
424431
$container->setDefinition($handlerId.'.http_code_strategy', $activationDef);
425432
$activation = new Reference($handlerId.'.http_code_strategy');
426-
} else {
427-
$activation = $handler['action_level'];
428433
}
429434

430435
$definition->setArguments([

Tests/DependencyInjection/FixtureMonologExtensionTest.php

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,36 @@
1111

1212
namespace Symfony\Bundle\MonologBundle\Tests\DependencyInjection;
1313

14-
use Symfony\Bundle\MonologBundle\DependencyInjection\MonologExtension;
14+
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
15+
use Symfony\Bridge\Monolog\Processor\SwitchUserTokenProcessor;
1516
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass;
17+
use Symfony\Bundle\MonologBundle\DependencyInjection\MonologExtension;
1618
use Symfony\Component\DependencyInjection\ContainerBuilder;
1719
use Symfony\Component\DependencyInjection\Definition;
1820
use Symfony\Component\DependencyInjection\Reference;
1921

2022
abstract class FixtureMonologExtensionTest extends DependencyInjectionTest
2123
{
24+
/** @group legacy */
25+
public function testLegacyLoadWithSeveralHandlers()
26+
{
27+
if (class_exists(SwitchUserTokenProcessor::class)) {
28+
$this->markTestSkipped('Symfony MonologBridge < 5.2 is needed.');
29+
}
30+
31+
$this->doTestLoadWithSeveralHandlers(\Monolog\Logger::ERROR);
32+
}
33+
2234
public function testLoadWithSeveralHandlers()
35+
{
36+
if (!class_exists(SwitchUserTokenProcessor::class)) {
37+
$this->markTestSkipped('Symfony MonologBridge >= 5.2 is needed.');
38+
}
39+
40+
$this->doTestLoadWithSeveralHandlers(new Definition(ErrorLevelActivationStrategy::class, [\Monolog\Logger::ERROR]));
41+
}
42+
43+
private function doTestLoadWithSeveralHandlers($activation)
2344
{
2445
$container = $this->getContainer('multiple_handlers');
2546

@@ -41,14 +62,33 @@ public function testLoadWithSeveralHandlers()
4162

4263
$handler = $container->getDefinition('monolog.handler.main');
4364
$this->assertDICDefinitionClass($handler, 'Monolog\Handler\FingersCrossedHandler');
44-
$this->assertDICConstructorArguments($handler, [new Reference('monolog.handler.nested'), \Monolog\Logger::ERROR, 0, true, true, \Monolog\Logger::NOTICE]);
65+
$this->assertDICConstructorArguments($handler, [new Reference('monolog.handler.nested'), $activation, 0, true, true, \Monolog\Logger::NOTICE]);
4566

4667
$handler = $container->getDefinition('monolog.handler.filtered');
4768
$this->assertDICDefinitionClass($handler, 'Monolog\Handler\FilterHandler');
4869
$this->assertDICConstructorArguments($handler, [new Reference('monolog.handler.nested2'), [\Monolog\Logger::WARNING, \Monolog\Logger::ERROR], \Monolog\Logger::EMERGENCY, true]);
4970
}
5071

72+
/** @group legacy */
73+
public function testLegacyLoadWithOverwriting()
74+
{
75+
if (class_exists(SwitchUserTokenProcessor::class)) {
76+
$this->markTestSkipped('Symfony MonologBridge < 5.2 is needed.');
77+
}
78+
79+
$this->doTestLoadWithOverwriting(\Monolog\Logger::ERROR);
80+
}
81+
5182
public function testLoadWithOverwriting()
83+
{
84+
if (!class_exists(SwitchUserTokenProcessor::class)) {
85+
$this->markTestSkipped('Symfony MonologBridge >= 5.2 is needed.');
86+
}
87+
88+
$this->doTestLoadWithOverwriting(new Definition(ErrorLevelActivationStrategy::class, [\Monolog\Logger::ERROR]));
89+
}
90+
91+
private function doTestLoadWithOverwriting($activation)
5292
{
5393
$container = $this->getContainer('overwriting');
5494

@@ -69,7 +109,7 @@ public function testLoadWithOverwriting()
69109

70110
$handler = $container->getDefinition('monolog.handler.main');
71111
$this->assertDICDefinitionClass($handler, 'Monolog\Handler\FingersCrossedHandler');
72-
$this->assertDICConstructorArguments($handler, [new Reference('monolog.handler.nested'), \Monolog\Logger::ERROR, 0, true, true, null]);
112+
$this->assertDICConstructorArguments($handler, [new Reference('monolog.handler.nested'), $activation, 0, true, true, null]);
73113
}
74114

75115
public function testLoadWithNewAtEnd()

Tests/DependencyInjection/MonologExtensionTest.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
namespace Symfony\Bundle\MonologBundle\Tests\DependencyInjection;
1313

1414
use InvalidArgumentException;
15+
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
1516
use Monolog\Handler\RollbarHandler;
1617
use Monolog\Logger;
1718
use Monolog\Processor\UidProcessor;
19+
use Symfony\Bridge\Monolog\Processor\SwitchUserTokenProcessor;
1820
use Symfony\Bundle\MonologBundle\DependencyInjection\MonologExtension;
1921
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass;
2022
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -495,8 +497,27 @@ public function testLogglyHandler()
495497
$this->assertDICDefinitionMethodCallAt(1, $handler, 'setTag', ['foo,bar']);
496498
}
497499

500+
/** @group legacy */
501+
public function testLegacyFingersCrossedHandlerWhenExcluded404sAreSpecified()
502+
{
503+
if (class_exists(SwitchUserTokenProcessor::class)) {
504+
$this->markTestSkipped('Symfony MonologBridge < 5.2 is needed.');
505+
}
506+
507+
$this->doTestFingersCrossedHandlerWhenExcluded404sAreSpecified(\Monolog\Logger::WARNING);
508+
}
509+
498510
/** @group legacy */
499511
public function testFingersCrossedHandlerWhenExcluded404sAreSpecified()
512+
{
513+
if (!class_exists(SwitchUserTokenProcessor::class)) {
514+
$this->markTestSkipped('Symfony MonologBridge >= 5.2 is needed.');
515+
}
516+
517+
$this->doTestFingersCrossedHandlerWhenExcluded404sAreSpecified(new Definition(ErrorLevelActivationStrategy::class, [\Monolog\Logger::WARNING]));
518+
}
519+
520+
private function doTestFingersCrossedHandlerWhenExcluded404sAreSpecified($activation)
500521
{
501522
$container = $this->getContainer([['handlers' => [
502523
'main' => ['type' => 'fingers_crossed', 'handler' => 'nested', 'excluded_404s' => ['^/foo', '^/bar']],
@@ -514,14 +535,33 @@ public function testFingersCrossedHandlerWhenExcluded404sAreSpecified()
514535

515536
$strategy = $container->getDefinition('monolog.handler.main.not_found_strategy');
516537
$this->assertDICDefinitionClass($strategy, 'Symfony\Bridge\Monolog\Handler\FingersCrossed\NotFoundActivationStrategy');
517-
$this->assertDICConstructorArguments($strategy, [new Reference('request_stack'), ['^/foo', '^/bar'], \Monolog\Logger::WARNING]);
538+
$this->assertDICConstructorArguments($strategy, [new Reference('request_stack'), ['^/foo', '^/bar'], $activation]);
518539

519540
$handler = $container->getDefinition('monolog.handler.main');
520541
$this->assertDICDefinitionClass($handler, 'Monolog\Handler\FingersCrossedHandler');
521542
$this->assertDICConstructorArguments($handler, [new Reference('monolog.handler.nested'), new Reference('monolog.handler.main.not_found_strategy'), 0, true, true, null]);
522543
}
523544

545+
/** @group legacy */
546+
public function testLegacyFingersCrossedHandlerWhenExcludedHttpCodesAreSpecified()
547+
{
548+
if (class_exists(SwitchUserTokenProcessor::class)) {
549+
$this->markTestSkipped('Symfony MonologBridge < 5.2 is needed.');
550+
}
551+
552+
$this->doTestFingersCrossedHandlerWhenExcludedHttpCodesAreSpecified(\Monolog\Logger::WARNING);
553+
}
554+
524555
public function testFingersCrossedHandlerWhenExcludedHttpCodesAreSpecified()
556+
{
557+
if (!class_exists(SwitchUserTokenProcessor::class)) {
558+
$this->markTestSkipped('Symfony MonologBridge >= 5.2 is needed.');
559+
}
560+
561+
$this->doTestFingersCrossedHandlerWhenExcludedHttpCodesAreSpecified(new Definition(ErrorLevelActivationStrategy::class, [\Monolog\Logger::WARNING]));
562+
}
563+
564+
private function doTestFingersCrossedHandlerWhenExcludedHttpCodesAreSpecified($activation)
525565
{
526566
if (!class_exists('Symfony\Bridge\Monolog\Handler\FingersCrossed\HttpCodeActivationStrategy')) {
527567
$this->markTestSkipped('Symfony Monolog 4.1+ is needed.');
@@ -554,7 +594,7 @@ public function testFingersCrossedHandlerWhenExcludedHttpCodesAreSpecified()
554594
['code' => 404, 'urls' => []],
555595
['code' => 405, 'urls' => ['^/foo', '^/bar']]
556596
],
557-
\Monolog\Logger::WARNING
597+
$activation,
558598
]);
559599

560600
$handler = $container->getDefinition('monolog.handler.main');

0 commit comments

Comments
 (0)