8000 [SecurityBundle] Make firewalls event dispatcher traceable on debug mode · symfony/symfony@688e0bf · GitHub
[go: up one dir, main page]

Skip to content

Commit 688e0bf

Browse files
committed
[SecurityBundle] Make firewalls event dispatcher traceable on debug mode
1 parent 1f7bc10 commit 688e0bf

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

src/Symfony/Bundle/SecurityBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CHANGELOG
1010
* Modify "icon.svg" to improve accessibility for blind/low vision users
1111
* Make `Security::login()` return the authenticator response
1212
* Deprecate the `security.firewalls.logout.csrf_token_generator` config option, use `security.firewalls.logout.csrf_token_manager` instead
13+
* Make firewalls event dispatcher traceable on debug mode
1314

1415
6.2
1516
---
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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\Bundle\SecurityBundle\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\ContainerInterface;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher;
19+
use Symfony\Component\Stopwatch\Stopwatch;
20+
21+
/**
22+
* @author Mathieu Lechat <mathieu.lechat@les-tilleuls.coop>
23+
*
24+
* @internal
25+
*/
26+
class MakeFirewallsEventDispatcherTraceablePass implements CompilerPassInterface
27+
{
28+
public function process(ContainerBuilder $container)
29+
{
30+
if (!$container->has('event_dispatcher') || !$container->hasParameter('security.firewalls')) {
31+
return;
32+
}
33+
34+
if (!$container->getParameter('kernel.debug') || !class_exists(Stopwatch::class) || !$container->has('debug.stopwatch')) {
35+
return;
36+
}
37+
38+
$dispatchersId = [];
39+
40+
foreach ($container->getParameter('security.firewalls') as $firewallName) {
41+
$dispatcherId = 'security.event_dispatcher.'.$firewallName;
42+
43+
if (!$container->has($dispatcherId)) {
44+
continue;
45+
}
46+
47+
$dispatchersId[$dispatcherId] = 'debug.'.$dispatcherId;
48+
49+
$container->register($dispatchersId[$dispatcherId], TraceableEventDispatcher::class)
50+
->setDecoratedService($dispatcherId)
51+
->setArguments([
52+
new Reference($dispatchersId[$dispatcherId].'.inner'),
53+
new Reference('debug.stopwatch'),
54+
new Reference('logger', ContainerInterface::NULL_ON_INVALID_REFERENCE),
55+
new Reference('request_stack', ContainerInterface::NULL_ON_INVALID_REFERENCE),
56+
]);
57+
}
58+
59+
foreach (['kernel.event_subscriber', 'kernel.event_listener'] as $tagName) {
60+
foreach ($container->findTaggedServiceIds($tagName) as $taggedServiceId => $tags) {
61+
$taggedServiceDefinition = $container->findDefinition($taggedServiceId);
62+
$taggedServiceDefinition->clearTag($tagName);
63+
64+
foreach ($tags as $tag) {
65+
if ($dispatcherId = $tag['dispatcher'] ?? null) {
66+
$tag['dispatcher'] = $dispatchersId[$dispatcherId] ?? $dispatcherId;
67+
}
68+
$taggedServiceDefinition->addTag($tagName, $tag);
69+
}
70+
}
71+
}
72+
}
73+
}

src/Symfony/Bundle/SecurityBundle/SecurityBundle.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\AddSecurityVotersPass;
1616
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\AddSessionDomainConstraintPass;
1717
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\CleanRememberMeVerifierPass;
18+
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\MakeFirewallsEventDispatcherTraceablePass;
1819
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\RegisterCsrfFeaturesPass;
1920
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\RegisterEntryPointPass;
2021
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\RegisterGlobalSecurityEventListenersPass;
@@ -92,5 +93,8 @@ public function build(ContainerBuilder $container)
9293
AuthenticationEvents::ALIASES,
9394
SecurityEvents::ALIASES
9495
)));
96+
97+
// must be registered before DecoratorServicePass
98+
$container->addCompilerPass(new MakeFirewallsEventDispatcherTraceablePass(), PassConfig::TYPE_OPTIMIZE, 1);
9599
}
96100
}

0 commit comments

Comments
 (0)
0