Brak opisu

EventDispatcher.php 5.4KB

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