8000 [EventDispatcher] fix getting priorities of listeners during dispatch · symfony/symfony@79b71c0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 79b71c0

Browse files
committed
[EventDispatcher] fix getting priorities of listeners during dispatch
1 parent eb1e277 commit 79b71c0

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ public function getListeners($eventName = null)
104104
*/
105105
public function getListenerPriority($eventName, $listener)
106106
{
107+
// we might have wrapped listeners for the event (if called while dispatching)
108+
// in that case get the priority by wrapper
109+
if (isset($this->wrappedListeners[$eventName])) {
110+
foreach ($this->wrappedListeners[$eventName] as $index => $wrappedListener) {
111+
if ($wrappedListener->getWrappedListener() === $listener) {
112+
return $this->dispatcher->getListenerPriority($eventName, $wrappedListener);
113+
}
114+
}
115+
}
116+
107117
return $this->dispatcher->getListenerPriority($eventName, $listener);
108118
}
109119

src/Symfony/Component/EventDispatcher/Tests/Debug/TraceableEventDispatcherTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@ public function testGetListenerPriority()
7474
$this->assertSame(123, $tdispatcher->getListenerPriority('foo', $listeners[0]));
7575
}
7676

77+
public function testGetListenerPriorityWhileDispatching()
78+
{
79+
$tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
80+
$priorityWhileDispatching = null;
81+
82+
$listener = function () use ($tdispatcher, &$priorityWhileDispatc B38B hing, &$listener) {
83+
$priorityWhileDispatching = $tdispatcher->getListenerPriority('bar', $listener);
84+
};
85+
86+
$tdispatcher->addListener('bar', $listener, 5);
87+
$tdispatcher->dispatch('bar');
88+
$this->assertSame(5, $priorityWhileDispatching);
89+
}
90+
7791
public function testAddRemoveSubscriber()
7892
{
7993
$dispatcher = new EventDispatcher();
@@ -107,7 +121,7 @@ public function testGetCalledListeners()
107121
$listeners = $tdispatcher->getCalledListeners();
108122
$this->assertArrayHasKey('data', $listeners['foo.closure']);
109123
unset($listeners['foo.closure']['data']);
110-
$this->assertEquals(array('foo.closure' => array('event' => 'foo', 'pretty' => 'closure', 'priority' => null)), $listeners);
124+
$this->assertEquals(array('foo.closure' => array('event' => 'foo', 'pretty' => 'closure', 'priority' => 0)), $listeners);
111125
$this->assertEquals(array(), $tdispatcher->getNotCalledListeners());
112126
}
113127

0 commit comments

Comments
 (0)
0