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

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