Commit da662429 authored by catch's avatar catch

Issue #2682373 by klausi, thePanz: Implement ContainerAwareEventDispatcher::getListenerPriority()

parent 3c7e318e
......@@ -156,6 +156,35 @@ public function getListeners($event_name = NULL) {
return $result;
}
/**
* {@inheritdoc}
*/
public function getListenerPriority($eventName, $listener) {
// Parts copied from \Symfony\Component\EventDispatcher, that's why you see
// a yoda condition here.
if (!isset($this->listeners[$eventName])) {
return;
}
foreach ($this->listeners[$eventName] as $priority => $listeners) {
if (FALSE !== ($key = array_search(['callable' => $listener], $listeners, TRUE))) {
return $priority;
}
}
// Resolve service definitions if the listener has not been found so far.
foreach ($this->listeners[$eventName] as $priority => &$definitions) {
foreach ($definitions as $key => &$definition) {
if (!isset($definition['callable'])) {
// Once the callable is retrieved we keep it for subsequent method
// invocations on this class.
$definition['callable'] = [$this->container->get($definition['service'][0]), $definition['service'][1]];
if ($definition['callable'] === $listener) {
return $priority;
}
}
}
}
}
/**
* {@inheritdoc}
*/
......
......@@ -172,10 +172,24 @@ public function testRemoveService()
$this->assertTrue($otherService->preFooInvoked);
}
public function testGetListenerPriority()
public function testGetListenerPriorityWithServices()
{
// Override the parent test as our implementation doesn't define
// getListenerPriority().
$container = new ContainerBuilder();
$container->register('listener_service', TestEventListener::class);
$listeners = array(
'test_event' => array(
5 => array(
array('service' => array('listener_service', 'preFoo')),
),
),
);
$dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
$listenerService = $container->get('listener_service');
$actualPriority = $dispatcher->getListenerPriority('test_event', [$listenerService, 'preFoo']);
$this->assertSame(5, $actualPriority);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment