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