4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\EventDispatcher
;
14 use Symfony\Component\DependencyInjection\ContainerInterface
;
17 * Lazily loads listeners and subscribers from the dependency injection
20 * @author Fabien Potencier <fabien@symfony.com>
21 * @author Bernhard Schussek <bschussek@gmail.com>
22 * @author Jordan Alliot <jordan.alliot@gmail.com>
24 class ContainerAwareEventDispatcher
extends EventDispatcher
27 * The container from where services are loaded
28 * @var ContainerInterface
33 * The service IDs of the event listeners and subscribers
36 private $listenerIds = array();
39 * The services registered as listeners
42 private $listeners = array();
47 * @param ContainerInterface $container A ContainerInterface instance
49 public function __construct(ContainerInterface
$container)
51 $this->container
= $container;
55 * Adds a service as event listener
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 integer $priority The higher this value, the earlier an event listener
61 * will be triggered in the chain.
64 * @throws \InvalidArgumentException
66 public function addListenerService($eventName, $callback, $priority = 0)
68 if (!is_array($callback) || 2 !== count($callback)) {
69 throw new \
InvalidArgumentException('Expected an array("service", "method") argument');
72 $this->listenerIds
[$eventName][] = array($callback[0], $callback[1], $priority);
75 public function removeListener($eventName, $listener)
77 $this->lazyLoad($eventName);
79 if (isset($this->listeners
[$eventName])) {
80 foreach ($this->listeners
[$eventName] as $key => $l) {
81 foreach ($this->listenerIds
[$eventName] as $i => $args) {
82 list($serviceId, $method, $priority) = $args;
83 if ($key === $serviceId.'.'.$method) {
84 if ($listener === array($l, $method)) {
85 unset($this->listeners
[$eventName][$key]);
86 if (empty($this->listeners
[$eventName])) {
87 unset($this->listeners
[$eventName]);
89 unset($this->listenerIds
[$eventName][$i]);
90 if (empty($this->listenerIds
[$eventName])) {
91 unset($this->listenerIds
[$eventName]);
99 parent
::removeListener($eventName, $listener);
103 * @see EventDispatcherInterface::hasListeners
105 public function hasListeners($eventName = null)
107 if (null === $eventName) {
108 return (Boolean
) count($this->listenerIds
) || (Boolean
) count($this->listeners
);
111 if (isset($this->listenerIds
[$eventName])) {
115 return parent
::hasListeners($eventName);
119 * @see EventDispatcherInterface::getListeners
121 public function getListeners($eventName = null)
123 if (null === $eventName) {
124 foreach (array_keys($this->listenerIds
) as $serviceEventName) {
125 $this->lazyLoad($serviceEventName);
128 $this->lazyLoad($eventName);
131 return parent
::getListeners($eventName);
135 * Adds a service as event subscriber
137 * @param string $serviceId The service ID of the subscriber service
138 * @param string $class The service's class name (which must implement EventSubscriberInterface)
140 public function addSubscriberService($serviceId, $class)
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);
148 foreach ($params as $listener) {
149 $this->listenerIds
[$eventName][] = array($serviceId, $listener[0], isset($listener[1]) ? $listener[1] : 0);
158 * Lazily loads listeners for this event from the dependency injection
161 * @throws \InvalidArgumentException if the service is not defined
163 public function dispatch($eventName, Event
$event = null)
165 $this->lazyLoad($eventName);
167 return parent
::dispatch($eventName, $event);
170 public function getContainer()
172 return $this->container
;
176 * Lazily loads listeners for this event from the dependency injection
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.
183 protected function lazyLoad($eventName)
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);
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);
198 $this->listeners
[$eventName][$key] = $listener;