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 * @deprecated since 3.3, to be removed in 4.0. Use EventDispatcher with closure factories instead.
26 class ContainerAwareEventDispatcher extends EventDispatcher
31 * The service IDs of the event listeners and subscribers.
33 private $listenerIds = array();
36 * The services registered as listeners.
38 private $listeners = array();
40 public function __construct(ContainerInterface $container)
42 $this->container = $container;
44 $class = get_class($this);
45 if ($this instanceof \PHPUnit_Framework_MockObject_MockObject || $this instanceof \Prophecy\Doubler\DoubleInterface) {
46 $class = get_parent_class($class);
48 if (__CLASS__ !== $class) {
49 @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);
54 * Adds a service as event listener.
56 * @param string $eventName Event for which the listener is added
57 * @param array $callback The service ID of the listener service & the method
58 * name that has to be called
59 * @param int $priority The higher this value, the earlier an event listener
60 * will be triggered in the chain.
63 * @throws \InvalidArgumentException
65 public function addListenerService($eventName, $callback, $priority = 0)
67 @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 if (!is_array($callback) || 2 !== count($callback)) {
70 throw new \InvalidArgumentException('Expected an array("service", "method") argument');
73 $this->listenerIds[$eventName][] = array($callback[0], $callback[1], $priority);
76 public function removeListener($eventName, $listener)
78 $this->lazyLoad($eventName);
80 if (isset($this->listenerIds[$eventName])) {
81 foreach ($this->listenerIds[$eventName] as $i => list($serviceId, $method)) {
82 $key = $serviceId.'.'.$method;
83 if (isset($this->listeners[$eventName][$key]) && $listener === array($this->listeners[$eventName][$key], $method)) {
84 unset($this->listeners[$eventName][$key]);
85 if (empty($this->listeners[$eventName])) {
86 unset($this->listeners[$eventName]);
88 unset($this->listenerIds[$eventName][$i]);
89 if (empty($this->listenerIds[$eventName])) {
90 unset($this->listenerIds[$eventName]);
96 parent::removeListener($eventName, $listener);
102 public function hasListeners($eventName = null)
104 if (null === $eventName) {
105 return $this->listenerIds || $this->listeners || parent::hasListeners();
108 if (isset($this->listenerIds[$eventName])) {
112 return parent::hasListeners($eventName);
118 public function getListeners($eventName = null)
120 if (null === $eventName) {
121 foreach ($this->listenerIds as $serviceEventName => $args) {
122 $this->lazyLoad($serviceEventName);
125 $this->lazyLoad($eventName);
128 return parent::getListeners($eventName);
134 public function getListenerPriority($eventName, $listener)
136 $this->lazyLoad($eventName);
138 return parent::getListenerPriority($eventName, $listener);
142 * Adds a service as event subscriber.
144 * @param string $serviceId The service ID of the subscriber service
145 * @param string $class The service's class name (which must implement EventSubscriberInterface)
147 public function addSubscriberService($serviceId, $class)
149 @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 foreach ($class::getSubscribedEvents() as $eventName => $params) {
152 if (is_string($params)) {
153 $this->listenerIds[$eventName][] = array($serviceId, $params, 0);
154 } elseif (is_string($params[0])) {
155 $this->listenerIds[$eventName][] = array($serviceId, $params[0], isset($params[1]) ? $params[1] : 0);
157 foreach ($params as $listener) {
158 $this->listenerIds[$eventName][] = array($serviceId, $listener[0], isset($listener[1]) ? $listener[1] : 0);
164 public function getContainer()
166 @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 return $this->container;
172 * Lazily loads listeners for this event from the dependency injection
175 * @param string $eventName The name of the event to dispatch. The name of
176 * the event is the name of the method that is
177 * invoked on listeners.
179 protected function lazyLoad($eventName)
181 if (isset($this->listenerIds[$eventName])) {
182 foreach ($this->listenerIds[$eventName] as list($serviceId, $method, $priority)) {
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 ($this->listeners[$eventName][$key] !== $listener) {
189 parent::removeListener($eventName, array($this->listeners[$eventName][$key], $method));
190 $this->addListener($eventName, array($listener, $method), $priority);
193 $this->listeners[$eventName][$key] = $listener;