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\Debug
;
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
;
21 * Collects some data about event listeners.
23 * This event dispatcher delegates the dispatching to another one.
25 * @author Fabien Potencier <fabien@symfony.com>
27 class TraceableEventDispatcher
implements TraceableEventDispatcherInterface
34 private $wrappedListeners;
36 public function __construct(EventDispatcherInterface
$dispatcher, Stopwatch
$stopwatch, LoggerInterface
$logger = null)
38 $this->dispatcher
= $dispatcher;
39 $this->stopwatch
= $stopwatch;
40 $this->logger
= $logger;
41 $this->called
= array();
42 $this->wrappedListeners
= array();
48 public function addListener($eventName, $listener, $priority = 0)
50 $this->dispatcher
->addListener($eventName, $listener, $priority);
56 public function addSubscriber(EventSubscriberInterface
$subscriber)
58 $this->dispatcher
->addSubscriber($subscriber);
64 public function removeListener($eventName, $listener)
66 if (isset($this->wrappedListeners
[$eventName])) {
67 foreach ($this->wrappedListeners
[$eventName] as $index => $wrappedListener) {
68 if ($wrappedListener->getWrappedListener() === $listener) {
69 $listener = $wrappedListener;
70 unset($this->wrappedListeners
[$eventName][$index]);
76 return $this->dispatcher
->removeListener($eventName, $listener);
82 public function removeSubscriber(EventSubscriberInterface
$subscriber)
84 return $this->dispatcher
->removeSubscriber($subscriber);
90 public function getListeners($eventName = null)
92 return $this->dispatcher
->getListeners($eventName);
98 public function getListenerPriority($eventName, $listener)
100 if (!method_exists($this->dispatcher
, 'getListenerPriority')) {
104 return $this->dispatcher
->getListenerPriority($eventName, $listener);
110 public function hasListeners($eventName = null)
112 return $this->dispatcher
->hasListeners($eventName);
118 public function dispatch($eventName, Event
$event = null)
120 if (null === $event) {
121 $event = new Event();
124 if (null !== $this->logger
&& $event->isPropagationStopped()) {
125 $this->logger
->debug(sprintf('The "%s" event is already stopped. No listeners have been called.', $eventName));
128 $this->preProcess($eventName);
129 $this->preDispatch($eventName, $event);
131 $e = $this->stopwatch
->start($eventName, 'section');
133 $this->dispatcher
->dispatch($eventName, $event);
135 if ($e->isStarted()) {
139 $this->postDispatch($eventName, $event);
140 $this->postProcess($eventName);
148 public function getCalledListeners()
151 foreach ($this->called
as $eventName => $listeners) {
152 foreach ($listeners as $listener) {
153 $info = $this->getListenerInfo($listener->getWrappedListener(), $eventName);
154 $called[$eventName.'.'.$info['pretty']] = $info;
164 public function getNotCalledListeners()
167 $allListeners = $this->getListeners();
168 } catch (\Exception
$e) {
169 if (null !== $this->logger
) {
170 $this->logger
->info('An exception was thrown while getting the uncalled listeners.', array('exception' => $e));
173 // unable to retrieve the uncalled listeners
177 $notCalled = array();
178 foreach ($allListeners as $eventName => $listeners) {
179 foreach ($listeners as $listener) {
181 if (isset($this->called
[$eventName])) {
182 foreach ($this->called
[$eventName] as $l) {
183 if ($l->getWrappedListener() === $listener) {
192 $info = $this->getListenerInfo($listener, $eventName);
193 $notCalled[$eventName.'.'.$info['pretty']] = $info;
198 uasort($notCalled, array($this, 'sortListenersByPriority'));
204 * Proxies all method calls to the original event dispatcher.
206 * @param string $method The method name
207 * @param array $arguments The method arguments
211 public function __call($method, $arguments)
213 return call_user_func_array(array($this->dispatcher
, $method), $arguments);
217 * Called before dispatching the event.
219 * @param string $eventName The event name
220 * @param Event $event The event
222 protected function preDispatch($eventName, Event
$event)
227 * Called after dispatching the event.
229 * @param string $eventName The event name
230 * @param Event $event The event
232 protected function postDispatch($eventName, Event
$event)
236 private function preProcess($eventName)
238 foreach ($this->dispatcher
->getListeners($eventName) as $listener) {
239 $info = $this->getListenerInfo($listener, $eventName);
240 $name = isset($info['class']) ?
$info['class'] : $info['type'];
241 $wrappedListener = new WrappedListener($listener, $name, $this->stopwatch
, $this);
242 $this->wrappedListeners
[$eventName][] = $wrappedListener;
243 $this->dispatcher
->removeListener($eventName, $listener);
244 $this->dispatcher
->addListener($eventName, $wrappedListener, $info['priority']);
248 private function postProcess($eventName)
250 unset($this->wrappedListeners
[$eventName]);
252 foreach ($this->dispatcher
->getListeners($eventName) as $listener) {
253 if (!$listener instanceof WrappedListener
) { // #12845: a new listener was added during dispatch.
257 $priority = $this->getListenerPriority($eventName, $listener);
258 $this->dispatcher
->removeListener($eventName, $listener);
259 $this->dispatcher
->addListener($eventName, $listener->getWrappedListener(), $priority);
261 $info = $this->getListenerInfo($listener->getWrappedListener(), $eventName);
262 if ($listener->wasCalled()) {
263 if (null !== $this->logger
) {
264 $this->logger
->debug(sprintf('Notified event "%s" to listener "%s".', $eventName, $info['pretty']));
267 if (!isset($this->called
[$eventName])) {
268 $this->called
[$eventName] = new \
SplObjectStorage();
271 $this->called
[$eventName]->attach($listener);
274 if (null !== $this->logger
&& $skipped) {
275 $this->logger
->debug(sprintf('Listener "%s" was not called for event "%s".', $info['pretty'], $eventName));
278 if ($listener->stoppedPropagation()) {
279 if (null !== $this->logger
) {
280 $this->logger
->debug(sprintf('Listener "%s" stopped propagation of the event "%s".', $info['pretty'], $eventName));
289 * Returns information about the listener.
291 * @param object $listener The listener
292 * @param string $eventName The event name
294 * @return array Information about the listener
296 private function getListenerInfo($listener, $eventName)
299 'event' => $eventName,
300 'priority' => $this->getListenerPriority($eventName, $listener),
303 // unwrap for correct listener info
304 if ($listener instanceof WrappedListener
) {
305 $listener = $listener->getWrappedListener();
308 if ($listener instanceof \Closure
) {
311 'pretty' => 'closure',
313 } elseif (is_string($listener)) {
315 $r = new \
ReflectionFunction($listener);
316 $file = $r->getFileName();
317 $line = $r->getStartLine();
318 } catch (\ReflectionException
$e) {
323 'type' => 'Function',
324 'function' => $listener,
327 'pretty' => $listener,
329 } elseif (is_array($listener) ||
(is_object($listener) && is_callable($listener))) {
330 if (!is_array($listener)) {
331 $listener = array($listener, '__invoke');
333 $class = is_object($listener[0]) ?
get_class($listener[0]) : $listener[0];
335 $r = new \
ReflectionMethod($class, $listener[1]);
336 $file = $r->getFileName();
337 $line = $r->getStartLine();
338 } catch (\ReflectionException
$e) {
345 'method' => $listener[1],
348 'pretty' => $class.'::'.$listener[1],
355 private function sortListenersByPriority($a, $b)
357 if (is_int($a['priority']) && !is_int($b['priority'])) {
361 if (!is_int($a['priority']) && is_int($b['priority'])) {
365 if ($a['priority'] === $b['priority']) {
369 if ($a['priority'] > $b['priority']) {