Skip to content

Commit

Permalink
Restore container-aware event dispatcher
Browse files Browse the repository at this point in the history
for supported Symfony versions.

The container aware variant has been deprecated (3.4) and removed (4.0),
but is still required to keep the RegisterListenersPass happy (2.8).
  • Loading branch information
UrGuardian4ngel committed Dec 28, 2017
1 parent fd171df commit ef22792
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
5 changes: 4 additions & 1 deletion resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ services:
public: true

event_dispatcher:
class: Symfony\Component\EventDispatcher\EventDispatcher
class: Symfony\Component\EventDispatcher\EventDispatcherInterface
factory: GrumPHP\Event\EventDispatcherFactory::create
arguments:
- '@service_container'
public: true

filesystem:
Expand Down
37 changes: 37 additions & 0 deletions spec/Event/EventDispatcherFactorySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace spec\GrumPHP\Event;

use PhpSpec\Exception\Example\SkippingException;
use PhpSpec\ObjectBehavior;
use GrumPHP\Event\EventDispatcherFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcher;

class EventDispatcherFactorySpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType(EventDispatcherFactory::class);
}

function it_creates_an_event_dispatcher(
ContainerInterface $container
) {
$eventDispatcher = self::create($container);
$eventDispatcher->shouldBeAnInstanceOf(EventDispatcher::class);
}

function it_creates_a_container_aware_event_dispatcher_instead_when_available(
ContainerInterface $container
) {
if (!class_exists(ContainerAwareEventDispatcher::class)) {
throw new SkippingException('A container-aware implementation of the event dispatcher is no longer available.');
}

$eventDispatcher = self::create($container);
$eventDispatcher->shouldBeAnInstanceOf(ContainerAwareEventDispatcher::class);
$eventDispatcher->getContainer()->shouldReturn($container);
}
}
25 changes: 25 additions & 0 deletions src/Event/EventDispatcherFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace GrumPHP\Event;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcher;

class EventDispatcherFactory
{
/**
* @param ContainerInterface $container
*
* @return EventDispatcherInterface
*/
public static function create(ContainerInterface $container)
{
if (class_exists(ContainerAwareEventDispatcher::class)) {
return new ContainerAwareEventDispatcher($container);
}

return new EventDispatcher();
}
}

0 comments on commit ef22792

Please sign in to comment.