Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
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. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
ContainerAwareEventDispatcher.php
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 PHPUnit\Framework\MockObject\MockObject;
015 use Symfony\Component\DependencyInjection\ContainerInterface;
016
017 /**
018 * Lazily loads listeners and subscribers from the dependency injection
019 * container.
020 *
021 * @author Fabien Potencier <fabien@symfony.com>
022 * @author Bernhard Schussek <bschussek@gmail.com>
023 * @author Jordan Alliot <jordan.alliot@gmail.com>
024 *
025 * @deprecated since 3.3, to be removed in 4.0. Use EventDispatcher with closure factories instead.
026 */
027 class ContainerAwareEventDispatcher extends EventDispatcher
028 {
029 private $container;
030
031 /**
032 * The service IDs of the event listeners and subscribers.
033 */
034 private $listenerIds = [];
035
036 /**
037 * The services registered as listeners.
038 */
039 private $listeners = [];
040
041 public function __construct(ContainerInterface $container)
042 {
043 $this->container = $container;
044
045 $class = static::class;
046 if ($this instanceof \PHPUnit_Framework_MockObject_MockObject || $this instanceof MockObject || $this instanceof \Prophecy\Doubler\DoubleInterface) {
047 $class = get_parent_class($class);
048 }
049 if (__CLASS__ !== $class) {
050 @trigger_error(sprintf('The %s class is deprecated since Symfony 3.3 and will be removed in 4.0. Use EventDispatcher with closure factories instead.', __CLASS__), \E_USER_DEPRECATED);
051 }
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 @trigger_error(sprintf('The %s class is deprecated since Symfony 3.3 and will be removed in 4.0. Use EventDispatcher with closure factories instead.', __CLASS__), \E_USER_DEPRECATED);
069
070 if (!\is_array($callback) || 2 !== \count($callback)) {
071 throw new \InvalidArgumentException('Expected an ["service", "method"] argument.');
072 }
073
074 $this->listenerIds[$eventName][] = [$callback[0], $callback[1], $priority];
075 }
076
077 public function removeListener($eventName, $listener)
078 {
079 $this->lazyLoad($eventName);
080
081 if (isset($this->listenerIds[$eventName])) {
082 foreach ($this->listenerIds[$eventName] as $i => list($serviceId, $method)) {
083 $key = $serviceId.'.'.$method;
084 if (isset($this->listeners[$eventName][$key]) && $listener === [$this->listeners[$eventName][$key], $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 parent::removeListener($eventName, $listener);
098 }
099
100 /**
101 * {@inheritdoc}
102 */
103 public function hasListeners($eventName = null)
104 {
105 if (null === $eventName) {
106 return $this->listenerIds || $this->listeners || parent::hasListeners();
107 }
108
109 if (isset($this->listenerIds[$eventName])) {
110 return true;
111 }
112
113 return parent::hasListeners($eventName);
114 }
115
116 /**
117 * {@inheritdoc}
118 */
119 public function getListeners($eventName = null)
120 {
121 if (null === $eventName) {
122 foreach ($this->listenerIds as $serviceEventName => $args) {
123 $this->lazyLoad($serviceEventName);
124 }
125 } else {
126 $this->lazyLoad($eventName);
127 }
128
129 return parent::getListeners($eventName);
130 }
131
132 /**
133 * {@inheritdoc}
134 */
135 public function getListenerPriority($eventName, $listener)
136 {
137 $this->lazyLoad($eventName);
138
139 return parent::getListenerPriority($eventName, $listener);
140 }
141
142 /**
143 * Adds a service as event subscriber.
144 *
145 * @param string $serviceId The service ID of the subscriber service
146 * @param string $class The service's class name (which must implement EventSubscriberInterface)
147 */
148 public function addSubscriberService($serviceId, $class)
149 {
150 @trigger_error(sprintf('The %s class is deprecated since Symfony 3.3 and will be removed in 4.0. Use EventDispatcher with closure factories instead.', __CLASS__), \E_USER_DEPRECATED);
151
152 foreach ($class::getSubscribedEvents() as $eventName => $params) {
153 if (\is_string($params)) {
154 $this->listenerIds[$eventName][] = [$serviceId, $params, 0];
155 } elseif (\is_string($params[0])) {
156 $this->listenerIds[$eventName][] = [$serviceId, $params[0], isset($params[1]) ? $params[1] : 0];
157 } else {
158 foreach ($params as $listener) {
159 $this->listenerIds[$eventName][] = [$serviceId, $listener[0], isset($listener[1]) ? $listener[1] : 0];
160 }
161 }
162 }
163 }
164
165 public function getContainer()
166 {
167 @trigger_error('The '.__METHOD__.'() method is deprecated since Symfony 3.3 as its class will be removed in 4.0. Inject the container or the services you need in your listeners/subscribers instead.', \E_USER_DEPRECATED);
168
169 return $this->container;
170 }
171
172 /**
173 * Lazily loads listeners for this event from the dependency injection
174 * container.
175 *
176 * @param string $eventName The name of the event to dispatch. The name of
177 * the event is the name of the method that is
178 * invoked on listeners.
179 */
180 protected function lazyLoad($eventName)
181 {
182 if (isset($this->listenerIds[$eventName])) {
183 foreach ($this->listenerIds[$eventName] as list($serviceId, $method, $priority)) {
184 $listener = $this->container->get($serviceId);
185
186 $key = $serviceId.'.'.$method;
187 if (!isset($this->listeners[$eventName][$key])) {
188 $this->addListener($eventName, [$listener, $method], $priority);
189 } elseif ($this->listeners[$eventName][$key] !== $listener) {
190 parent::removeListener($eventName, [$this->listeners[$eventName][$key], $method]);
191 $this->addListener($eventName, [$listener, $method], $priority);
192 }
193
194 $this->listeners[$eventName][$key] = $listener;
195 }
196 }
197 }
198 }
199