Skip to content

[HttpKernel] [EventDispatcher] TraceableEventDispatcher kills event priorities #8405

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 2 commits 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 @@ -96,7 +96,7 @@ public function removeListener($eventName, $listener)
}
}

parent::removeListener($eventName, $listener);
return parent::removeListener($eventName, $listener);
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/EventDispatcher/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ public function removeListener($eventName, $listener)
}
}

/**
* @see EventDispatcherInterface::getListenerPriority
*/
public function getListenerPriority($eventName, $listener) {
if (!isset($this->listeners[$eventName])) {
return null;
}

foreach ($this->listeners[$eventName] as $priority => $listeners) {
if (false !== ($key = array_search($listener, $listeners, true))) {
return $priority;
}
}

return null;
}

/**
* @see EventDispatcherInterface::addSubscriber
*
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,14 @@ public function getListeners($eventName = null);
* @return Boolean true if the specified event has any listeners, false otherwise
*/
public function hasListeners($eventName = null);

/**
* Returns the priority of the given listener.
*
* @param string $eventName The event to get the listener's priority
* @param callable $listener The listener for which the priority is returned
*
* @return integer|null The listener's priority value, null if the listener wasn't found
*/
public function getListenerPriority($eventName, $listener);
Copy link
Member

Choose a reason for hiding this comment

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

adding a new method in an existing interface is, unfortunately, also a BC break :(

}
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,12 @@ public function hasListeners($eventName = null)
{
return $this->dispatcher->hasListeners($eventName);
}

/**
* {@inheritdoc}
*/
public function getListenerPriority($eventName, $listener)
{
return $this->dispatcher->getListenerPriority($eventName, $listener);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,19 @@ public function testDispatchByPriority()
$this->assertEquals(array('3', '2', '1'), $invoked);
}

public function testGetListenerPriorty()
{
$listener1 = new TestEventListener();
$listener2 = new TestEventListener();
$listener3 = new TestEventListener();
$this->dispatcher->addListener('pre.foo', $listener1, -10);
$this->dispatcher->addListener('pre.foo', $listener2);
$this->assertEquals(-10, $this->dispatcher->getListenerPriority('pre.foo', $listener1));
$this->assertEquals(0, $this->dispatcher->getListenerPriority('pre.foo', $listener2));
$this->assertNull($this->dispatcher->getListenerPriority('pre.bar', $listener1));
$this->assertNull($this->dispatcher->getListenerPriority('pre.foo', $listener3));
}

public function testRemoveListener()
{
$this->dispatcher->addListener('pre.bar', $this->listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ public function hasListeners($eventName = null)
return $this->dispatcher->hasListeners($eventName);
}

/**
* {@inheritdoc}
*/
public function getListenerPriority($eventName, $listener)
{
return $this->dispatcher->getListenerPriority($eventName, $listener);
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -372,10 +380,11 @@ private function preDispatch($eventName, Event $event)
$listeners = $this->dispatcher->getListeners($eventName);

foreach ($listeners as $listener) {
$priority = $this->dispatcher->getListenerPriority($eventName, $listener);
$this->dispatcher->removeListener($eventName, $listener);
$wrapped = $this->wrapListener($eventName, $listener);
$this->wrappedListeners[$this->id][$wrapped] = $listener;
$this->dispatcher->addListener($eventName, $wrapped);
$this->dispatcher->addListener($eventName, $wrapped, $priority);
}

switch ($eventName) {
Expand Down Expand Up @@ -433,8 +442,9 @@ private function postDispatch($eventName, Event $event)
}

foreach ($this->wrappedListeners[$this->id] as $wrapped) {
$priority = $this->dispatcher->getListenerPriority($eventName, $wrapped);
$this->dispatcher->removeListener($eventName, $wrapped);
$this->dispatcher->addListener($eventName, $this->wrappedListeners[$this->id][$wrapped]);
$this->dispatcher->addListener($eventName, $this->wrappedListeners[$this->id][$wrapped], $priority);
}

unset($this->wrappedListeners[$this->id]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ public function testHasListeners()
$this->assertTrue($tdispatcher->hasListeners('foo'));
}

public function testGetListenerPriority()
{
$dispatcher = new EventDispatcher();
$tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());

$tdispatcher->addListener('foo', $listener = function () { ; }, 10);
$this->assertSame($dispatcher->getListenerPriority('foo', $listener), $tdispatcher->getListenerPriority('foo', $listener));
}

public function testAddRemoveSubscriber()
{
$dispatcher = new EventDispatcher();
Expand Down Expand Up @@ -162,6 +171,26 @@ public function testDispatchNested()
$dispatcher->dispatch('foo');
}

public function testDispatchByPriority()
{
$invoked = array();
$listener1 = function () use (&$invoked) {
$invoked[] = '1';
};
$listener2 = function () use (&$invoked) {
$invoked[] = '2';
};
$listener3 = function () use (&$invoked) {
$invoked[] = '3';
};
$dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
$dispatcher->addListener('pre.foo', $listener1, -10);
$dispatcher->addListener('pre.foo', $listener2);
$dispatcher->addListener('pre.foo', $listener3, 10);
$dispatcher->dispatch('pre.foo');
$this->assertEquals(array('3', '2', '1'), $invoked);
}

public function testStopwatchSections()
{
$dispatcher = new TraceableEventDispatcher(new EventDispatcher(), $stopwatch = new Stopwatch());
Expand Down