8000 [EventDispatcher] fix BC layer by nicolas-grekas · Pull Request #31258 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[EventDispatcher] fix BC layer #31258

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

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,9 @@ public function __call($method, $arguments)
*/
protected function beforeDispatch(string $eventName, $event)
{
$this->preDispatch($eventName, $event);
if ($event instanceof Event) {
$this->preDispatch($eventName, $event);
}
}

/**
Expand All @@ -305,7 +307,9 @@ protected function beforeDispatch(string $eventName, $event)
*/
protected function afterDispatch(string $eventName, $event)
{
$this->postDispatch($eventName, $event);
if ($event instanceof Event) {
$this->postDispatch($eventName, $event);
}
}

/**
Expand Down
4 changes: 3 additions & 1 deletion 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($event instanceof Event || !$listener instanceof WrappedListener ? $event : new WrappedEvent($event), $eventName, $this);
Copy link
Member
@chalasr chalasr Apr 26, 2019

Choose a reason for hiding this comment

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

$event instanceof Event is always false because of the early return l234
and !$listener instanceof WrappedListener is always true as listeners are optimized at this point (plain \Closure).
Broken test case:

diff --git a/src/Symfony/Component/EventDispatcher/Tests/Debug/TraceableEventDispatcherTest.php b/src/Symfony/Component/EventDispatcher/Tests/Debug/TraceableEventDispatcherTest.php
index 8fd0305b0e..db3176dcd1 100644
--- a/src/Symfony/Component/EventDispatcher/Tests/Debug/TraceableEventDispatcherTest.php
+++ b/src/Symfony/Component/EventDispatcher/Tests/Debug/TraceableEventDispatcherTest.php
@@ -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
 {
@@ -151,6 +152,15 @@ class TraceableEventDispatcherTest extends TestCase
         $this->assertArrayHasKey('stub', $listeners[0]);
     }
 
+    public function testDispatchContractsEvent()
+    {
+        $tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
+        $tdispatcher->addListener('foo', function () {}, 5);
+        $tdispatcher->dispatch(new ContractsEvent(), 'foo');
+        $listeners = $tdispatcher->getCalledListeners();
+        $this->assertArrayHasKey('stub', $listeners[0]);
+    }
+
     public function testGetCalledListenersNested()
     {
         $tdispatcher = null;

}
}

Expand Down
0