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

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. use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
  14. /**
  15.  * The EventDispatcherInterface is the central point of Symfony's event listener system.
  16.  *
  17.  * Listeners are registered on the manager and events are dispatched through the
  18.  * manager.
  19.  *
  20.  * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  21.  * @author Jonathan Wage <jonwage@gmail.com>
  22.  * @author Roman Borschel <roman@code-factory.org>
  23.  * @author Bernhard Schussek <bschussek@gmail.com>
  24.  * @author Fabien Potencier <fabien@symfony.com>
  25.  * @author Jordi Boggiano <j.boggiano@seld.be>
  26.  * @author Jordan Alliot <jordan.alliot@gmail.com>
  27.  * @author Nicolas Grekas <p@tchwork.com>
  28.  */
  29. class EventDispatcher implements EventDispatcherInterface
  30. {
  31.     private $listeners = [];
  32.     private $sorted = [];
  33.     private $optimized;
  34.     public function __construct()
  35.     {
  36.         if (__CLASS__ === \get_class($this)) {
  37.             $this->optimized = [];
  38.         }
  39.     }
  40.     /**
  41.      * {@inheritdoc}
  42.      *
  43.      * @param string|null $eventName
  44.      */
  45.     public function dispatch($event/*, string $eventName = null*/)
  46.     {
  47.         $eventName < \func_num_args() ? func_get_arg(1) : null;
  48.         if (\is_object($event)) {
  49.             $eventName $eventName ?? \get_class($event);
  50.         } elseif (\is_string($event) && (null === $eventName || $eventName instanceof Event)) {
  51.             @trigger_error(sprintf('Calling the "%s::dispatch()" method with the event name as the first argument is deprecated since Symfony 4.3, pass it as the second argument and provide the event object as the first argument instead.'EventDispatcherInterface::class), E_USER_DEPRECATED);
  52.             $swap $event;
  53.             $event $eventName ?? new Event();
  54.             $eventName $swap;
  55.         } else {
  56.             throw new \TypeError(sprintf('Argument 1 passed to "%s::dispatch()" must be an object, %s given.'EventDispatcherInterface::class, \is_object($event) ? \get_class($event) : \gettype($event)));
  57.         }
  58.         if (null !== $this->optimized && null !== $eventName) {
  59.             $listeners $this->optimized[$eventName] ?? (empty($this->listeners[$eventName]) ? [] : $this->optimizeListeners($eventName));
  60.         } else {
  61.             $listeners $this->getListeners($eventName);
  62.         }
  63.         if ($listeners) {
  64.             $this->callListeners($listeners$eventName$event);
  65.         }
  66.         return $event;
  67.     }
  68.     /**
  69.      * {@inheritdoc}
  70.      */
  71.     public function getListeners($eventName null)
  72.     {
  73.         if (null !== $eventName) {
  74.             if (empty($this->listeners[$eventName])) {
  75.                 return [];
  76.             }
  77.             if (!isset($this->sorted[$eventName])) {
  78.                 $this->sortListeners($eventName);
  79.             }
  80.             return $this->sorted[$eventName];
  81.         }
  82.         foreach ($this->listeners as $eventName => $eventListeners) {
  83.             if (!isset($this->sorted[$eventName])) {
  84.                 $this->sortListeners($eventName);
  85.             }
  86.         }
  87.         return array_filter($this->sorted);
  88.     }
  89.     /**
  90.      * {@inheritdoc}
  91.      */
  92.     public function getListenerPriority($eventName$listener)
  93.     {
  94.         if (empty($this->listeners[$eventName])) {
  95.             return;
  96.         }
  97.         if (\is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure) {
  98.             $listener[0] = $listener[0]();
  99.         }
  100.         foreach ($this->listeners[$eventName] as $priority => &$listeners) {
  101.             foreach ($listeners as &$v) {
  102.                 if ($v !== $listener && \is_array($v) && isset($v[0]) && $v[0] instanceof \Closure) {
  103.                     $v[0] = $v[0]();
  104.                 }
  105.                 if ($v === $listener) {
  106.                     return $priority;
  107.                 }
  108.             }
  109.         }
  110.     }
  111.     /**
  112.      * {@inheritdoc}
  113.      */
  114.     public function hasListeners($eventName null)
  115.     {
  116.         if (null !== $eventName) {
  117.             return !empty($this->listeners[$eventName]);
  118.         }
  119.         foreach ($this->listeners as $eventListeners) {
  120.             if ($eventListeners) {
  121.                 return true;
  122.             }
  123.         }
  124.         return false;
  125.     }
  126.     /**
  127.      * {@inheritdoc}
  128.      */
  129.     public function addListener($eventName$listener$priority 0)
  130.     {
  131.         $this->listeners[$eventName][$priority][] = $listener;
  132.         unset($this->sorted[$eventName], $this->optimized[$eventName]);
  133.     }
  134.     /**
  135.      * {@inheritdoc}
  136.      */
  137.     public function removeListener($eventName$listener)
  138.     {
  139.         if (empty($this->listeners[$eventName])) {
  140.             return;
  141.         }
  142.         if (\is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure) {
  143.             $listener[0] = $listener[0]();
  144.         }
  145.         foreach ($this->listeners[$eventName] as $priority => &$listeners) {
  146.             foreach ($listeners as $k => &$v) {
  147.                 if ($v !== $listener && \is_array($v) && isset($v[0]) && $v[0] instanceof \Closure) {
  148.                     $v[0] = $v[0]();
  149.                 }
  150.                 if ($v === $listener) {
  151.                     unset($listeners[$k], $this->sorted[$eventName], $this->optimized[$eventName]);
  152.                 }
  153.             }
  154.             if (!$listeners) {
  155.                 unset($this->listeners[$eventName][$priority]);
  156.             }
  157.         }
  158.     }
  159.     /**
  160.      * {@inheritdoc}
  161.      */
  162.     public function addSubscriber(EventSubscriberInterface $subscriber)
  163.     {
  164.         foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
  165.             if (\is_string($params)) {
  166.                 $this->addListener($eventName, [$subscriber$params]);
  167.             } elseif (\is_string($params[0])) {
  168.                 $this->addListener($eventName, [$subscriber$params[0]], isset($params[1]) ? $params[1] : 0);
  169.             } else {
  170.                 foreach ($params as $listener) {
  171.                     $this->addListener($eventName, [$subscriber$listener[0]], isset($listener[1]) ? $listener[1] : 0);
  172.                 }
  173.             }
  174.         }
  175.     }
  176.     /**
  177.      * {@inheritdoc}
  178.      */
  179.     public function removeSubscriber(EventSubscriberInterface $subscriber)
  180.     {
  181.         foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
  182.             if (\is_array($params) && \is_array($params[0])) {
  183.                 foreach ($params as $listener) {
  184.                     $this->removeListener($eventName, [$subscriber$listener[0]]);
  185.                 }
  186.             } else {
  187.                 $this->removeListener($eventName, [$subscriber, \is_string($params) ? $params $params[0]]);
  188.             }
  189.         }
  190.     }
  191.     /**
  192.      * Triggers the listeners of an event.
  193.      *
  194.      * This method can be overridden to add functionality that is executed
  195.      * for each listener.
  196.      *
  197.      * @param callable[] $listeners The event listeners
  198.      * @param string     $eventName The name of the event to dispatch
  199.      * @param object     $event     The event object to pass to the event handlers/listeners
  200.      */
  201.     protected function callListeners(iterable $listenersstring $eventName$event)
  202.     {
  203.         if ($event instanceof Event) {
  204.             $this->doDispatch($listeners$eventName$event);
  205.             return;
  206.         }
  207.         $stoppable $event instanceof ContractsEvent || $event instanceof StoppableEventInterface;
  208.         foreach ($listeners as $listener) {
  209.             if ($stoppable && $event->isPropagationStopped()) {
  210.                 break;
  211.             }
  212.             // @deprecated: the ternary operator is part of a BC layer and should be removed in 5.0
  213.             $listener($listener instanceof WrappedListener ? new LegacyEventProxy($event) : $event$eventName$this);
  214.         }
  215.     }
  216.     /**
  217.      * @deprecated since Symfony 4.3, use callListeners() instead
  218.      */
  219.     protected function doDispatch($listeners$eventNameEvent $event)
  220.     {
  221.         foreach ($listeners as $listener) {
  222.             if ($event->isPropagationStopped()) {
  223.                 break;
  224.             }
  225.             $listener($event$eventName$this);
  226.         }
  227.     }
  228.     /**
  229.      * Sorts the internal list of listeners for the given event by priority.
  230.      */
  231.     private function sortListeners(string $eventName)
  232.     {
  233.         krsort($this->listeners[$eventName]);
  234.         $this->sorted[$eventName] = [];
  235.         foreach ($this->listeners[$eventName] as &$listeners) {
  236.             foreach ($listeners as $k => $listener) {
  237.                 if (\is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure) {
  238.                     $listener[0] = $listener[0]();
  239.                 }
  240.                 $this->sorted[$eventName][] = $listener;
  241.             }
  242.         }
  243.     }
  244.     /**
  245.      * Optimizes the internal list of listeners for the given event by priority.
  246.      */
  247.     private function optimizeListeners(string $eventName): array
  248.     {
  249.         krsort($this->listeners[$eventName]);
  250.         $this->optimized[$eventName] = [];
  251.         foreach ($this->listeners[$eventName] as &$listeners) {
  252.             foreach ($listeners as &$listener) {
  253.                 $closure = &$this->optimized[$eventName][];
  254.                 if (\is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure) {
  255.                     $closure = static function (...$args) use (&$listener, &$closure) {
  256.                         if ($listener[0] instanceof \Closure) {
  257.                             $listener[0] = $listener[0]();
  258.                         }
  259.                         ($closure = \Closure::fromCallable($listener))(...$args);
  260.                     };
  261.                 } else {
  262.                     $closure $listener instanceof \Closure || $listener instanceof WrappedListener $listener : \Closure::fromCallable($listener);
  263.                 }
  264.             }
  265.         }
  266.         return $this->optimized[$eventName];
  267.     }
  268. }