EventDispatcher.php 5.2 KB

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