EventDispatcher.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\EventDispatcher;
  11. /**
  12. * The EventDispatcherInterface is the central point of Symfony's event listener system.
  13. *
  14. * Listeners are registered on the manager and events are dispatched through the
  15. * manager.
  16. *
  17. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  18. * @author Jonathan Wage <jonwage@gmail.com>
  19. * @author Roman Borschel <roman@code-factory.org>
  20. * @author Bernhard Schussek <bschussek@gmail.com>
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. * @author Jordi Boggiano <j.boggiano@seld.be>
  23. * @author Jordan Alliot <jordan.alliot@gmail.com>
  24. *
  25. * @api
  26. */
  27. class EventDispatcher implements EventDispatcherInterface
  28. {
  29. private $listeners = array();
  30. private $sorted = array();
  31. /**
  32. * @see EventDispatcherInterface::dispatch
  33. *
  34. * @api
  35. */
  36. public function dispatch($eventName, Event $event = null)
  37. {
  38. if (null === $event) {
  39. $event = new Event();
  40. }
  41. $event->setDispatcher($this);
  42. $event->setName($eventName);
  43. if (!isset($this->listeners[$eventName])) {
  44. return $event;
  45. }
  46. $this->doDispatch($this->getListeners($eventName), $eventName, $event);
  47. return $event;
  48. }
  49. /**
  50. * @see EventDispatcherInterface::getListeners
  51. */
  52. public function getListeners($eventName = null)
  53. {
  54. if (null !== $eventName) {
  55. if (!isset($this->sorted[$eventName])) {
  56. $this->sortListeners($eventName);
  57. }
  58. return $this->sorted[$eventName];
  59. }
  60. foreach (array_keys($this->listeners) as $eventName) {
  61. if (!isset($this->sorted[$eventName])) {
  62. $this->sortListeners($eventName);
  63. }
  64. }
  65. return $this->sorted;
  66. }
  67. /**
  68. * @see EventDispatcherInterface::hasListeners
  69. */
  70. public function hasListeners($eventName = null)
  71. {
  72. return (Boolean) count($this->getListeners($eventName));
  73. }
  74. /**
  75. * @see EventDispatcherInterface::addListener
  76. *
  77. * @api
  78. */
  79. public function addListener($eventName, $listener, $priority = 0)
  80. {
  81. $this->listeners[$eventName][$priority][] = $listener;
  82. unset($this->sorted[$eventName]);
  83. }
  84. /**
  85. * @see EventDispatcherInterface::removeListener
  86. */
  87. public function removeListener($eventName, $listener)
  88. {
  89. if (!isset($this->listeners[$eventName])) {
  90. return;
  91. }
  92. foreach ($this->listeners[$eventName] as $priority => $listeners) {
  93. if (false !== ($key = array_search($listener, $listeners, true))) {
  94. unset($this->listeners[$eventName][$priority][$key], $this->sorted[$eventName]);
  95. }
  96. }
  97. }
  98. /**
  99. * @see EventDispatcherInterface::addSubscriber
  100. *
  101. * @api
  102. */
  103. public function addSubscriber(EventSubscriberInterface $subscriber)
  104. {
  105. foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
  106. if (is_string($params)) {
  107. $this->addListener($eventName, array($subscriber, $params));
  108. } elseif (is_string($params[0])) {
  109. $this->addListener($eventName, array($subscriber, $params[0]), isset($params[1]) ? $params[1] : 0);
  110. } else {
  111. foreach ($params as $listener) {
  112. $this->addListener($eventName, array($subscriber, $listener[0]), isset($listener[1]) ? $listener[1] : 0);
  113. }
  114. }
  115. }
  116. }
  117. /**
  118. * @see EventDispatcherInterface::removeSubscriber
  119. */
  120. public function removeSubscriber(EventSubscriberInterface $subscriber)
  121. {
  122. foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
  123. if (is_array($params) && is_array($params[0])) {
  124. foreach ($params as $listener) {
  125. $this->removeListener($eventName, array($subscriber, $listener[0]));
  126. }
  127. } else {
  128. $this->removeListener($eventName, array($subscriber, is_string($params) ? $params : $params[0]));
  129. }
  130. }
  131. }
  132. /**
  133. * Triggers the listeners of an event.
  134. *
  135. * This method can be overridden to add functionality that is executed
  136. * for each listener.
  137. *
  138. * @param array[callback] $listeners The event listeners.
  139. * @param string $eventName The name of the event to dispatch.
  140. * @param Event $event The event object to pass to the event handlers/listeners.
  141. */
  142. protected function doDispatch($listeners, $eventName, Event $event)
  143. {
  144. foreach ($listeners as $listener) {
  145. call_user_func($listener, $event);
  146. if ($event->isPropagationStopped()) {
  147. break;
  148. }
  149. }
  150. }
  151. /**
  152. * Sorts the internal list of listeners for the given event by priority.
  153. *
  154. * @param string $eventName The name of the event.
  155. */
  156. private function sortListeners($eventName)
  157. {
  158. $this->sorted[$eventName] = array();
  159. if (isset($this->listeners[$eventName])) {
  160. krsort($this->listeners[$eventName]);
  161. $this->sorted[$eventName] = call_user_func_array('array_merge', $this->listeners[$eventName]);
  162. }
  163. }
  164. }