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.
29 * @var ContainerInterface
34 * The service IDs of the event listeners and subscribers.
38 private $listenerIds = array();
41 * The services registered as listeners.
45 private $listeners = array();
50 * @param ContainerInterface $container A ContainerInterface instance
52 public function __construct(ContainerInterface $container)
54 $this->container = $container;
58 * Adds a service as event listener.
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.
67 * @throws \InvalidArgumentException
69 public function addListenerService($eventName, $callback, $priority = 0)
71 if (!is_array($callback) || 2 !== count($callback)) {
72 throw new \InvalidArgumentException('Expected an array("service", "method") argument');
75 $this->listenerIds[$eventName][] = array($callback[0], $callback[1], $priority);
78 public function removeListener($eventName, $listener)
80 $this->lazyLoad($eventName);
82 if (isset($this->listenerIds[$eventName])) {
83 foreach ($this->listenerIds[$eventName] as $i => $args) {
84 list($serviceId, $method, $priority) = $args;
85 $key = $serviceId.'.'.$method;
86 if (isset($this->listeners[$eventName][$key]) && $listener === array($this->listeners[$eventName][$key], $method)) {
87 unset($this->listeners[$eventName][$key]);
88 if (empty($this->listeners[$eventName])) {
89 unset($this->listeners[$eventName]);
91 unset($this->listenerIds[$eventName][$i]);
92 if (empty($this->listenerIds[$eventName])) {
93 unset($this->listenerIds[$eventName]);
99 parent::removeListener($eventName, $listener);
105 public function hasListeners($eventName = null)
107 if (null === $eventName) {
108 return (bool) count($this->listenerIds) || (bool) count($this->listeners);
111 if (isset($this->listenerIds[$eventName])) {
115 return parent::hasListeners($eventName);
121 public function getListeners($eventName = null)
123 if (null === $eventName) {
124 foreach ($this->listenerIds as $serviceEventName => $args) {
125 $this->lazyLoad($serviceEventName);
128 $this->lazyLoad($eventName);
131 return parent::getListeners($eventName);
137 public function getListenerPriority($eventName, $listener)
139 $this->lazyLoad($eventName);
141 return parent::getListenerPriority($eventName, $listener);
145 * Adds a service as event subscriber.
147 * @param string $serviceId The service ID of the subscriber service
148 * @param string $class The service's class name (which must implement EventSubscriberInterface)
150 public function addSubscriberService($serviceId, $class)
152 foreach ($class::getSubscribedEvents() as $eventName => $params) {
153 if (is_string($params)) {
154 $this->listenerIds[$eventName][] = array($serviceId, $params, 0);
155 } elseif (is_string($params[0])) {
156 $this->listenerIds[$eventName][] = array($serviceId, $params[0], isset($params[1]) ? $params[1] : 0);
158 foreach ($params as $listener) {
159 $this->listenerIds[$eventName][] = array($serviceId, $listener[0], isset($listener[1]) ? $listener[1] : 0);
165 public function getContainer()
167 return $this->container;
171 * Lazily loads listeners for this event from the dependency injection
174 * @param string $eventName The name of the event to dispatch. The name of
175 * the event is the name of the method that is
176 * invoked on listeners.
178 protected function lazyLoad($eventName)
180 if (isset($this->listenerIds[$eventName])) {
181 foreach ($this->listenerIds[$eventName] as $args) {
182 list($serviceId, $method, $priority) = $args;
183 $listener = $this->container->get($serviceId);
185 $key = $serviceId.'.'.$method;
186 if (!isset($this->listeners[$eventName][$key])) {
187 $this->addListener($eventName, array($listener, $method), $priority);
188 } elseif ($listener !== $this->listeners[$eventName][$key]) {
189 parent::removeListener($eventName, array($this->listeners[$eventName][$key], $method));
190 $this->addListener($eventName, array($listener, $method), $priority);
193 $this->listeners[$eventName][$key] = $listener;