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 null;
  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.         return null;
  111.     }
  112.     /**
  113.      * {@inheritdoc}
  114.      */
  115.     public function hasListeners($eventName null)
  116.     {
  117.         if (null !== $eventName) {
  118.             return !empty($this->listeners[$eventName]);
  119.         }
  120.         foreach ($this->listeners as $eventListeners) {
  121.             if ($eventListeners) {
  122.                 return true;
  123.             }
  124.         }
  125.         return false;
  126.     }
  127.     /**
  128.      * {@inheritdoc}
  129.      */
  130.     public function addListener($eventName$listener$priority 0)
  131.     {
  132.         $this->listeners[$eventName][$priority][] = $listener;
  133.         unset($this->sorted[$eventName], $this->optimized[$eventName]);
  134.     }
  135.     /**
  136.      * {@inheritdoc}
  137.      */
  138.     public function removeListener($eventName$listener)
  139.     {
  140.         if (empty($this->listeners[$eventName])) {
  141.             return;
  142.         }
  143.         if (\is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure) {
  144.             $listener[0] = $listener[0]();
  145.         }
  146.         foreach ($this->listeners[$eventName] as $priority => &$listeners) {
  147.             foreach ($listeners as $k => &$v) {
  148.                 if ($v !== $listener && \is_array($v) && isset($v[0]) && $v[0] instanceof \Closure) {
  149.                     $v[0] = $v[0]();
  150.                 }
  151.                 if ($v === $listener) {
  152.                     unset($listeners[$k], $this->sorted[$eventName], $this->optimized[$eventName]);
  153.                 }
  154.             }
  155.             if (!$listeners) {
  156.                 unset($this->listeners[$eventName][$priority]);
  157.             }
  158.         }
  159.     }
  160.     /**
  161.      * {@inheritdoc}
  162.      */
  163.     public function addSubscriber(EventSubscriberInterface $subscriber)
  164.     {
  165.         foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
  166.             if (\is_string($params)) {
  167.                 $this->addListener($eventName, [$subscriber$params]);
  168.             } elseif (\is_string($params[0])) {
  169.                 $this->addListener($eventName, [$subscriber$params[0]], isset($params[1]) ? $params[1] : 0);
  170.             } else {
  171.                 foreach ($params as $listener) {
  172.                     $this->addListener($eventName, [$subscriber$listener[0]], isset($listener[1]) ? $listener[1] : 0);
  173.                 }
  174.             }
  175.         }
  176.     }
  177.     /**
  178.      * {@inheritdoc}
  179.      */
  180.     public function removeSubscriber(EventSubscriberInterface $subscriber)
  181.     {
  182.         foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
  183.             if (\is_array($params) && \is_array($params[0])) {
  184.                 foreach ($params as $listener) {
  185.                     $this->removeListener($eventName, [$subscriber$listener[0]]);
  186.                 }
  187.             } else {
  188.                 $this->removeListener($eventName, [$subscriber\is_string($params) ? $params $params[0]]);
  189.             }
  190.         }
  191.     }
  192.     /**
  193.      * Triggers the listeners of an event.
  194.      *
  195.      * This method can be overridden to add functionality that is executed
  196.      * for each listener.
  197.      *
  198.      * @param callable[] $listeners The event listeners
  199.      * @param string     $eventName The name of the event to dispatch
  200.      * @param object     $event     The event object to pass to the event handlers/listeners
  201.      */
  202.     protected function callListeners(iterable $listenersstring $eventName$event)
  203.     {
  204.         if ($event instanceof Event) {
  205.             $this->doDispatch($listeners$eventName$event);
  206.             return;
  207.         }
  208.         $stoppable $event instanceof ContractsEvent || $event instanceof StoppableEventInterface;
  209.         foreach ($listeners as $listener) {
  210.             if ($stoppable && $event->isPropagationStopped()) {
  211.                 break;
  212.             }
  213.             // @deprecated: the ternary operator is part of a BC layer and should be removed in 5.0
  214.             $listener($listener instanceof WrappedListener ? new LegacyEventProxy($event) : $event$eventName$this);
  215.         }
  216.     }
  217.     /**
  218.      * @deprecated since Symfony 4.3, use callListeners() instead
  219.      */
  220.     protected function doDispatch($listeners$eventNameEvent $event)
  221.     {
  222.         foreach ($listeners as $listener) {
  223.             if ($event->isPropagationStopped()) {
  224.                 break;
  225.             }
  226.             $listener($event$eventName$this);
  227.         }
  228.     }
  229.     /**
  230.      * Sorts the internal list of listeners for the given event by priority.
  231.      */
  232.     private function sortListeners(string $eventName)
  233.     {
  234.         krsort($this->listeners[$eventName]);
  235.         $this->sorted[$eventName] = [];
  236.         foreach ($this->listeners[$eventName] as &$listeners) {
  237.             foreach ($listeners as $k => $listener) {
  238.                 if (\is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure) {
  239.                     $listener[0] = $listener[0]();
  240.                 }
  241.                 $this->sorted[$eventName][] = $listener;
  242.             }
  243.         }
  244.     }
  245.     /**
  246.      * Optimizes the internal list of listeners for the given event by priority.
  247.      */
  248.     private function optimizeListeners(string $eventName): array
  249.     {
  250.         krsort($this->listeners[$eventName]);
  251.         $this->optimized[$eventName] = [];
  252.         foreach ($this->listeners[$eventName] as &$listeners) {
  253.             foreach ($listeners as &$listener) {
  254.                 $closure = &$this->optimized[$eventName][];
  255.                 if (\is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure) {
  256.                     $closure = static function (...$args) use (&$listener, &$closure) {
  257.                         if ($listener[0] instanceof \Closure) {
  258.                             $listener[0] = $listener[0]();
  259.                         }
  260.                         ($closure \Closure::fromCallable($listener))(...$args);
  261.                     };
  262.                 } else {
  263.                     $closure $listener instanceof \Closure || $listener instanceof WrappedListener $listener \Closure::fromCallable($listener);
  264.                 }
  265.             }
  266.         }
  267.         return $this->optimized[$eventName];
  268.     }
  269. }