Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\EventDispatcher;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Lazily loads listeners and subscribers from the dependency injection
|
Chris@0
|
18 * container.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
21 * @author Bernhard Schussek <bschussek@gmail.com>
|
Chris@0
|
22 * @author Jordan Alliot <jordan.alliot@gmail.com>
|
Chris@14
|
23 *
|
Chris@14
|
24 * @deprecated since 3.3, to be removed in 4.0. Use EventDispatcher with closure factories instead.
|
Chris@0
|
25 */
|
Chris@0
|
26 class ContainerAwareEventDispatcher extends EventDispatcher
|
Chris@0
|
27 {
|
Chris@0
|
28 private $container;
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * The service IDs of the event listeners and subscribers.
|
Chris@0
|
32 */
|
Chris@17
|
33 private $listenerIds = [];
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * The services registered as listeners.
|
Chris@0
|
37 */
|
Chris@17
|
38 private $listeners = [];
|
Chris@0
|
39
|
Chris@0
|
40 public function __construct(ContainerInterface $container)
|
Chris@0
|
41 {
|
Chris@0
|
42 $this->container = $container;
|
Chris@14
|
43
|
Chris@17
|
44 $class = \get_class($this);
|
Chris@14
|
45 if ($this instanceof \PHPUnit_Framework_MockObject_MockObject || $this instanceof \Prophecy\Doubler\DoubleInterface) {
|
Chris@14
|
46 $class = get_parent_class($class);
|
Chris@14
|
47 }
|
Chris@14
|
48 if (__CLASS__ !== $class) {
|
Chris@14
|
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);
|
Chris@14
|
50 }
|
Chris@0
|
51 }
|
Chris@0
|
52
|
Chris@0
|
53 /**
|
Chris@0
|
54 * Adds a service as event listener.
|
Chris@0
|
55 *
|
Chris@0
|
56 * @param string $eventName Event for which the listener is added
|
Chris@0
|
57 * @param array $callback The service ID of the listener service & the method
|
Chris@0
|
58 * name that has to be called
|
Chris@0
|
59 * @param int $priority The higher this value, the earlier an event listener
|
Chris@0
|
60 * will be triggered in the chain.
|
Chris@0
|
61 * Defaults to 0.
|
Chris@0
|
62 *
|
Chris@0
|
63 * @throws \InvalidArgumentException
|
Chris@0
|
64 */
|
Chris@0
|
65 public function addListenerService($eventName, $callback, $priority = 0)
|
Chris@0
|
66 {
|
Chris@14
|
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);
|
Chris@14
|
68
|
Chris@17
|
69 if (!\is_array($callback) || 2 !== \count($callback)) {
|
Chris@17
|
70 throw new \InvalidArgumentException('Expected an ["service", "method"] argument');
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@17
|
73 $this->listenerIds[$eventName][] = [$callback[0], $callback[1], $priority];
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 public function removeListener($eventName, $listener)
|
Chris@0
|
77 {
|
Chris@0
|
78 $this->lazyLoad($eventName);
|
Chris@0
|
79
|
Chris@0
|
80 if (isset($this->listenerIds[$eventName])) {
|
Chris@14
|
81 foreach ($this->listenerIds[$eventName] as $i => list($serviceId, $method)) {
|
Chris@0
|
82 $key = $serviceId.'.'.$method;
|
Chris@17
|
83 if (isset($this->listeners[$eventName][$key]) && $listener === [$this->listeners[$eventName][$key], $method]) {
|
Chris@0
|
84 unset($this->listeners[$eventName][$key]);
|
Chris@0
|
85 if (empty($this->listeners[$eventName])) {
|
Chris@0
|
86 unset($this->listeners[$eventName]);
|
Chris@0
|
87 }
|
Chris@0
|
88 unset($this->listenerIds[$eventName][$i]);
|
Chris@0
|
89 if (empty($this->listenerIds[$eventName])) {
|
Chris@0
|
90 unset($this->listenerIds[$eventName]);
|
Chris@0
|
91 }
|
Chris@0
|
92 }
|
Chris@0
|
93 }
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@0
|
96 parent::removeListener($eventName, $listener);
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 /**
|
Chris@0
|
100 * {@inheritdoc}
|
Chris@0
|
101 */
|
Chris@0
|
102 public function hasListeners($eventName = null)
|
Chris@0
|
103 {
|
Chris@0
|
104 if (null === $eventName) {
|
Chris@12
|
105 return $this->listenerIds || $this->listeners || parent::hasListeners();
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@0
|
108 if (isset($this->listenerIds[$eventName])) {
|
Chris@0
|
109 return true;
|
Chris@0
|
110 }
|
Chris@0
|
111
|
Chris@0
|
112 return parent::hasListeners($eventName);
|
Chris@0
|
113 }
|
Chris@0
|
114
|
Chris@0
|
115 /**
|
Chris@0
|
116 * {@inheritdoc}
|
Chris@0
|
117 */
|
Chris@0
|
118 public function getListeners($eventName = null)
|
Chris@0
|
119 {
|
Chris@0
|
120 if (null === $eventName) {
|
Chris@0
|
121 foreach ($this->listenerIds as $serviceEventName => $args) {
|
Chris@0
|
122 $this->lazyLoad($serviceEventName);
|
Chris@0
|
123 }
|
Chris@0
|
124 } else {
|
Chris@0
|
125 $this->lazyLoad($eventName);
|
Chris@0
|
126 }
|
Chris@0
|
127
|
Chris@0
|
128 return parent::getListeners($eventName);
|
Chris@0
|
129 }
|
Chris@0
|
130
|
Chris@0
|
131 /**
|
Chris@0
|
132 * {@inheritdoc}
|
Chris@0
|
133 */
|
Chris@0
|
134 public function getListenerPriority($eventName, $listener)
|
Chris@0
|
135 {
|
Chris@0
|
136 $this->lazyLoad($eventName);
|
Chris@0
|
137
|
Chris@0
|
138 return parent::getListenerPriority($eventName, $listener);
|
Chris@0
|
139 }
|
Chris@0
|
140
|
Chris@0
|
141 /**
|
Chris@0
|
142 * Adds a service as event subscriber.
|
Chris@0
|
143 *
|
Chris@0
|
144 * @param string $serviceId The service ID of the subscriber service
|
Chris@0
|
145 * @param string $class The service's class name (which must implement EventSubscriberInterface)
|
Chris@0
|
146 */
|
Chris@0
|
147 public function addSubscriberService($serviceId, $class)
|
Chris@0
|
148 {
|
Chris@14
|
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);
|
Chris@14
|
150
|
Chris@0
|
151 foreach ($class::getSubscribedEvents() as $eventName => $params) {
|
Chris@17
|
152 if (\is_string($params)) {
|
Chris@17
|
153 $this->listenerIds[$eventName][] = [$serviceId, $params, 0];
|
Chris@17
|
154 } elseif (\is_string($params[0])) {
|
Chris@17
|
155 $this->listenerIds[$eventName][] = [$serviceId, $params[0], isset($params[1]) ? $params[1] : 0];
|
Chris@0
|
156 } else {
|
Chris@0
|
157 foreach ($params as $listener) {
|
Chris@17
|
158 $this->listenerIds[$eventName][] = [$serviceId, $listener[0], isset($listener[1]) ? $listener[1] : 0];
|
Chris@0
|
159 }
|
Chris@0
|
160 }
|
Chris@0
|
161 }
|
Chris@0
|
162 }
|
Chris@0
|
163
|
Chris@0
|
164 public function getContainer()
|
Chris@0
|
165 {
|
Chris@14
|
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);
|
Chris@14
|
167
|
Chris@0
|
168 return $this->container;
|
Chris@0
|
169 }
|
Chris@0
|
170
|
Chris@0
|
171 /**
|
Chris@0
|
172 * Lazily loads listeners for this event from the dependency injection
|
Chris@0
|
173 * container.
|
Chris@0
|
174 *
|
Chris@0
|
175 * @param string $eventName The name of the event to dispatch. The name of
|
Chris@0
|
176 * the event is the name of the method that is
|
Chris@0
|
177 * invoked on listeners.
|
Chris@0
|
178 */
|
Chris@0
|
179 protected function lazyLoad($eventName)
|
Chris@0
|
180 {
|
Chris@0
|
181 if (isset($this->listenerIds[$eventName])) {
|
Chris@0
|
182 foreach ($this->listenerIds[$eventName] as list($serviceId, $method, $priority)) {
|
Chris@0
|
183 $listener = $this->container->get($serviceId);
|
Chris@0
|
184
|
Chris@0
|
185 $key = $serviceId.'.'.$method;
|
Chris@0
|
186 if (!isset($this->listeners[$eventName][$key])) {
|
Chris@17
|
187 $this->addListener($eventName, [$listener, $method], $priority);
|
Chris@14
|
188 } elseif ($this->listeners[$eventName][$key] !== $listener) {
|
Chris@17
|
189 parent::removeListener($eventName, [$this->listeners[$eventName][$key], $method]);
|
Chris@17
|
190 $this->addListener($eventName, [$listener, $method], $priority);
|
Chris@0
|
191 }
|
Chris@0
|
192
|
Chris@0
|
193 $this->listeners[$eventName][$key] = $listener;
|
Chris@0
|
194 }
|
Chris@0
|
195 }
|
Chris@0
|
196 }
|
Chris@0
|
197 }
|