[EventDispatcher] Fix TraceableEventDispatcher FC/BC layer

This commit is contained in:
Robin Chalas 2019-04-25 20:51:40 +02:00
parent 27d10a658d
commit c5b3b34b51
5 changed files with 31 additions and 9 deletions

View File

@ -18,6 +18,7 @@ use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
use Symfony\Component\EventDispatcher\LegacyEventProxy;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
@ -295,7 +296,7 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
*/
protected function beforeDispatch(string $eventName, $event)
{
$this->preDispatch($eventName, $event);
$this->preDispatch($eventName, $event instanceof Event ? $event : new LegacyEventProxy($event));
}
/**
@ -305,7 +306,7 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
*/
protected function afterDispatch(string $eventName, $event)
{
$this->postDispatch($eventName, $event);
$this->postDispatch($eventName, $event instanceof Event ? $event : new LegacyEventProxy($event));
}
/**

View File

@ -14,7 +14,7 @@ namespace Symfony\Component\EventDispatcher\Debug;
use Psr\EventDispatcher\StoppableEventInterface;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\WrappedEvent;
use Symfony\Component\EventDispatcher\LegacyEventProxy;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\VarDumper\Caster\ClassStub;
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
@ -112,8 +112,8 @@ class WrappedListener
public function __invoke(Event $event, $eventName, EventDispatcherInterface $dispatcher)
{
if ($event instanceof WrappedEvent) {
$event = $event->getWrappedEvent();
if ($event instanceof LegacyEventProxy) {
$event = $event->getEvent();
}
$dispatcher = $this->dispatcher ?: $dispatcher;

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\EventDispatcher;
use Psr\EventDispatcher\StoppableEventInterface;
use Symfony\Component\EventDispatcher\Debug\WrappedListener;
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
/**
@ -242,7 +243,8 @@ class EventDispatcher implements EventDispatcherInterface
if ($stoppable && $event->isPropagationStopped()) {
break;
}
$listener($event instanceof Event ? $event : new WrappedEvent($event), $eventName, $this);
// @deprecated: the ternary operator is part of a BC layer and should be removed in 5.0
$listener($listener instanceof WrappedListener ? new LegacyEventProxy($event) : $event, $eventName, $this);
}
}
@ -296,7 +298,7 @@ class EventDispatcher implements EventDispatcherInterface
($closure = \Closure::fromCallable($listener))(...$args);
};
} else {
$closure = $listener instanceof \Closure ? $listener : \Closure::fromCallable($listener);
$closure = $listener instanceof \Closure || $listener instanceof WrappedListener ? $listener : \Closure::fromCallable($listener);
}
}
}

View File

@ -17,7 +17,7 @@ use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
/**
* @internal to be removed in 5.0.
*/
final class WrappedEvent extends Event
final class LegacyEventProxy extends Event
{
private $event;
@ -32,7 +32,7 @@ final class WrappedEvent extends Event
/**
* @return object $event
*/
public function getWrappedEvent()
public function getEvent()
{
return $this->event;
}
@ -54,4 +54,9 @@ final class WrappedEvent extends Event
$this->event->stopPropagation();
}
public function __call($name, $args)
{
return $this->event->{$name}(...$args);
}
}

View File

@ -18,6 +18,7 @@ use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
class TraceableEventDispatcherTest extends TestCase
{
@ -139,6 +140,19 @@ class TraceableEventDispatcherTest extends TestCase
$this->assertEquals([['event' => 'foo', 'pretty' => 'closure', 'priority' => 5]], $listeners);
}
public function testDispatchContractsEvent()
{
$expectedEvent = new ContractsEvent();
$tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
$tdispatcher->addListener('foo', function ($event) use ($expectedEvent) {
$this->assertSame($event, $expectedEvent);
}, 5);
$tdispatcher->dispatch($expectedEvent, 'foo');
$listeners = $tdispatcher->getCalledListeners();
$this->assertArrayHasKey('stub', $listeners[0]);
}
public function testDispatchAfterReset()
{
$tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());