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

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