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