EventDispatcher.php 7.0 KB

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