From 45f1a16b9380d56ef84de6810d348cc02759e4ea Mon Sep 17 00:00:00 2001 From: alexandresalome Date: Wed, 9 Jan 2013 21:46:26 +0100 Subject: [PATCH 1/2] make RegisterKernelListenersPass reusable --- .../Compiler/RegisterKernelListenersPass.php | 38 +++++++++++++++++-- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/RegisterKernelListenersPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/RegisterKernelListenersPass.php index ad406eb6a3d90..c7e3380cff72a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/RegisterKernelListenersPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/RegisterKernelListenersPass.php @@ -14,17 +14,47 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +/** + * Compiler pass to register tagged services for an event dispatcher. + */ class RegisterKernelListenersPass implements CompilerPassInterface { + /** + * @var string + */ + protected $dispatcherService; + + /** + * @var string + */ + protected $listenerTag; + + /** + * @var string + */ + protected $subscriberTag; + + /** + * @param string $dispatcherService Service name of the event dispatcher in processed container + * @param string $listenerTag Tag name used for listener + * @param string $listenerTag Tag name used for subscribers + */ + public function __construct($dispatcherService = 'event_dispatcher', $listenerTag = 'kernel.event_listener', $subscriberTag = 'kernel.event_subscriber') + { + $this->dispatcherService = $dispatcherService; + $this->listenerTag = $listenerTag; + $this->subscriberTag = $subscriberTag; + } + public function process(ContainerBuilder $container) { - if (!$container->hasDefinition('event_dispatcher')) { + if (!$container->hasDefinition($this->dispatcherService)) { return; } - $definition = $container->getDefinition('event_dispatcher'); + $definition = $container->getDefinition($this->dispatcherService); - foreach ($container->findTaggedServiceIds('kernel.event_listener') as $id => $events) { + foreach ($container->findTaggedServiceIds($this->listenerTag) as $id => $events) { foreach ($events as $event) { $priority = isset($event['priority']) ? $event['priority'] : 0; @@ -44,7 +74,7 @@ public function process(ContainerBuilder $container) } } - foreach ($container->findTaggedServiceIds('kernel.event_subscriber') as $id => $attributes) { + foreach ($container->findTaggedServiceIds($this->subscriberTag) as $id => $attributes) { // We must assume that the class value has been correctly filled, even if the service is created by a factory $class = $container->getDefinition($id)->getClass(); From 5047227489bf3509a6fade7d9c3f19d0113f2aac Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 25 Apr 2013 15:44:15 +0200 Subject: [PATCH 2/2] moved the kernel listener compiler pass to HttpKernel to make it reusable (refs #6643) --- src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md | 2 ++ .../Bundle/FrameworkBundle/FrameworkBundle.php | 4 ++-- .../DependencyInjection/RegisterListenersPass.php} | 10 ++++++---- .../RegisterListenersPassTest.php} | 12 ++++++------ 4 files changed, 16 insertions(+), 12 deletions(-) rename src/Symfony/{Bundle/FrameworkBundle/DependencyInjection/Compiler/RegisterKernelListenersPass.php => Component/HttpKernel/DependencyInjection/RegisterListenersPass.php} (91%) rename src/Symfony/{Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/RegisterKernelListenersPassTest.php => Component/HttpKernel/Tests/DependencyInjection/RegisterListenersPassTest.php} (84%) diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index bdba9ca4299a7..a1934f286695d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -4,6 +4,8 @@ CHANGELOG 2.3.0 ----- + * [BC BREAK] the `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RegisterKernelListenersPass` was moved + to `Component\HttpKernel\DependencyInjection\RegisterListenersPass` * added ControllerNameParser::build() to converts a controller short notation (a:b:c) to a class::method notation * added possibility to run PHP built-in server in production environment * added possibility to load the serializer component in the service container diff --git a/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php b/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php index d96e187125280..085ad876df1e9 100644 --- a/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php +++ b/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php @@ -15,7 +15,6 @@ use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddValidatorInitializersPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FormPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TemplatingPass; -use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RegisterKernelListenersPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RoutingResolverPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslatorPass; @@ -31,6 +30,7 @@ use Symfony\Component\DependencyInjection\Compiler\PassConfig; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Bundle\Bundle; +use Symfony\Component\HttpKernel\DependencyInjection\RegisterListenersPass; /** * Bundle. @@ -56,7 +56,7 @@ public function build(ContainerBuilder $container) $container->addCompilerPass(new RoutingResolverPass()); $container->addCompilerPass(new ProfilerPass()); - $container->addCompilerPass(new RegisterKernelListenersPass(), PassConfig::TYPE_AFTER_REMOVING); + $container->addCompilerPass(new RegisterListenersPass(), PassConfig::TYPE_AFTER_REMOVING); $container->addCompilerPass(new TemplatingPass()); $container->addCompilerPass(new AddConstraintValidatorsPass()); $container->addCompilerPass(new AddValidatorInitializersPass()); diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/RegisterKernelListenersPass.php b/src/Symfony/Component/HttpKernel/DependencyInjection/RegisterListenersPass.php similarity index 91% rename from src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/RegisterKernelListenersPass.php rename to src/Symfony/Component/HttpKernel/DependencyInjection/RegisterListenersPass.php index c7e3380cff72a..ee00fbb6656bc 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/RegisterKernelListenersPass.php +++ b/src/Symfony/Component/HttpKernel/DependencyInjection/RegisterListenersPass.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; +namespace Symfony\Component\HttpKernel\DependencyInjection; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; @@ -17,7 +17,7 @@ /** * Compiler pass to register tagged services for an event dispatcher. */ -class RegisterKernelListenersPass implements CompilerPassInterface +class RegisterListenersPass implements CompilerPassInterface { /** * @var string @@ -35,9 +35,11 @@ class RegisterKernelListenersPass implements CompilerPassInterface protected $subscriberTag; /** + * Constructor. + * * @param string $dispatcherService Service name of the event dispatcher in processed container - * @param string $listenerTag Tag name used for listener - * @param string $listenerTag Tag name used for subscribers + * @param string $listenerTag Tag name used for listener + * @param string $subscriberTag Tag name used for subscribers */ public function __construct($dispatcherService = 'event_dispatcher', $listenerTag = 'kernel.event_listener', $subscriberTag = 'kernel.event_subscriber') { diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/RegisterKernelListenersPassTest.php b/src/Symfony/Component/HttpKernel/Tests/DependencyInjection/RegisterListenersPassTest.php similarity index 84% rename from src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/RegisterKernelListenersPassTest.php rename to src/Symfony/Component/HttpKernel/Tests/DependencyInjection/RegisterListenersPassTest.php index 670052fe1b3ad..d1e825a8241b2 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/RegisterKernelListenersPassTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/DependencyInjection/RegisterListenersPassTest.php @@ -9,13 +9,13 @@ * file that was distributed with this source code. */ -namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler; +namespace Symfony\Component\HttpKernel\Tests\DependencyInjection; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; -use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RegisterKernelListenersPass; +use Symfony\Component\HttpKernel\DependencyInjection\RegisterListenersPass; -class RegisterKernelListenersPassTest extends \PHPUnit_Framework_TestCase +class RegisterListenersPassTest extends \PHPUnit_Framework_TestCase { /** * Tests that event subscribers not implementing EventSubscriberInterface @@ -49,7 +49,7 @@ public function testEventSubscriberWithoutInterface() ->method('getDefinition') ->will($this->returnValue($definition)); - $registerListenersPass = new RegisterKernelListenersPass(); + $registerListenersPass = new RegisterListenersPass(); $registerListenersPass->process($builder); } @@ -62,7 +62,7 @@ public function testValidEventSubscriber() $definition = $this->getMock('Symfony\Component\DependencyInjection\Definition'); $definition->expects($this->atLeastOnce()) ->method('getClass') - ->will($this->returnValue('Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\SubscriberService')); + ->will($this->returnValue('Symfony\Component\HttpKernel\Tests\DependencyInjection\SubscriberService')); $builder = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder'); $builder->expects($this->any()) @@ -78,7 +78,7 @@ public function testValidEventSubscriber() ->method('getDefinition') ->will($this->returnValue($definition)); - $registerListenersPass = new RegisterKernelListenersPass(); + $registerListenersPass = new RegisterListenersPass(); $registerListenersPass->process($builder); } }