8000 [EventDispatcher] Fix TraceableEventDispatcher FC/BC layer by chalasr · Pull Request #31254 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[EventDispatcher] Fix TraceableEventDispatcher FC/BC layer #31254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[EventDispatcher] Fix TraceableEventDispatcher FC/BC layer
  • Loading branch information
Robin Chalas committed Apr 27, 2019
commit c5b3b34b51a07e361d0eb3cf44fa8a2e01224329
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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;
Expand Down Expand Up @@ -295,7 +296,7 @@ public function __call($method, $arguments)
*/
protected function beforeDispatch(string $eventName, $event)
{
$this->preDispatch($eventName, $event);
$this->preDispatch($eventName, $event instanceof Event ? $event : new LegacyEventProxy($event));
}

/**
Expand All @@ -305,7 +306,7 @@ protected function beforeDispatch(string $eventName, $event)
*/
protected function afterDispatch(string $eventName, $event)
{
$this->postDispatch($eventName, $event);
$this->postDispatch($eventName, $event instanceof Event ? $event : new LegacyEventProxy($event));
}

/**
Expand Down
8000
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
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;
Expand Down Expand Up @@ -112,8 +112,8 @@ public function getInfo($eventName)

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;
Expand Down
6 changes: 4 additions & 2 deletions src/Symfony/Component/EventDispatcher/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -242,7 +243,8 @@ protected function callListeners(iterable $listeners, string $eventName, $event)
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);
}
}

Expand Down Expand Up @@ -296,7 +298,7 @@ private function optimizeListeners(string $eventName): array
($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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WrappedListener handles this optimization internally

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* @internal to be removed in 5.0.
*/
final class WrappedEvent extends Event
final class LegacyEventProxy extends Event
{
private $event;

Expand All @@ -32,7 +32,7 @@ public function __construct($event)
/**
* @return object $event
*/
public function getWrappedEvent()
public function getEvent()
{
return $this->event;
}
Expand All @@ -54,4 +54,9 @@ public function stopPropagation()

$this->event->stopPropagation();
}

public function __call($name, $args)
{
return $this->event->{$name}(...$args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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
{
Expand Down Expand Up @@ -139,6 +140,19 @@ public function testClearCalledListeners()
$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());
Expand Down
0