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\Debug;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
Chris@0
|
15 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
Chris@0
|
16 use Symfony\Component\EventDispatcher\Event;
|
Chris@0
|
17 use Symfony\Component\Stopwatch\Stopwatch;
|
Chris@0
|
18 use Psr\Log\LoggerInterface;
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * Collects some data about event listeners.
|
Chris@0
|
22 *
|
Chris@0
|
23 * This event dispatcher delegates the dispatching to another one.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
26 */
|
Chris@0
|
27 class TraceableEventDispatcher implements TraceableEventDispatcherInterface
|
Chris@0
|
28 {
|
Chris@0
|
29 protected $logger;
|
Chris@0
|
30 protected $stopwatch;
|
Chris@0
|
31
|
Chris@0
|
32 private $called;
|
Chris@0
|
33 private $dispatcher;
|
Chris@0
|
34 private $wrappedListeners;
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * Constructor.
|
Chris@0
|
38 *
|
Chris@0
|
39 * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
|
Chris@0
|
40 * @param Stopwatch $stopwatch A Stopwatch instance
|
Chris@0
|
41 * @param LoggerInterface $logger A LoggerInterface instance
|
Chris@0
|
42 */
|
Chris@0
|
43 public function __construct(EventDispatcherInterface $dispatcher, Stopwatch $stopwatch, LoggerInterface $logger = null)
|
Chris@0
|
44 {
|
Chris@0
|
45 $this->dispatcher = $dispatcher;
|
Chris@0
|
46 $this->stopwatch = $stopwatch;
|
Chris@0
|
47 $this->logger = $logger;
|
Chris@0
|
48 $this->called = array();
|
Chris@0
|
49 $this->wrappedListeners = array();
|
Chris@0
|
50 }
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * {@inheritdoc}
|
Chris@0
|
54 */
|
Chris@0
|
55 public function addListener($eventName, $listener, $priority = 0)
|
Chris@0
|
56 {
|
Chris@0
|
57 $this->dispatcher->addListener($eventName, $listener, $priority);
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 /**
|
Chris@0
|
61 * {@inheritdoc}
|
Chris@0
|
62 */
|
Chris@0
|
63 public function addSubscriber(EventSubscriberInterface $subscriber)
|
Chris@0
|
64 {
|
Chris@0
|
65 $this->dispatcher->addSubscriber($subscriber);
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 /**
|
Chris@0
|
69 * {@inheritdoc}
|
Chris@0
|
70 */
|
Chris@0
|
71 public function removeListener($eventName, $listener)
|
Chris@0
|
72 {
|
Chris@0
|
73 if (isset($this->wrappedListeners[$eventName])) {
|
Chris@0
|
74 foreach ($this->wrappedListeners[$eventName] as $index => $wrappedListener) {
|
Chris@0
|
75 if ($wrappedListener->getWrappedListener() === $listener) {
|
Chris@0
|
76 $listener = $wrappedListener;
|
Chris@0
|
77 unset($this->wrappedListeners[$eventName][$index]);
|
Chris@0
|
78 break;
|
Chris@0
|
79 }
|
Chris@0
|
80 }
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 return $this->dispatcher->removeListener($eventName, $listener);
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@0
|
86 /**
|
Chris@0
|
87 * {@inheritdoc}
|
Chris@0
|
88 */
|
Chris@0
|
89 public function removeSubscriber(EventSubscriberInterface $subscriber)
|
Chris@0
|
90 {
|
Chris@0
|
91 return $this->dispatcher->removeSubscriber($subscriber);
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 /**
|
Chris@0
|
95 * {@inheritdoc}
|
Chris@0
|
96 */
|
Chris@0
|
97 public function getListeners($eventName = null)
|
Chris@0
|
98 {
|
Chris@0
|
99 return $this->dispatcher->getListeners($eventName);
|
Chris@0
|
100 }
|
Chris@0
|
101
|
Chris@0
|
102 /**
|
Chris@0
|
103 * {@inheritdoc}
|
Chris@0
|
104 */
|
Chris@0
|
105 public function getListenerPriority($eventName, $listener)
|
Chris@0
|
106 {
|
Chris@0
|
107 // we might have wrapped listeners for the event (if called while dispatching)
|
Chris@0
|
108 // in that case get the priority by wrapper
|
Chris@0
|
109 if (isset($this->wrappedListeners[$eventName])) {
|
Chris@0
|
110 foreach ($this->wrappedListeners[$eventName] as $index => $wrappedListener) {
|
Chris@0
|
111 if ($wrappedListener->getWrappedListener() === $listener) {
|
Chris@0
|
112 return $this->dispatcher->getListenerPriority($eventName, $wrappedListener);
|
Chris@0
|
113 }
|
Chris@0
|
114 }
|
Chris@0
|
115 }
|
Chris@0
|
116
|
Chris@0
|
117 return $this->dispatcher->getListenerPriority($eventName, $listener);
|
Chris@0
|
118 }
|
Chris@0
|
119
|
Chris@0
|
120 /**
|
Chris@0
|
121 * {@inheritdoc}
|
Chris@0
|
122 */
|
Chris@0
|
123 public function hasListeners($eventName = null)
|
Chris@0
|
124 {
|
Chris@0
|
125 return $this->dispatcher->hasListeners($eventName);
|
Chris@0
|
126 }
|
Chris@0
|
127
|
Chris@0
|
128 /**
|
Chris@0
|
129 * {@inheritdoc}
|
Chris@0
|
130 */
|
Chris@0
|
131 public function dispatch($eventName, Event $event = null)
|
Chris@0
|
132 {
|
Chris@0
|
133 if (null === $event) {
|
Chris@0
|
134 $event = new Event();
|
Chris@0
|
135 }
|
Chris@0
|
136
|
Chris@0
|
137 if (null !== $this->logger && $event->isPropagationStopped()) {
|
Chris@0
|
138 $this->logger->debug(sprintf('The "%s" event is already stopped. No listeners have been called.', $eventName));
|
Chris@0
|
139 }
|
Chris@0
|
140
|
Chris@0
|
141 $this->preProcess($eventName);
|
Chris@0
|
142 $this->preDispatch($eventName, $event);
|
Chris@0
|
143
|
Chris@0
|
144 $e = $this->stopwatch->start($eventName, 'section');
|
Chris@0
|
145
|
Chris@0
|
146 $this->dispatcher->dispatch($eventName, $event);
|
Chris@0
|
147
|
Chris@0
|
148 if ($e->isStarted()) {
|
Chris@0
|
149 $e->stop();
|
Chris@0
|
150 }
|
Chris@0
|
151
|
Chris@0
|
152 $this->postDispatch($eventName, $event);
|
Chris@0
|
153 $this->postProcess($eventName);
|
Chris@0
|
154
|
Chris@0
|
155 return $event;
|
Chris@0
|
156 }
|
Chris@0
|
157
|
Chris@0
|
158 /**
|
Chris@0
|
159 * {@inheritdoc}
|
Chris@0
|
160 */
|
Chris@0
|
161 public function getCalledListeners()
|
Chris@0
|
162 {
|
Chris@0
|
163 $called = array();
|
Chris@0
|
164 foreach ($this->called as $eventName => $listeners) {
|
Chris@0
|
165 foreach ($listeners as $listener) {
|
Chris@0
|
166 $called[$eventName.'.'.$listener->getPretty()] = $listener->getInfo($eventName);
|
Chris@0
|
167 }
|
Chris@0
|
168 }
|
Chris@0
|
169
|
Chris@0
|
170 return $called;
|
Chris@0
|
171 }
|
Chris@0
|
172
|
Chris@0
|
173 /**
|
Chris@0
|
174 * {@inheritdoc}
|
Chris@0
|
175 */
|
Chris@0
|
176 public function getNotCalledListeners()
|
Chris@0
|
177 {
|
Chris@0
|
178 try {
|
Chris@0
|
179 $allListeners = $this->getListeners();
|
Chris@0
|
180 } catch (\Exception $e) {
|
Chris@0
|
181 if (null !== $this->logger) {
|
Chris@0
|
182 $this->logger->info('An exception was thrown while getting the uncalled listeners.', array('exception' => $e));
|
Chris@0
|
183 }
|
Chris@0
|
184
|
Chris@0
|
185 // unable to retrieve the uncalled listeners
|
Chris@0
|
186 return array();
|
Chris@0
|
187 }
|
Chris@0
|
188
|
Chris@0
|
189 $notCalled = array();
|
Chris@0
|
190 foreach ($allListeners as $eventName => $listeners) {
|
Chris@0
|
191 foreach ($listeners as $listener) {
|
Chris@0
|
192 $called = false;
|
Chris@0
|
193 if (isset($this->called[$eventName])) {
|
Chris@0
|
194 foreach ($this->called[$eventName] as $l) {
|
Chris@0
|
195 if ($l->getWrappedListener() === $listener) {
|
Chris@0
|
196 $called = true;
|
Chris@0
|
197
|
Chris@0
|
198 break;
|
Chris@0
|
199 }
|
Chris@0
|
200 }
|
Chris@0
|
201 }
|
Chris@0
|
202
|
Chris@0
|
203 if (!$called) {
|
Chris@0
|
204 if (!$listener instanceof WrappedListener) {
|
Chris@0
|
205 $listener = new WrappedListener($listener, null, $this->stopwatch, $this);
|
Chris@0
|
206 }
|
Chris@0
|
207 $notCalled[$eventName.'.'.$listener->getPretty()] = $listener->getInfo($eventName);
|
Chris@0
|
208 }
|
Chris@0
|
209 }
|
Chris@0
|
210 }
|
Chris@0
|
211
|
Chris@0
|
212 uasort($notCalled, array($this, 'sortListenersByPriority'));
|
Chris@0
|
213
|
Chris@0
|
214 return $notCalled;
|
Chris@0
|
215 }
|
Chris@0
|
216
|
Chris@0
|
217 /**
|
Chris@0
|
218 * Proxies all method calls to the original event dispatcher.
|
Chris@0
|
219 *
|
Chris@0
|
220 * @param string $method The method name
|
Chris@0
|
221 * @param array $arguments The method arguments
|
Chris@0
|
222 *
|
Chris@0
|
223 * @return mixed
|
Chris@0
|
224 */
|
Chris@0
|
225 public function __call($method, $arguments)
|
Chris@0
|
226 {
|
Chris@0
|
227 return call_user_func_array(array($this->dispatcher, $method), $arguments);
|
Chris@0
|
228 }
|
Chris@0
|
229
|
Chris@0
|
230 /**
|
Chris@0
|
231 * Called before dispatching the event.
|
Chris@0
|
232 *
|
Chris@0
|
233 * @param string $eventName The event name
|
Chris@0
|
234 * @param Event $event The event
|
Chris@0
|
235 */
|
Chris@0
|
236 protected function preDispatch($eventName, Event $event)
|
Chris@0
|
237 {
|
Chris@0
|
238 }
|
Chris@0
|
239
|
Chris@0
|
240 /**
|
Chris@0
|
241 * Called after dispatching the event.
|
Chris@0
|
242 *
|
Chris@0
|
243 * @param string $eventName The event name
|
Chris@0
|
244 * @param Event $event The event
|
Chris@0
|
245 */
|
Chris@0
|
246 protected function postDispatch($eventName, Event $event)
|
Chris@0
|
247 {
|
Chris@0
|
248 }
|
Chris@0
|
249
|
Chris@0
|
250 private function preProcess($eventName)
|
Chris@0
|
251 {
|
Chris@0
|
252 foreach ($this->dispatcher->getListeners($eventName) as $listener) {
|
Chris@0
|
253 $priority = $this->getListenerPriority($eventName, $listener);
|
Chris@0
|
254 $wrappedListener = new WrappedListener($listener, null, $this->stopwatch, $this);
|
Chris@0
|
255 $this->wrappedListeners[$eventName][] = $wrappedListener;
|
Chris@0
|
256 $this->dispatcher->removeListener($eventName, $listener);
|
Chris@0
|
257 $this->dispatcher->addListener($eventName, $wrappedListener, $priority);
|
Chris@0
|
258 }
|
Chris@0
|
259 }
|
Chris@0
|
260
|
Chris@0
|
261 private function postProcess($eventName)
|
Chris@0
|
262 {
|
Chris@0
|
263 unset($this->wrappedListeners[$eventName]);
|
Chris@0
|
264 $skipped = false;
|
Chris@0
|
265 foreach ($this->dispatcher->getListeners($eventName) as $listener) {
|
Chris@0
|
266 if (!$listener instanceof WrappedListener) { // #12845: a new listener was added during dispatch.
|
Chris@0
|
267 continue;
|
Chris@0
|
268 }
|
Chris@0
|
269 // Unwrap listener
|
Chris@0
|
270 $priority = $this->getListenerPriority($eventName, $listener);
|
Chris@0
|
271 $this->dispatcher->removeListener($eventName, $listener);
|
Chris@0
|
272 $this->dispatcher->addListener($eventName, $listener->getWrappedListener(), $priority);
|
Chris@0
|
273
|
Chris@0
|
274 if (null !== $this->logger) {
|
Chris@0
|
275 $context = array('event' => $eventName, 'listener' => $listener->getPretty());
|
Chris@0
|
276 }
|
Chris@0
|
277
|
Chris@0
|
278 if ($listener->wasCalled()) {
|
Chris@0
|
279 if (null !== $this->logger) {
|
Chris@0
|
280 $this->logger->debug('Notified event "{event}" to listener "{listener}".', $context);
|
Chris@0
|
281 }
|
Chris@0
|
282
|
Chris@0
|
283 if (!isset($this->called[$eventName])) {
|
Chris@0
|
284 $this->called[$eventName] = new \SplObjectStorage();
|
Chris@0
|
285 }
|
Chris@0
|
286
|
Chris@0
|
287 $this->called[$eventName]->attach($listener);
|
Chris@0
|
288 }
|
Chris@0
|
289
|
Chris@0
|
290 if (null !== $this->logger && $skipped) {
|
Chris@0
|
291 $this->logger->debug('Listener "{listener}" was not called for event "{event}".', $context);
|
Chris@0
|
292 }
|
Chris@0
|
293
|
Chris@0
|
294 if ($listener->stoppedPropagation()) {
|
Chris@0
|
295 if (null !== $this->logger) {
|
Chris@0
|
296 $this->logger->debug('Listener "{listener}" stopped propagation of the event "{event}".', $context);
|
Chris@0
|
297 }
|
Chris@0
|
298
|
Chris@0
|
299 $skipped = true;
|
Chris@0
|
300 }
|
Chris@0
|
301 }
|
Chris@0
|
302 }
|
Chris@0
|
303
|
Chris@0
|
304 private function sortListenersByPriority($a, $b)
|
Chris@0
|
305 {
|
Chris@0
|
306 if (is_int($a['priority']) && !is_int($b['priority'])) {
|
Chris@0
|
307 return 1;
|
Chris@0
|
308 }
|
Chris@0
|
309
|
Chris@0
|
310 if (!is_int($a['priority']) && is_int($b['priority'])) {
|
Chris@0
|
311 return -1;
|
Chris@0
|
312 }
|
Chris@0
|
313
|
Chris@0
|
314 if ($a['priority'] === $b['priority']) {
|
Chris@0
|
315 return 0;
|
Chris@0
|
316 }
|
Chris@0
|
317
|
Chris@0
|
318 if ($a['priority'] > $b['priority']) {
|
Chris@0
|
319 return -1;
|
Chris@0
|
320 }
|
Chris@0
|
321
|
Chris@0
|
322 return 1;
|
Chris@0
|
323 }
|
Chris@0
|
324 }
|