vendor/symfony/event-dispatcher/EventDispatcher.php line 68

Open in your IDE?
  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. use Psr\EventDispatcher\StoppableEventInterface;
  12. use Symfony\Component\EventDispatcher\Debug\WrappedListener;
  13. /**
  14. * The EventDispatcherInterface is the central point of Symfony's event listener system.
  15. *
  16. * Listeners are registered on the manager and events are dispatched through the
  17. * manager.
  18. *
  19. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  20. * @author Jonathan Wage <jonwage@gmail.com>
  21. * @author Roman Borschel <roman@code-factory.org>
  22. * @author Bernhard Schussek <bschussek@gmail.com>
  23. * @author Fabien Potencier <fabien@symfony.com>
  24. * @author Jordi Boggiano <j.boggiano@seld.be>
  25. * @author Jordan Alliot <jordan.alliot@gmail.com>
  26. * @author Nicolas Grekas <p@tchwork.com>
  27. */
  28. class EventDispatcher implements EventDispatcherInterface
  29. {
  30. private $listeners = [];
  31. private $sorted = [];
  32. private $optimized;
  33. public function __construct()
  34. {
  35. if (__CLASS__ === static::class) {
  36. $this->optimized = [];
  37. }
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function dispatch(object $event, ?string $eventName = null): object
  43. {
  44. $eventName = $eventName ?? \get_class($event);
  45. if (null !== $this->optimized) {
  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->callListeners($listeners, $eventName, $event);
  52. }
  53. return $event;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function getListeners(?string $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(string $eventName, $listener)
  80. {
  81. if (empty($this->listeners[$eventName])) {
  82. return null;
  83. }
  84. if (\is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure && 2 >= \count($listener)) {
  85. $listener[0] = $listener[0]();
  86. $listener[1] = $listener[1] ?? '__invoke';
  87. }
  88. foreach ($this->listeners[$eventName] as $priority => &$listeners) {
  89. foreach ($listeners as &$v) {
  90. if ($v !== $listener && \is_array($v) && isset($v[0]) && $v[0] instanceof \Closure && 2 >= \count($v)) {
  91. $v[0] = $v[0]();
  92. $v[1] = $v[1] ?? '__invoke';
  93. }
  94. if ($v === $listener || ($listener instanceof \Closure && $v == $listener)) {
  95. return $priority;
  96. }
  97. }
  98. }
  99. return null;
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function hasListeners(?string $eventName = null)
  105. {
  106. if (null !== $eventName) {
  107. return !empty($this->listeners[$eventName]);
  108. }
  109. foreach ($this->listeners as $eventListeners) {
  110. if ($eventListeners) {
  111. return true;
  112. }
  113. }
  114. return false;
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function addListener(string $eventName, $listener, int $priority = 0)
  120. {
  121. $this->listeners[$eventName][$priority][] = $listener;
  122. unset($this->sorted[$eventName], $this->optimized[$eventName]);
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function removeListener(string $eventName, $listener)
  128. {
  129. if (empty($this->listeners[$eventName])) {
  130. return;
  131. }
  132. if (\is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure && 2 >= \count($listener)) {
  133. $listener[0] = $listener[0]();
  134. $listener[1] = $listener[1] ?? '__invoke';
  135. }
  136. foreach ($this->listeners[$eventName] as $priority => &$listeners) {
  137. foreach ($listeners as $k => &$v) {
  138. if ($v !== $listener && \is_array($v) && isset($v[0]) && $v[0] instanceof \Closure && 2 >= \count($v)) {
  139. $v[0] = $v[0]();
  140. $v[1] = $v[1] ?? '__invoke';
  141. }
  142. if ($v === $listener || ($listener instanceof \Closure && $v == $listener)) {
  143. unset($listeners[$k], $this->sorted[$eventName], $this->optimized[$eventName]);
  144. }
  145. }
  146. if (!$listeners) {
  147. unset($this->listeners[$eventName][$priority]);
  148. }
  149. }
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function addSubscriber(EventSubscriberInterface $subscriber)
  155. {
  156. foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
  157. if (\is_string($params)) {
  158. $this->addListener($eventName, [$subscriber, $params]);
  159. } elseif (\is_string($params[0])) {
  160. $this->addListener($eventName, [$subscriber, $params[0]], $params[1] ?? 0);
  161. } else {
  162. foreach ($params as $listener) {
  163. $this->addListener($eventName, [$subscriber, $listener[0]], $listener[1] ?? 0);
  164. }
  165. }
  166. }
  167. }
  168. /**
  169. * {@inheritdoc}
  170. */
  171. public function removeSubscriber(EventSubscriberInterface $subscriber)
  172. {
  173. foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
  174. if (\is_array($params) && \is_array($params[0])) {
  175. foreach ($params as $listener) {
  176. $this->removeListener($eventName, [$subscriber, $listener[0]]);
  177. }
  178. } else {
  179. $this->removeListener($eventName, [$subscriber, \is_string($params) ? $params : $params[0]]);
  180. }
  181. }
  182. }
  183. /**
  184. * Triggers the listeners of an event.
  185. *
  186. * This method can be overridden to add functionality that is executed
  187. * for each listener.
  188. *
  189. * @param callable[] $listeners The event listeners
  190. * @param string $eventName The name of the event to dispatch
  191. * @param object $event The event object to pass to the event handlers/listeners
  192. */
  193. protected function callListeners(iterable $listeners, string $eventName, object $event)
  194. {
  195. $stoppable = $event instanceof StoppableEventInterface;
  196. foreach ($listeners as $listener) {
  197. if ($stoppable && $event->isPropagationStopped()) {
  198. break;
  199. }
  200. $listener($event, $eventName, $this);
  201. }
  202. }
  203. /**
  204. * Sorts the internal list of listeners for the given event by priority.
  205. */
  206. private function sortListeners(string $eventName)
  207. {
  208. krsort($this->listeners[$eventName]);
  209. $this->sorted[$eventName] = [];
  210. foreach ($this->listeners[$eventName] as &$listeners) {
  211. foreach ($listeners as $k => &$listener) {
  212. if (\is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure && 2 >= \count($listener)) {
  213. $listener[0] = $listener[0]();
  214. $listener[1] = $listener[1] ?? '__invoke';
  215. }
  216. $this->sorted[$eventName][] = $listener;
  217. }
  218. }
  219. }
  220. /**
  221. * Optimizes the internal list of listeners for the given event by priority.
  222. */
  223. private function optimizeListeners(string $eventName): array
  224. {
  225. krsort($this->listeners[$eventName]);
  226. $this->optimized[$eventName] = [];
  227. foreach ($this->listeners[$eventName] as &$listeners) {
  228. foreach ($listeners as &$listener) {
  229. $closure = &$this->optimized[$eventName][];
  230. if (\is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure && 2 >= \count($listener)) {
  231. $closure = static function (...$args) use (&$listener, &$closure) {
  232. if ($listener[0] instanceof \Closure) {
  233. $listener[0] = $listener[0]();
  234. $listener[1] = $listener[1] ?? '__invoke';
  235. }
  236. ($closure = \Closure::fromCallable($listener))(...$args);
  237. };
  238. } else {
  239. $closure = $listener instanceof \Closure || $listener instanceof WrappedListener ? $listener : \Closure::fromCallable($listener);
  240. }
  241. }
  242. }
  243. return $this->optimized[$eventName];
  244. }
  245. }