composer package updates
[openemr.git] / vendor / symfony / event-dispatcher / ContainerAwareEventDispatcher.php
blob243ce5ce509a29b0acc8005402dfd134317ff03e
1 <?php
3 /*
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;
16 /**
17 * Lazily loads listeners and subscribers from the dependency injection
18 * container.
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
26 private $container;
28 /**
29 * The service IDs of the event listeners and subscribers.
31 private $listenerIds = array();
33 /**
34 * The services registered as listeners.
36 private $listeners = array();
38 public function __construct(ContainerInterface $container)
40 $this->container = $container;
43 /**
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.
51 * Defaults to 0.
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);
88 /**
89 * {@inheritdoc}
91 public function hasListeners($eventName = null)
93 if (null === $eventName) {
94 return $this->listenerIds || $this->listeners || parent::hasListeners();
97 if (isset($this->listenerIds[$eventName])) {
98 return true;
101 return parent::hasListeners($eventName);
105 * {@inheritdoc}
107 public function getListeners($eventName = null)
109 if (null === $eventName) {
110 foreach ($this->listenerIds as $serviceEventName => $args) {
111 $this->lazyLoad($serviceEventName);
113 } else {
114 $this->lazyLoad($eventName);
117 return parent::getListeners($eventName);
121 * {@inheritdoc}
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);
143 } else {
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
158 * container.
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;