[ Index ] |
PHP Cross Reference of phpBB-3.3.14-deutsch |
[Summary view] [Print] [Text view]
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 PHPUnit\Framework\MockObject\MockObject; 15 use Symfony\Component\DependencyInjection\ContainerInterface; 16 17 /** 18 * Lazily loads listeners and subscribers from the dependency injection 19 * container. 20 * 21 * @author Fabien Potencier <fabien@symfony.com> 22 * @author Bernhard Schussek <bschussek@gmail.com> 23 * @author Jordan Alliot <jordan.alliot@gmail.com> 24 * 25 * @deprecated since 3.3, to be removed in 4.0. Use EventDispatcher with closure factories instead. 26 */ 27 class ContainerAwareEventDispatcher extends EventDispatcher 28 { 29 private $container; 30 31 /** 32 * The service IDs of the event listeners and subscribers. 33 */ 34 private $listenerIds = []; 35 36 /** 37 * The services registered as listeners. 38 */ 39 private $listeners = []; 40 41 public function __construct(ContainerInterface $container) 42 { 43 $this->container = $container; 44 45 $class = static::class; 46 if ($this instanceof \PHPUnit_Framework_MockObject_MockObject || $this instanceof MockObject || $this instanceof \Prophecy\Doubler\DoubleInterface) { 47 $class = get_parent_class($class); 48 } 49 if (__CLASS__ !== $class) { 50 @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); 51 } 52 } 53 54 /** 55 * Adds a service as event listener. 56 * 57 * @param string $eventName Event for which the listener is added 58 * @param array $callback The service ID of the listener service & the method 59 * name that has to be called 60 * @param int $priority The higher this value, the earlier an event listener 61 * will be triggered in the chain. 62 * Defaults to 0. 63 * 64 * @throws \InvalidArgumentException 65 */ 66 public function addListenerService($eventName, $callback, $priority = 0) 67 { 68 @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); 69 70 if (!\is_array($callback) || 2 !== \count($callback)) { 71 throw new \InvalidArgumentException('Expected an ["service", "method"] argument.'); 72 } 73 74 $this->listenerIds[$eventName][] = [$callback[0], $callback[1], $priority]; 75 } 76 77 public function removeListener($eventName, $listener) 78 { 79 $this->lazyLoad($eventName); 80 81 if (isset($this->listenerIds[$eventName])) { 82 foreach ($this->listenerIds[$eventName] as $i => list($serviceId, $method)) { 83 $key = $serviceId.'.'.$method; 84 if (isset($this->listeners[$eventName][$key]) && $listener === [$this->listeners[$eventName][$key], $method]) { 85 unset($this->listeners[$eventName][$key]); 86 if (empty($this->listeners[$eventName])) { 87 unset($this->listeners[$eventName]); 88 } 89 unset($this->listenerIds[$eventName][$i]); 90 if (empty($this->listenerIds[$eventName])) { 91 unset($this->listenerIds[$eventName]); 92 } 93 } 94 } 95 } 96 97 parent::removeListener($eventName, $listener); 98 } 99 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 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Mon Nov 25 19:05:08 2024 | Cross-referenced by PHPXref 0.7.1 |