Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

ContainerAwareEventDispatcher.php

Zuletzt modifiziert: 09.10.2024, 12:58 - Dateigröße: 6.52 KiB


 001  <?php
 002   
 003  /*
 004   * This file is part of the Symfony package.
 005   *
 006   * (c) Fabien Potencier <fabien@symfony.com>
 007   *
 008   * For the full copyright and license information, please view the LICENSE
 009   * file that was distributed with this source code.
 010   */
 011   
 012  namespace Symfony\Component\EventDispatcher;
 013   
 014  use Symfony\Component\DependencyInjection\ContainerInterface;
 015   
 016  /**
 017   * Lazily loads listeners and subscribers from the dependency injection
 018   * container
 019   *
 020   * @author Fabien Potencier <fabien@symfony.com>
 021   * @author Bernhard Schussek <bschussek@gmail.com>
 022   * @author Jordan Alliot <jordan.alliot@gmail.com>
 023   */
 024  class ContainerAwareEventDispatcher extends EventDispatcher
 025  {
 026      /**
 027       * The container from where services are loaded
 028       * @var ContainerInterface
 029       */
 030      private $container;
 031   
 032      /**
 033       * The service IDs of the event listeners and subscribers
 034       * @var array
 035       */
 036      private $listenerIds = array();
 037   
 038      /**
 039       * The services registered as listeners
 040       * @var array
 041       */
 042      private $listeners = array();
 043   
 044      /**
 045       * Constructor.
 046       *
 047       * @param ContainerInterface $container A ContainerInterface instance
 048       */
 049      public function __construct(ContainerInterface $container)
 050      {
 051          $this->container = $container;
 052      }
 053   
 054      /**
 055       * Adds a service as event listener
 056       *
 057       * @param string $eventName Event for which the listener is added
 058       * @param array  $callback  The service ID of the listener service & the method
 059       *                            name that has to be called
 060       * @param int     $priority The higher this value, the earlier an event listener
 061       *                            will be triggered in the chain.
 062       *                            Defaults to 0.
 063       *
 064       * @throws \InvalidArgumentException
 065       */
 066      public function addListenerService($eventName, $callback, $priority = 0)
 067      {
 068          if (!is_array($callback) || 2 !== count($callback)) {
 069              throw new \InvalidArgumentException('Expected an array("service", "method") argument');
 070          }
 071   
 072          $this->listenerIds[$eventName][] = array($callback[0], $callback[1], $priority);
 073      }
 074   
 075      public function removeListener($eventName, $listener)
 076      {
 077          $this->lazyLoad($eventName);
 078   
 079          if (isset($this->listeners[$eventName])) {
 080              foreach ($this->listeners[$eventName] as $key => $l) {
 081                  foreach ($this->listenerIds[$eventName] as $i => $args) {
 082                      list($serviceId, $method, $priority) = $args;
 083                      if ($key === $serviceId.'.'.$method) {
 084                          if ($listener === array($l, $method)) {
 085                              unset($this->listeners[$eventName][$key]);
 086                              if (empty($this->listeners[$eventName])) {
 087                                  unset($this->listeners[$eventName]);
 088                              }
 089                              unset($this->listenerIds[$eventName][$i]);
 090                              if (empty($this->listenerIds[$eventName])) {
 091                                  unset($this->listenerIds[$eventName]);
 092                              }
 093                          }
 094                      }
 095                  }
 096              }
 097          }
 098   
 099          parent::removeListener($eventName, $listener);
 100      }
 101   
 102      /**
 103       * @see EventDispatcherInterface::hasListeners
 104       */
 105      public function hasListeners($eventName = null)
 106      {
 107          if (null === $eventName) {
 108              return (bool) count($this->listenerIds) || (bool) count($this->listeners);
 109          }
 110   
 111          if (isset($this->listenerIds[$eventName])) {
 112              return true;
 113          }
 114   
 115          return parent::hasListeners($eventName);
 116      }
 117   
 118      /**
 119       * @see EventDispatcherInterface::getListeners
 120       */
 121      public function getListeners($eventName = null)
 122      {
 123          if (null === $eventName) {
 124              foreach (array_keys($this->listenerIds) as $serviceEventName) {
 125                  $this->lazyLoad($serviceEventName);
 126              }
 127          } else {
 128              $this->lazyLoad($eventName);
 129          }
 130   
 131          return parent::getListeners($eventName);
 132      }
 133   
 134      /**
 135       * Adds a service as event subscriber
 136       *
 137       * @param string $serviceId The service ID of the subscriber service
 138       * @param string $class     The service's class name (which must implement EventSubscriberInterface)
 139       */
 140      public function addSubscriberService($serviceId, $class)
 141      {
 142          foreach ($class::getSubscribedEvents() as $eventName => $params) {
 143              if (is_string($params)) {
 144                  $this->listenerIds[$eventName][] = array($serviceId, $params, 0);
 145              } elseif (is_string($params[0])) {
 146                  $this->listenerIds[$eventName][] = array($serviceId, $params[0], isset($params[1]) ? $params[1] : 0);
 147              } else {
 148                  foreach ($params as $listener) {
 149                      $this->listenerIds[$eventName][] = array($serviceId, $listener[0], isset($listener[1]) ? $listener[1] : 0);
 150                  }
 151              }
 152          }
 153      }
 154   
 155      /**
 156       * {@inheritdoc}
 157       *
 158       * Lazily loads listeners for this event from the dependency injection
 159       * container.
 160       *
 161       * @throws \InvalidArgumentException if the service is not defined
 162       */
 163      public function dispatch($eventName, Event $event = null)
 164      {
 165          $this->lazyLoad($eventName);
 166   
 167          return parent::dispatch($eventName, $event);
 168      }
 169   
 170      public function getContainer()
 171      {
 172          return $this->container;
 173      }
 174   
 175      /**
 176       * Lazily loads listeners for this event from the dependency injection
 177       * container.
 178       *
 179       * @param string $eventName The name of the event to dispatch. The name of
 180       *                          the event is the name of the method that is
 181       *                          invoked on listeners.
 182       */
 183      protected function lazyLoad($eventName)
 184      {
 185          if (isset($this->listenerIds[$eventName])) {
 186              foreach ($this->listenerIds[$eventName] as $args) {
 187                  list($serviceId, $method, $priority) = $args;
 188                  $listener = $this->container->get($serviceId);
 189   
 190                  $key = $serviceId.'.'.$method;
 191                  if (!isset($this->listeners[$eventName][$key])) {
 192                      $this->addListener($eventName, array($listener, $method), $priority);
 193                  } elseif ($listener !== $this->listeners[$eventName][$key]) {
 194                      parent::removeListener($eventName, array($this->listeners[$eventName][$key], $method));
 195                      $this->addListener($eventName, array($listener, $method), $priority);
 196                  }
 197   
 198                  $this->listeners[$eventName][$key] = $listener;
 199              }
 200          }
 201      }
 202  }
 203