[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/event-dispatcher/ -> ContainerAwareEventDispatcher.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  use Symfony\Component\DependencyInjection\ContainerInterface;
  15  
  16  /**
  17   * Lazily loads listeners and subscribers from the dependency injection
  18   * container.
  19   *
  20   * @author Fabien Potencier <fabien@symfony.com>
  21   * @author Bernhard Schussek <bschussek@gmail.com>
  22   * @author Jordan Alliot <jordan.alliot@gmail.com>
  23   */
  24  class ContainerAwareEventDispatcher extends EventDispatcher
  25  {
  26      private $container;
  27  
  28      /**
  29       * The service IDs of the event listeners and subscribers.
  30       */
  31      private $listenerIds = array();
  32  
  33      /**
  34       * The services registered as listeners.
  35       */
  36      private $listeners = array();
  37  
  38      public function __construct(ContainerInterface $container)
  39      {
  40          $this->container = $container;
  41      }
  42  
  43      /**
  44       * Adds a service as event listener.
  45       *
  46       * @param string $eventName Event for which the listener is added
  47       * @param array  $callback  The service ID of the listener service & the method
  48       *                          name that has to be called
  49       * @param int    $priority  The higher this value, the earlier an event listener
  50       *                          will be triggered in the chain.
  51       *                          Defaults to 0.
  52       *
  53       * @throws \InvalidArgumentException
  54       */
  55      public function addListenerService($eventName, $callback, $priority = 0)
  56      {
  57          if (!\is_array($callback) || 2 !== \count($callback)) {
  58              throw new \InvalidArgumentException('Expected an array("service", "method") argument');
  59          }
  60  
  61          $this->listenerIds[$eventName][] = array($callback[0], $callback[1], $priority);
  62      }
  63  
  64      public function removeListener($eventName, $listener)
  65      {
  66          $this->lazyLoad($eventName);
  67  
  68          if (isset($this->listenerIds[$eventName])) {
  69              foreach ($this->listenerIds[$eventName] as $i => $args) {
  70                  list($serviceId, $method) = $args;
  71                  $key = $serviceId.'.'.$method;
  72                  if (isset($this->listeners[$eventName][$key]) && $listener === array($this->listeners[$eventName][$key], $method)) {
  73                      unset($this->listeners[$eventName][$key]);
  74                      if (empty($this->listeners[$eventName])) {
  75                          unset($this->listeners[$eventName]);
  76                      }
  77                      unset($this->listenerIds[$eventName][$i]);
  78                      if (empty($this->listenerIds[$eventName])) {
  79                          unset($this->listenerIds[$eventName]);
  80                      }
  81                  }
  82              }
  83          }
  84  
  85          parent::removeListener($eventName, $listener);
  86      }
  87  
  88      /**
  89       * {@inheritdoc}
  90       */
  91      public function hasListeners($eventName = null)
  92      {
  93          if (null === $eventName) {
  94              return $this->listenerIds || $this->listeners || parent::hasListeners();
  95          }
  96  
  97          if (isset($this->listenerIds[$eventName])) {
  98              return true;
  99          }
 100  
 101          return parent::hasListeners($eventName);
 102      }
 103  
 104      /**
 105       * {@inheritdoc}
 106       */
 107      public function getListeners($eventName = null)
 108      {
 109          if (null === $eventName) {
 110              foreach ($this->listenerIds as $serviceEventName => $args) {
 111                  $this->lazyLoad($serviceEventName);
 112              }
 113          } else {
 114              $this->lazyLoad($eventName);
 115          }
 116  
 117          return parent::getListeners($eventName);
 118      }
 119  
 120      /**
 121       * {@inheritdoc}
 122       */
 123      public function getListenerPriority($eventName, $listener)
 124      {
 125          $this->lazyLoad($eventName);
 126  
 127          return parent::getListenerPriority($eventName, $listener);
 128      }
 129  
 130      /**
 131       * Adds a service as event subscriber.
 132       *
 133       * @param string $serviceId The service ID of the subscriber service
 134       * @param string $class     The service's class name (which must implement EventSubscriberInterface)
 135       */
 136      public function addSubscriberService($serviceId, $class)
 137      {
 138          foreach ($class::getSubscribedEvents() as $eventName => $params) {
 139              if (\is_string($params)) {
 140                  $this->listenerIds[$eventName][] = array($serviceId, $params, 0);
 141              } elseif (\is_string($params[0])) {
 142                  $this->listenerIds[$eventName][] = array($serviceId, $params[0], isset($params[1]) ? $params[1] : 0);
 143              } else {
 144                  foreach ($params as $listener) {
 145                      $this->listenerIds[$eventName][] = array($serviceId, $listener[0], isset($listener[1]) ? $listener[1] : 0);
 146                  }
 147              }
 148          }
 149      }
 150  
 151      public function getContainer()
 152      {
 153          return $this->container;
 154      }
 155  
 156      /**
 157       * Lazily loads listeners for this event from the dependency injection
 158       * container.
 159       *
 160       * @param string $eventName The name of the event to dispatch. The name of
 161       *                          the event is the name of the method that is
 162       *                          invoked on listeners.
 163       */
 164      protected function lazyLoad($eventName)
 165      {
 166          if (isset($this->listenerIds[$eventName])) {
 167              foreach ($this->listenerIds[$eventName] as $args) {
 168                  list($serviceId, $method, $priority) = $args;
 169                  $listener = $this->container->get($serviceId);
 170  
 171                  $key = $serviceId.'.'.$method;
 172                  if (!isset($this->listeners[$eventName][$key])) {
 173                      $this->addListener($eventName, array($listener, $method), $priority);
 174                  } elseif ($this->listeners[$eventName][$key] !== $listener) {
 175                      parent::removeListener($eventName, array($this->listeners[$eventName][$key], $method));
 176                      $this->addListener($eventName, array($listener, $method), $priority);
 177                  }
 178  
 179                  $this->listeners[$eventName][$key] = $listener;
 180              }
 181          }
 182      }
 183  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1