Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\EventDispatcher; Chris@0: Chris@0: /** Chris@0: * The EventDispatcherInterface is the central point of Symfony's event listener system. Chris@0: * Chris@0: * Listeners are registered on the manager and events are dispatched through the Chris@0: * manager. Chris@0: * Chris@0: * @author Guilherme Blanco Chris@0: * @author Jonathan Wage Chris@0: * @author Roman Borschel Chris@0: * @author Bernhard Schussek Chris@0: * @author Fabien Potencier Chris@0: * @author Jordi Boggiano Chris@0: * @author Jordan Alliot Chris@0: */ Chris@0: class EventDispatcher implements EventDispatcherInterface Chris@0: { Chris@0: private $listeners = array(); Chris@0: private $sorted = array(); Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function dispatch($eventName, Event $event = null) Chris@0: { Chris@0: if (null === $event) { Chris@0: $event = new Event(); Chris@0: } Chris@0: Chris@0: if ($listeners = $this->getListeners($eventName)) { Chris@0: $this->doDispatch($listeners, $eventName, $event); Chris@0: } Chris@0: Chris@0: return $event; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getListeners($eventName = null) Chris@0: { Chris@0: if (null !== $eventName) { Chris@0: if (!isset($this->listeners[$eventName])) { Chris@0: return array(); Chris@0: } Chris@0: Chris@0: if (!isset($this->sorted[$eventName])) { Chris@0: $this->sortListeners($eventName); Chris@0: } Chris@0: Chris@0: return $this->sorted[$eventName]; Chris@0: } Chris@0: Chris@0: foreach ($this->listeners as $eventName => $eventListeners) { Chris@0: if (!isset($this->sorted[$eventName])) { Chris@0: $this->sortListeners($eventName); Chris@0: } Chris@0: } Chris@0: Chris@0: return array_filter($this->sorted); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getListenerPriority($eventName, $listener) Chris@0: { Chris@0: if (!isset($this->listeners[$eventName])) { Chris@0: return; Chris@0: } Chris@0: Chris@0: foreach ($this->listeners[$eventName] as $priority => $listeners) { Chris@0: if (false !== in_array($listener, $listeners, true)) { Chris@0: return $priority; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function hasListeners($eventName = null) Chris@0: { Chris@0: return (bool) $this->getListeners($eventName); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function addListener($eventName, $listener, $priority = 0) Chris@0: { Chris@0: $this->listeners[$eventName][$priority][] = $listener; Chris@0: unset($this->sorted[$eventName]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function removeListener($eventName, $listener) Chris@0: { Chris@0: if (!isset($this->listeners[$eventName])) { Chris@0: return; Chris@0: } Chris@0: Chris@0: foreach ($this->listeners[$eventName] as $priority => $listeners) { Chris@0: if (false !== ($key = array_search($listener, $listeners, true))) { Chris@0: unset($this->listeners[$eventName][$priority][$key], $this->sorted[$eventName]); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function addSubscriber(EventSubscriberInterface $subscriber) Chris@0: { Chris@0: foreach ($subscriber->getSubscribedEvents() as $eventName => $params) { Chris@0: if (is_string($params)) { Chris@0: $this->addListener($eventName, array($subscriber, $params)); Chris@0: } elseif (is_string($params[0])) { Chris@0: $this->addListener($eventName, array($subscriber, $params[0]), isset($params[1]) ? $params[1] : 0); Chris@0: } else { Chris@0: foreach ($params as $listener) { Chris@0: $this->addListener($eventName, array($subscriber, $listener[0]), isset($listener[1]) ? $listener[1] : 0); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function removeSubscriber(EventSubscriberInterface $subscriber) Chris@0: { Chris@0: foreach ($subscriber->getSubscribedEvents() as $eventName => $params) { Chris@0: if (is_array($params) && is_array($params[0])) { Chris@0: foreach ($params as $listener) { Chris@0: $this->removeListener($eventName, array($subscriber, $listener[0])); Chris@0: } Chris@0: } else { Chris@0: $this->removeListener($eventName, array($subscriber, is_string($params) ? $params : $params[0])); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Triggers the listeners of an event. Chris@0: * Chris@0: * This method can be overridden to add functionality that is executed Chris@0: * for each listener. Chris@0: * Chris@0: * @param callable[] $listeners The event listeners Chris@0: * @param string $eventName The name of the event to dispatch Chris@0: * @param Event $event The event object to pass to the event handlers/listeners Chris@0: */ Chris@0: protected function doDispatch($listeners, $eventName, Event $event) Chris@0: { Chris@0: foreach ($listeners as $listener) { Chris@0: if ($event->isPropagationStopped()) { Chris@0: break; Chris@0: } Chris@0: call_user_func($listener, $event, $eventName, $this); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sorts the internal list of listeners for the given event by priority. Chris@0: * Chris@0: * @param string $eventName The name of the event Chris@0: */ Chris@0: private function sortListeners($eventName) Chris@0: { Chris@0: krsort($this->listeners[$eventName]); Chris@0: $this->sorted[$eventName] = call_user_func_array('array_merge', $this->listeners[$eventName]); Chris@0: } Chris@0: }