Skip to content
Snippets Groups Projects
Commit da662429 authored by catch's avatar catch
Browse files

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

parent 3c7e318e
Branches
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment