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
29 * The service IDs of the event listeners and subscribers.
31 private $listenerIds = array();
34 * The services registered as listeners.
36 private $listeners = array();
38 public function __construct(ContainerInterface
$container)
40 $this->container
= $container;
44 * Adds a service as event listener.
46 * @param string $eventName Event for which the listener is added
47 * @param array $callback The service ID of the listener service & the method
48 * name that has to be called
49 * @param int $priority The higher this value, the earlier an event listener
50 * will be triggered in the chain.
53 * @throws \InvalidArgumentException
55 public function addListenerService($eventName, $callback, $priority = 0)
57 if (!is_array($callback) ||
2 !== count($callback)) {
58 throw new \
InvalidArgumentException('Expected an array("service", "method") argument');
61 $this->listenerIds
[$eventName][] = array($callback[0], $callback[1], $priority);
64 public function removeListener($eventName, $listener)
66 $this->lazyLoad($eventName);
68 if (isset($this->listenerIds
[$eventName])) {
69 foreach ($this->listenerIds
[$eventName] as $i => $args) {
70 list($serviceId, $method) = $args;
71 $key = $serviceId.'.'.$method;
72 if (isset($this->listeners
[$eventName][$key]) && $listener === array($this->listeners
[$eventName][$key], $method)) {
73 unset($this->listeners
[$eventName][$key]);
74 if (empty($this->listeners
[$eventName])) {
75 unset($this->listeners
[$eventName]);
77 unset($this->listenerIds
[$eventName][$i]);
78 if (empty($this->listenerIds
[$eventName])) {
79 unset($this->listenerIds
[$eventName]);
85 parent
::removeListener($eventName, $listener);
91 public function hasListeners($eventName = null)
93 if (null === $eventName) {
94 return $this->listenerIds ||
$this->listeners || parent
::hasListeners();
97 if (isset($this->listenerIds
[$eventName])) {
101 return parent
::hasListeners($eventName);
107 public function getListeners($eventName = null)
109 if (null === $eventName) {
110 foreach ($this->listenerIds
as $serviceEventName => $args) {
111 $this->lazyLoad($serviceEventName);
114 $this->lazyLoad($eventName);
117 return parent
::getListeners($eventName);
123 public function getListenerPriority($eventName, $listener)
125 $this->lazyLoad($eventName);
127 return parent
::getListenerPriority($eventName, $listener);
131 * Adds a service as event subscriber.
133 * @param string $serviceId The service ID of the subscriber service
134 * @param string $class The service's class name (which must implement EventSubscriberInterface)
136 public function addSubscriberService($serviceId, $class)
138 foreach ($class::getSubscribedEvents() as $eventName => $params) {
139 if (is_string($params)) {
140 $this->listenerIds
[$eventName][] = array($serviceId, $params, 0);
141 } elseif (is_string($params[0])) {
142 $this->listenerIds
[$eventName][] = array($serviceId, $params[0], isset($params[1]) ?
$params[1] : 0);
144 foreach ($params as $listener) {
145 $this->listenerIds
[$eventName][] = array($serviceId, $listener[0], isset($listener[1]) ?
$listener[1] : 0);
151 public function getContainer()
153 return $this->container
;
157 * Lazily loads listeners for this event from the dependency injection
160 * @param string $eventName The name of the event to dispatch. The name of
161 * the event is the name of the method that is
162 * invoked on listeners.
164 protected function lazyLoad($eventName)
166 if (isset($this->listenerIds
[$eventName])) {
167 foreach ($this->listenerIds
[$eventName] as $args) {
168 list($serviceId, $method, $priority) = $args;
169 $listener = $this->container
->get($serviceId);
171 $key = $serviceId.'.'.$method;
172 if (!isset($this->listeners
[$eventName][$key])) {
173 $this->addListener($eventName, array($listener, $method), $priority);
174 } elseif ($this->listeners
[$eventName][$key] !== $listener) {
175 parent
::removeListener($eventName, array($this->listeners
[$eventName][$key], $method));
176 $this->addListener($eventName, array($listener, $method), $priority);
179 $this->listeners
[$eventName][$key] = $listener;