EventDispatcher.php 8.2 KB

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