Skip to content

Commit

Permalink
Support autowiring for EventDispatcher/EventDispatcherInterface
Browse files Browse the repository at this point in the history
CS Fixes
  • Loading branch information
chalasr committed Nov 16, 2016
1 parent a559036 commit 5fd4733
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/debug.xml
Expand Up @@ -9,9 +9,9 @@
</parameters>

<services>
<service id="debug.event_dispatcher" class="Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher">
<service id="debug.event_dispatcher" class="Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher" decorates="event_dispatcher">
<tag name="monolog.logger" channel="event" />
<argument type="service" id="debug.event_dispatcher.parent" />
<argument type="service" id="debug.event_dispatcher.inner" />
<argument type="service" id="debug.stopwatch" />
<argument type="service" id="logger" on-invalid="null" />
</service>
Expand Down
Expand Up @@ -7,6 +7,8 @@
<services>
<service id="event_dispatcher" class="Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher">
<argument type="service" id="service_container" />
<autowiring-type>Symfony\Component\EventDispatcher\EventDispatcherInterface</autowiring-type>
<autowiring-type>Symfony\Component\EventDispatcher\EventDispatcher</autowiring-type>
</service>

<service id="http_kernel" class="Symfony\Component\HttpKernel\HttpKernel">
Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -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()
Expand All @@ -42,4 +45,9 @@ public function getEngine()
{
return $this->engine;
}

public function getDispatcher()
{
return $this->dispatcher;
}
}

0 comments on commit 5fd4733

Please sign in to comment.