EventDispatcher.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 = [];
  29. private $sorted = [];
  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 [];
  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 null;
  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. return null;
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function hasListeners($eventName = null)
  92. {
  93. if (null !== $eventName) {
  94. return !empty($this->listeners[$eventName]);
  95. }
  96. foreach ($this->listeners as $eventListeners) {
  97. if ($eventListeners) {
  98. return true;
  99. }
  100. }
  101. return false;
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function addListener($eventName, $listener, $priority = 0)
  107. {
  108. $this->listeners[$eventName][$priority][] = $listener;
  109. unset($this->sorted[$eventName]);
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function removeListener($eventName, $listener)
  115. {
  116. if (empty($this->listeners[$eventName])) {
  117. return;
  118. }
  119. if (\is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure) {
  120. $listener[0] = $listener[0]();
  121. }
  122. foreach ($this->listeners[$eventName] as $priority => $listeners) {
  123. foreach ($listeners as $k => $v) {
  124. if ($v !== $listener && \is_array($v) && isset($v[0]) && $v[0] instanceof \Closure) {
  125. $v[0] = $v[0]();
  126. }
  127. if ($v === $listener) {
  128. unset($listeners[$k], $this->sorted[$eventName]);
  129. } else {
  130. $listeners[$k] = $v;
  131. }
  132. }
  133. if ($listeners) {
  134. $this->listeners[$eventName][$priority] = $listeners;
  135. } else {
  136. unset($this->listeners[$eventName][$priority]);
  137. }
  138. }
  139. }
  140. /**
  141. * {@inheritdoc}
  142. */
  143. public function addSubscriber(EventSubscriberInterface $subscriber)
  144. {
  145. foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
  146. if (\is_string($params)) {
  147. $this->addListener($eventName, [$subscriber, $params]);
  148. } elseif (\is_string($params[0])) {
  149. $this->addListener($eventName, [$subscriber, $params[0]], isset($params[1]) ? $params[1] : 0);
  150. } else {
  151. foreach ($params as $listener) {
  152. $this->addListener($eventName, [$subscriber, $listener[0]], isset($listener[1]) ? $listener[1] : 0);
  153. }
  154. }
  155. }
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. public function removeSubscriber(EventSubscriberInterface $subscriber)
  161. {
  162. foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
  163. if (\is_array($params) && \is_array($params[0])) {
  164. foreach ($params as $listener) {
  165. $this->removeListener($eventName, [$subscriber, $listener[0]]);
  166. }
  167. } else {
  168. $this->removeListener($eventName, [$subscriber, \is_string($params) ? $params : $params[0]]);
  169. }
  170. }
  171. }
  172. /**
  173. * Triggers the listeners of an event.
  174. *
  175. * This method can be overridden to add functionality that is executed
  176. * for each listener.
  177. *
  178. * @param callable[] $listeners The event listeners
  179. * @param string $eventName The name of the event to dispatch
  180. * @param Event $event The event object to pass to the event handlers/listeners
  181. */
  182. protected function doDispatch($listeners, $eventName, Event $event)
  183. {
  184. foreach ($listeners as $listener) {
  185. if ($event->isPropagationStopped()) {
  186. break;
  187. }
  188. \call_user_func($listener, $event, $eventName, $this);
  189. }
  190. }
  191. /**
  192. * Sorts the internal list of listeners for the given event by priority.
  193. *
  194. * @param string $eventName The name of the event
  195. */
  196. private function sortListeners($eventName)
  197. {
  198. krsort($this->listeners[$eventName]);
  199. $this->sorted[$eventName] = [];
  200. foreach ($this->listeners[$eventName] as $priority => $listeners) {
  201. foreach ($listeners as $k => $listener) {
  202. if (\is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure) {
  203. $listener[0] = $listener[0]();
  204. $this->listeners[$eventName][$priority][$k] = $listener;
  205. }
  206. $this->sorted[$eventName][] = $listener;
  207. }
  208. }
  209. }
  210. }