EventDispatcher.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. class EventDispatcher implements EventDispatcherInterface
  26. {
  27. private $listeners = array();
  28. private $sorted = array();
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function dispatch($eventName, Event $event = null)
  33. {
  34. if (null === $event) {
  35. $event = new Event();
  36. }
  37. $event->setDispatcher($this);
  38. $event->setName($eventName);
  39. if ($listeners = $this->getListeners($eventName)) {
  40. $this->doDispatch($listeners, $eventName, $event);
  41. }
  42. return $event;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function getListeners($eventName = null)
  48. {
  49. if (null !== $eventName) {
  50. if (!isset($this->listeners[$eventName])) {
  51. return array();
  52. }
  53. if (!isset($this->sorted[$eventName])) {
  54. $this->sortListeners($eventName);
  55. }
  56. return $this->sorted[$eventName];
  57. }
  58. foreach ($this->listeners as $eventName => $eventListeners) {
  59. if (!isset($this->sorted[$eventName])) {
  60. $this->sortListeners($eventName);
  61. }
  62. }
  63. return array_filter($this->sorted);
  64. }
  65. /**
  66. * Gets the listener priority for a specific event.
  67. *
  68. * Returns null if the event or the listener does not exist.
  69. *
  70. * @param string $eventName The name of the event
  71. * @param callable $listener The listener
  72. *
  73. * @return int|null The event listener priority
  74. */
  75. public function getListenerPriority($eventName, $listener)
  76. {
  77. if (!isset($this->listeners[$eventName])) {
  78. return;
  79. }
  80. foreach ($this->listeners[$eventName] as $priority => $listeners) {
  81. if (false !== \in_array($listener, $listeners, true)) {
  82. return $priority;
  83. }
  84. }
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function hasListeners($eventName = null)
  90. {
  91. return (bool) $this->getListeners($eventName);
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function addListener($eventName, $listener, $priority = 0)
  97. {
  98. $this->listeners[$eventName][$priority][] = $listener;
  99. unset($this->sorted[$eventName]);
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function removeListener($eventName, $listener)
  105. {
  106. if (!isset($this->listeners[$eventName])) {
  107. return;
  108. }
  109. foreach ($this->listeners[$eventName] as $priority => $listeners) {
  110. if (false !== ($key = array_search($listener, $listeners, true))) {
  111. unset($this->listeners[$eventName][$priority][$key], $this->sorted[$eventName]);
  112. }
  113. }
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function addSubscriber(EventSubscriberInterface $subscriber)
  119. {
  120. foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
  121. if (\is_string($params)) {
  122. $this->addListener($eventName, array($subscriber, $params));
  123. } elseif (\is_string($params[0])) {
  124. $this->addListener($eventName, array($subscriber, $params[0]), isset($params[1]) ? $params[1] : 0);
  125. } else {
  126. foreach ($params as $listener) {
  127. $this->addListener($eventName, array($subscriber, $listener[0]), isset($listener[1]) ? $listener[1] : 0);
  128. }
  129. }
  130. }
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function removeSubscriber(EventSubscriberInterface $subscriber)
  136. {
  137. foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
  138. if (\is_array($params) && \is_array($params[0])) {
  139. foreach ($params as $listener) {
  140. $this->removeListener($eventName, array($subscriber, $listener[0]));
  141. }
  142. } else {
  143. $this->removeListener($eventName, array($subscriber, \is_string($params) ? $params : $params[0]));
  144. }
  145. }
  146. }
  147. /**
  148. * Triggers the listeners of an event.
  149. *
  150. * This method can be overridden to add functionality that is executed
  151. * for each listener.
  152. *
  153. * @param callable[] $listeners The event listeners
  154. * @param string $eventName The name of the event to dispatch
  155. * @param Event $event The event object to pass to the event handlers/listeners
  156. */
  157. protected function doDispatch($listeners, $eventName, Event $event)
  158. {
  159. foreach ($listeners as $listener) {
  160. if ($event->isPropagationStopped()) {
  161. break;
  162. }
  163. \call_user_func($listener, $event, $eventName, $this);
  164. }
  165. }
  166. /**
  167. * Sorts the internal list of listeners for the given event by priority.
  168. *
  169. * @param string $eventName The name of the event
  170. */
  171. private function sortListeners($eventName)
  172. {
  173. krsort($this->listeners[$eventName]);
  174. $this->sorted[$eventName] = \call_user_func_array('array_merge', $this->listeners[$eventName]);
  175. }
  176. }