diff options
Diffstat (limited to 'vendor/symfony/event-dispatcher/EventDispatcher.php')
-rw-r--r-- | vendor/symfony/event-dispatcher/EventDispatcher.php | 89 |
1 files changed, 74 insertions, 15 deletions
diff --git a/vendor/symfony/event-dispatcher/EventDispatcher.php b/vendor/symfony/event-dispatcher/EventDispatcher.php index f1b63f70..bc79a958 100644 --- a/vendor/symfony/event-dispatcher/EventDispatcher.php +++ b/vendor/symfony/event-dispatcher/EventDispatcher.php @@ -24,6 +24,7 @@ namespace Symfony\Component\EventDispatcher; * @author Fabien Potencier <fabien@symfony.com> * @author Jordi Boggiano <j.boggiano@seld.be> * @author Jordan Alliot <jordan.alliot@gmail.com> + * @author Nicolas Grekas <p@tchwork.com> */ class EventDispatcher implements EventDispatcherInterface { @@ -39,9 +40,6 @@ class EventDispatcher implements EventDispatcherInterface $event = new Event(); } - $event->setDispatcher($this); - $event->setName($eventName); - if ($listeners = $this->getListeners($eventName)) { $this->doDispatch($listeners, $eventName, $event); } @@ -55,7 +53,7 @@ class EventDispatcher implements EventDispatcherInterface public function getListeners($eventName = null) { if (null !== $eventName) { - if (!isset($this->listeners[$eventName])) { + if (empty($this->listeners[$eventName])) { return array(); } @@ -78,9 +76,45 @@ class EventDispatcher implements EventDispatcherInterface /** * {@inheritdoc} */ + public function getListenerPriority($eventName, $listener) + { + if (empty($this->listeners[$eventName])) { + return; + } + + if (is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure) { + $listener[0] = $listener[0](); + } + + foreach ($this->listeners[$eventName] as $priority => $listeners) { + foreach ($listeners as $k => $v) { + if ($v !== $listener && is_array($v) && isset($v[0]) && $v[0] instanceof \Closure) { + $v[0] = $v[0](); + $this->listeners[$eventName][$priority][$k] = $v; + } + if ($v === $listener) { + return $priority; + } + } + } + } + + /** + * {@inheritdoc} + */ public function hasListeners($eventName = null) { - return (bool) count($this->getListeners($eventName)); + if (null !== $eventName) { + return !empty($this->listeners[$eventName]); + } + + foreach ($this->listeners as $eventListeners) { + if ($eventListeners) { + return true; + } + } + + return false; } /** @@ -97,13 +131,30 @@ class EventDispatcher implements EventDispatcherInterface */ public function removeListener($eventName, $listener) { - if (!isset($this->listeners[$eventName])) { + if (empty($this->listeners[$eventName])) { return; } + if (is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure) { + $listener[0] = $listener[0](); + } + foreach ($this->listeners[$eventName] as $priority => $listeners) { - if (false !== ($key = array_search($listener, $listeners, true))) { - unset($this->listeners[$eventName][$priority][$key], $this->sorted[$eventName]); + foreach ($listeners as $k => $v) { + if ($v !== $listener && is_array($v) && isset($v[0]) && $v[0] instanceof \Closure) { + $v[0] = $v[0](); + } + if ($v === $listener) { + unset($listeners[$k], $this->sorted[$eventName]); + } else { + $listeners[$k] = $v; + } + } + + if ($listeners) { + $this->listeners[$eventName][$priority] = $listeners; + } else { + unset($this->listeners[$eventName][$priority]); } } } @@ -148,9 +199,9 @@ class EventDispatcher implements EventDispatcherInterface * This method can be overridden to add functionality that is executed * for each listener. * - * @param callable[] $listeners The event listeners. - * @param string $eventName The name of the event to dispatch. - * @param Event $event The event object to pass to the event handlers/listeners. + * @param callable[] $listeners The event listeners + * @param string $eventName The name of the event to dispatch + * @param Event $event The event object to pass to the event handlers/listeners */ protected function doDispatch($listeners, $eventName, Event $event) { @@ -158,20 +209,28 @@ class EventDispatcher implements EventDispatcherInterface if ($event->isPropagationStopped()) { break; } - call_user_func($listener, $event, $eventName, $this); + \call_user_func($listener, $event, $eventName, $this); } } /** * Sorts the internal list of listeners for the given event by priority. * - * @param string $eventName The name of the event. + * @param string $eventName The name of the event */ private function sortListeners($eventName) { + krsort($this->listeners[$eventName]); $this->sorted[$eventName] = array(); - krsort($this->listeners[$eventName]); - $this->sorted[$eventName] = call_user_func_array('array_merge', $this->listeners[$eventName]); + foreach ($this->listeners[$eventName] as $priority => $listeners) { + foreach ($listeners as $k => $listener) { + if (\is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure) { + $listener[0] = $listener[0](); + $this->listeners[$eventName][$priority][$k] = $listener; + } + $this->sorted[$eventName][] = $listener; + } + } } } |