|
| 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 | +} |
0 commit comments