Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

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:54 - Dateigröße: 6.20 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       *
029       * @var ContainerInterface
030       */
031      private $container;
032   
033      /**
034       * The service IDs of the event listeners and subscribers.
035       *
036       * @var array
037       */
038      private $listenerIds = array();
039   
040      /**
041       * The services registered as listeners.
042       *
043       * @var array
044       */
045      private $listeners = array();
046   
047      /**
048       * Constructor.
049       *
050       * @param ContainerInterface $container A ContainerInterface instance
051       */
052      public function __construct(ContainerInterface $container)
053      {
054          $this->container = $container;
055      }
056   
057      /**
058       * Adds a service as event listener.
059       *
060       * @param string $eventName Event for which the listener is added
061       * @param array  $callback  The service ID of the listener service & the method
062       *                          name that has to be called
063       * @param int    $priority  The higher this value, the earlier an event listener
064       *                          will be triggered in the chain.
065       *                          Defaults to 0.
066       *
067       * @throws \InvalidArgumentException
068       */
069      public function addListenerService($eventName, $callback, $priority = 0)
070      {
071          if (!is_array($callback) || 2 !== count($callback)) {
072              throw new \InvalidArgumentException('Expected an array("service", "method") argument');
073          }
074   
075          $this->listenerIds[$eventName][] = array($callback[0], $callback[1], $priority);
076      }
077   
078      public function removeListener($eventName, $listener)
079      {
080          $this->lazyLoad($eventName);
081   
082          if (isset($this->listenerIds[$eventName])) {
083              foreach ($this->listenerIds[$eventName] as $i => $args) {
084                  list($serviceId, $method, $priority) = $args;
085                  $key = $serviceId.'.'.$method;
086                  if (isset($this->listeners[$eventName][$key]) && $listener === array($this->listeners[$eventName][$key], $method)) {
087                      unset($this->listeners[$eventName][$key]);
088                      if (empty($this->listeners[$eventName])) {
089                          unset($this->listeners[$eventName]);
090                      }
091                      unset($this->listenerIds[$eventName][$i]);
092                      if (empty($this->listenerIds[$eventName])) {
093                          unset($this->listenerIds[$eventName]);
094                      }
095                  }
096              }
097          }
098   
099          parent::removeListener($eventName, $listener);
100      }
101   
102      /**
103       * {@inheritdoc}
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       * {@inheritdoc}
120       */
121      public function getListeners($eventName = null)
122      {
123          if (null === $eventName) {
124              foreach ($this->listenerIds as $serviceEventName => $args) {
125                  $this->lazyLoad($serviceEventName);
126              }
127          } else {
128              $this->lazyLoad($eventName);
129          }
130   
131          return parent::getListeners($eventName);
132      }
133   
134      /**
135       * {@inheritdoc}
136       */
137      public function getListenerPriority($eventName, $listener)
138      {
139          $this->lazyLoad($eventName);
140   
141          return parent::getListenerPriority($eventName, $listener);
142      }
143   
144      /**
145       * Adds a service as event subscriber.
146       *
147       * @param string $serviceId The service ID of the subscriber service
148       * @param string $class     The service's class name (which must implement EventSubscriberInterface)
149       */
150      public function addSubscriberService($serviceId, $class)
151      {
152          foreach ($class::getSubscribedEvents() as $eventName => $params) {
153              if (is_string($params)) {
154                  $this->listenerIds[$eventName][] = array($serviceId, $params, 0);
155              } elseif (is_string($params[0])) {
156                  $this->listenerIds[$eventName][] = array($serviceId, $params[0], isset($params[1]) ? $params[1] : 0);
157              } else {
158                  foreach ($params as $listener) {
159                      $this->listenerIds[$eventName][] = array($serviceId, $listener[0], isset($listener[1]) ? $listener[1] : 0);
160                  }
161              }
162          }
163      }
164   
165      public function getContainer()
166      {
167          return $this->container;
168      }
169   
170      /**
171       * Lazily loads listeners for this event from the dependency injection
172       * container.
173       *
174       * @param string $eventName The name of the event to dispatch. The name of
175       *                          the event is the name of the method that is
176       *                          invoked on listeners.
177       */
178      protected function lazyLoad($eventName)
179      {
180          if (isset($this->listenerIds[$eventName])) {
181              foreach ($this->listenerIds[$eventName] as $args) {
182                  list($serviceId, $method, $priority) = $args;
183                  $listener = $this->container->get($serviceId);
184   
185                  $key = $serviceId.'.'.$method;
186                  if (!isset($this->listeners[$eventName][$key])) {
187                      $this->addListener($eventName, array($listener, $method), $priority);
188                  } elseif ($listener !== $this->listeners[$eventName][$key]) {
189                      parent::removeListener($eventName, array($this->listeners[$eventName][$key], $method));
190                      $this->addListener($eventName, array($listener, $method), $priority);
191                  }
192   
193                  $this->listeners[$eventName][$key] = $listener;
194              }
195          }
196      }
197  }
198