EventDispatcher.php 4.8KB

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