[ Index ]

PHP Cross Reference of phpBB-3.3.2-deutsch

title

Body

[close]

/vendor/symfony/event-dispatcher/ -> EventDispatcher.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\EventDispatcher;
  13  
  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  
  34      /**
  35       * {@inheritdoc}
  36       */
  37      public function dispatch($eventName, Event $event = null)
  38      {
  39          if (null === $event) {
  40              $event = new Event();
  41          }
  42  
  43          if ($listeners = $this->getListeners($eventName)) {
  44              $this->doDispatch($listeners, $eventName, $event);
  45          }
  46  
  47          return $event;
  48      }
  49  
  50      /**
  51       * {@inheritdoc}
  52       */
  53      public function getListeners($eventName = null)
  54      {
  55          if (null !== $eventName) {
  56              if (empty($this->listeners[$eventName])) {
  57                  return [];
  58              }
  59  
  60              if (!isset($this->sorted[$eventName])) {
  61                  $this->sortListeners($eventName);
  62              }
  63  
  64              return $this->sorted[$eventName];
  65          }
  66  
  67          foreach ($this->listeners as $eventName => $eventListeners) {
  68              if (!isset($this->sorted[$eventName])) {
  69                  $this->sortListeners($eventName);
  70              }
  71          }
  72  
  73          return array_filter($this->sorted);
  74      }
  75  
  76      /**
  77       * {@inheritdoc}
  78       */
  79      public function getListenerPriority($eventName, $listener)
  80      {
  81          if (empty($this->listeners[$eventName])) {
  82              return null;
  83          }
  84  
  85          if (\is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure) {
  86              $listener[0] = $listener[0]();
  87          }
  88  
  89          foreach ($this->listeners[$eventName] as $priority => $listeners) {
  90              foreach ($listeners as $k => $v) {
  91                  if ($v !== $listener && \is_array($v) && isset($v[0]) && $v[0] instanceof \Closure) {
  92                      $v[0] = $v[0]();
  93                      $this->listeners[$eventName][$priority][$k] = $v;
  94                  }
  95                  if ($v === $listener) {
  96                      return $priority;
  97                  }
  98              }
  99          }
 100  
 101          return null;
 102      }
 103  
 104      /**
 105       * {@inheritdoc}
 106       */
 107      public function hasListeners($eventName = null)
 108      {
 109          if (null !== $eventName) {
 110              return !empty($this->listeners[$eventName]);
 111          }
 112  
 113          foreach ($this->listeners as $eventListeners) {
 114              if ($eventListeners) {
 115                  return true;
 116              }
 117          }
 118  
 119          return false;
 120      }
 121  
 122      /**
 123       * {@inheritdoc}
 124       */
 125      public function addListener($eventName, $listener, $priority = 0)
 126      {
 127          $this->listeners[$eventName][$priority][] = $listener;
 128          unset($this->sorted[$eventName]);
 129      }
 130  
 131      /**
 132       * {@inheritdoc}
 133       */
 134      public function removeListener($eventName, $listener)
 135      {
 136          if (empty($this->listeners[$eventName])) {
 137              return;
 138          }
 139  
 140          if (\is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure) {
 141              $listener[0] = $listener[0]();
 142          }
 143  
 144          foreach ($this->listeners[$eventName] as $priority => $listeners) {
 145              foreach ($listeners as $k => $v) {
 146                  if ($v !== $listener && \is_array($v) && isset($v[0]) && $v[0] instanceof \Closure) {
 147                      $v[0] = $v[0]();
 148                  }
 149                  if ($v === $listener) {
 150                      unset($listeners[$k], $this->sorted[$eventName]);
 151                  } else {
 152                      $listeners[$k] = $v;
 153                  }
 154              }
 155  
 156              if ($listeners) {
 157                  $this->listeners[$eventName][$priority] = $listeners;
 158              } else {
 159                  unset($this->listeners[$eventName][$priority]);
 160              }
 161          }
 162      }
 163  
 164      /**
 165       * {@inheritdoc}
 166       */
 167      public function addSubscriber(EventSubscriberInterface $subscriber)
 168      {
 169          foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
 170              if (\is_string($params)) {
 171                  $this->addListener($eventName, [$subscriber, $params]);
 172              } elseif (\is_string($params[0])) {
 173                  $this->addListener($eventName, [$subscriber, $params[0]], isset($params[1]) ? $params[1] : 0);
 174              } else {
 175                  foreach ($params as $listener) {
 176                      $this->addListener($eventName, [$subscriber, $listener[0]], isset($listener[1]) ? $listener[1] : 0);
 177                  }
 178              }
 179          }
 180      }
 181  
 182      /**
 183       * {@inheritdoc}
 184       */
 185      public function removeSubscriber(EventSubscriberInterface $subscriber)
 186      {
 187          foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
 188              if (\is_array($params) && \is_array($params[0])) {
 189                  foreach ($params as $listener) {
 190                      $this->removeListener($eventName, [$subscriber, $listener[0]]);
 191                  }
 192              } else {
 193                  $this->removeListener($eventName, [$subscriber, \is_string($params) ? $params : $params[0]]);
 194              }
 195          }
 196      }
 197  
 198      /**
 199       * Triggers the listeners of an event.
 200       *
 201       * This method can be overridden to add functionality that is executed
 202       * for each listener.
 203       *
 204       * @param callable[] $listeners The event listeners
 205       * @param string     $eventName The name of the event to dispatch
 206       * @param Event      $event     The event object to pass to the event handlers/listeners
 207       */
 208      protected function doDispatch($listeners, $eventName, Event $event)
 209      {
 210          foreach ($listeners as $listener) {
 211              if ($event->isPropagationStopped()) {
 212                  break;
 213              }
 214              \call_user_func($listener, $event, $eventName, $this);
 215          }
 216      }
 217  
 218      /**
 219       * Sorts the internal list of listeners for the given event by priority.
 220       *
 221       * @param string $eventName The name of the event
 222       */
 223      private function sortListeners($eventName)
 224      {
 225          krsort($this->listeners[$eventName]);
 226          $this->sorted[$eventName] = [];
 227  
 228          foreach ($this->listeners[$eventName] as $priority => $listeners) {
 229              foreach ($listeners as $k => $listener) {
 230                  if (\is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure) {
 231                      $listener[0] = $listener[0]();
 232                      $this->listeners[$eventName][$priority][$k] = $listener;
 233                  }
 234                  $this->sorted[$eventName][] = $listener;
 235              }
 236          }
 237      }
 238  }


Generated: Wed Nov 11 20:28:18 2020 Cross-referenced by PHPXref 0.7.1