From 5fd47332b75a592b661f910cf091c4e33c34fd71 Mon Sep 17 00:00:00 2001 From: Robin Chalas Date: Thu, 20 Oct 2016 22:53:28 +0200 Subject: [PATCH] Support autowiring for EventDispatcher/EventDispatcherInterface CS Fixes --- .../DependencyInjection/FrameworkExtension.php | 6 ------ .../FrameworkBundle/Resources/config/debug.xml | 4 ++-- .../Resources/config/services.xml | 2 ++ .../Tests/Functional/AutowiringTypesTest.php | 17 +++++++++++++++++ .../AutowiringTypes/AutowiredServices.php | 10 +++++++++- 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index c3eab37c9ca8c..7d5969749a1e0 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -448,12 +448,6 @@ private function registerDebugConfiguration(array $config, ContainerBuilder $con if ($debug) { $loader->load('debug.xml'); - - // replace the regular event_dispatcher service with the debug one - $definition = $container->findDefinition('event_dispatcher'); - $definition->setPublic(false); - $container->setDefinition('debug.event_dispatcher.parent', $definition); - $container->setAlias('event_dispatcher', 'debug.event_dispatcher'); } $definition = $container->findDefinition('debug.debug_handlers_listener'); diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/debug.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/debug.xml index 073851b038e67..3d260e4aff728 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/debug.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/debug.xml @@ -9,9 +9,9 @@ - + - + diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml index f07f5261bb40a..288e5267b108c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml @@ -7,6 +7,8 @@ + Symfony\Component\EventDispatcher\EventDispatcherInterface + Symfony\Component\EventDispatcher\EventDispatcher diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/AutowiringTypesTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/AutowiringTypesTest.php index 52b8c8cdcf521..5fcf207e98ba2 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/AutowiringTypesTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/AutowiringTypesTest.php @@ -14,6 +14,8 @@ use Doctrine\Common\Annotations\AnnotationReader; use Doctrine\Common\Annotations\CachedReader; use Symfony\Component\Templating\EngineInterface as ComponentEngineInterface; +use Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher; +use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher; use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface as FrameworkBundleEngineInterface; class AutowiringTypesTest extends WebTestCase @@ -46,6 +48,21 @@ public function testTemplatingAutowiring() $this->assertInstanceOf(ComponentEngineInterface::class, $autowiredServices->getEngine()); } + public function testEventDispatcherAutowiring() + { + static::bootKernel(array('debug' => false)); + $container = static::$kernel->getContainer(); + + $autowiredServices = $container->get('test.autowiring_types.autowired_services'); + $this->assertInstanceOf(ContainerAwareEventDispatcher::class, $autowiredServices->getDispatcher(), 'The event_dispatcher service should be injected if the debug is not enabled'); + + static::bootKernel(array('debug' => true)); + $container = static::$kernel->getContainer(); + + $autowiredServices = $container->get('test.autowiring_types.autowired_services'); + $this->assertInstanceOf(TraceableEventDispatcher::class, $autowiredServices->getDispatcher(), 'The debug.event_dispatcher service should be injected if the debug is enabled'); + } + protected static function createKernel(array $options = array()) { return parent::createKernel(array('test_case' => 'AutowiringTypes') + $options); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/AutowiringTypes/AutowiredServices.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/AutowiringTypes/AutowiredServices.php index 0636d591e5677..9f9faf29f3cc5 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/AutowiringTypes/AutowiredServices.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/AutowiringTypes/AutowiredServices.php @@ -14,18 +14,21 @@ use Doctrine\Common\Annotations\Reader; use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface as FrameworkBundleEngineInterface; use Symfony\Component\Templating\EngineInterface; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; class AutowiredServices { private $annotationReader; private $frameworkBundleEngine; private $engine; + private $dispatcher; - public function __construct(Reader $annotationReader = null, FrameworkBundleEngineInterface $frameworkBundleEngine, EngineInterface $engine) + public function __construct(Reader $annotationReader = null, FrameworkBundleEngineInterface $frameworkBundleEngine, EngineInterface $engine, EventDispatcherInterface $dispatcher) { $this->annotationReader = $annotationReader; $this->frameworkBundleEngine = $frameworkBundleEngine; $this->engine = $engine; + $this->dispatcher = $dispatcher; } public function getAnnotationReader() @@ -42,4 +45,9 @@ public function getEngine() { return $this->engine; } + + public function getDispatcher() + { + return $this->dispatcher; + } }