4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\EventDispatcher
;
15 * The EventDispatcherInterface is the central point of Symfony's event listener system.
17 * Listeners are registered on the manager and events are dispatched through the
20 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
21 * @author Jonathan Wage <jonwage@gmail.com>
22 * @author Roman Borschel <roman@code-factory.org>
23 * @author Bernhard Schussek <bschussek@gmail.com>
24 * @author Fabien Potencier <fabien@symfony.com>
25 * @author Jordi Boggiano <j.boggiano@seld.be>
26 * @author Jordan Alliot <jordan.alliot@gmail.com>
30 class EventDispatcher
implements EventDispatcherInterface
32 private $listeners = array();
33 private $sorted = array();
36 * @see EventDispatcherInterface::dispatch
40 public function dispatch($eventName, Event
$event = null)
42 if (null === $event) {
46 $event->setDispatcher($this);
47 $event->setName($eventName);
49 if (!isset($this->listeners
[$eventName])) {
53 $this->doDispatch($this->getListeners($eventName), $eventName, $event);
59 * @see EventDispatcherInterface::getListeners
61 public function getListeners($eventName = null)
63 if (null !== $eventName) {
64 if (!isset($this->sorted
[$eventName])) {
65 $this->sortListeners($eventName);
68 return $this->sorted
[$eventName];
71 foreach (array_keys($this->listeners
) as $eventName) {
72 if (!isset($this->sorted
[$eventName])) {
73 $this->sortListeners($eventName);
81 * @see EventDispatcherInterface::hasListeners
83 public function hasListeners($eventName = null)
85 return (Boolean
) count($this->getListeners($eventName));
89 * @see EventDispatcherInterface::addListener
93 public function addListener($eventName, $listener, $priority = 0)
95 $this->listeners
[$eventName][$priority][] = $listener;
96 unset($this->sorted
[$eventName]);
100 * @see EventDispatcherInterface::removeListener
102 public function removeListener($eventName, $listener)
104 if (!isset($this->listeners
[$eventName])) {
108 foreach ($this->listeners
[$eventName] as $priority => $listeners) {
109 if (false !== ($key = array_search($listener, $listeners, true))) {
110 unset($this->listeners
[$eventName][$priority][$key], $this->sorted
[$eventName]);
116 * @see EventDispatcherInterface::addSubscriber
120 public function addSubscriber(EventSubscriberInterface
$subscriber)
122 foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
123 if (is_string($params)) {
124 $this->addListener($eventName, array($subscriber, $params));
125 } elseif (is_string($params[0])) {
126 $this->addListener($eventName, array($subscriber, $params[0]), isset($params[1]) ? $params[1] : 0);
128 foreach ($params as $listener) {
129 $this->addListener($eventName, array($subscriber, $listener[0]), isset($listener[1]) ? $listener[1] : 0);
136 * @see EventDispatcherInterface::removeSubscriber
138 public function removeSubscriber(EventSubscriberInterface
$subscriber)
140 foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
141 if (is_array($params) && is_array($params[0])) {
142 foreach ($params as $listener) {
143 $this->removeListener($eventName, array($subscriber, $listener[0]));
146 $this->removeListener($eventName, array($subscriber, is_string($params) ? $params : $params[0]));
152 * Triggers the listeners of an event.
154 * This method can be overridden to add functionality that is executed
157 * @param array[callback] $listeners The event listeners.
158 * @param string $eventName The name of the event to dispatch.
159 * @param Event $event The event object to pass to the event handlers/listeners.
161 protected function doDispatch($listeners, $eventName, Event
$event)
163 foreach ($listeners as $listener) {
164 call_user_func($listener, $event);
165 if ($event->isPropagationStopped()) {
172 * Sorts the internal list of listeners for the given event by priority.
174 * @param string $eventName The name of the event.
176 private function sortListeners($eventName)
178 $this->sorted
[$eventName] = array();
180 if (isset($this->listeners
[$eventName])) {
181 krsort($this->listeners
[$eventName]);
182 $this->sorted
[$eventName] = call_user_func_array('array_merge', $this->listeners
[$eventName]);