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;
39 * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
40 * @param Stopwatch $stopwatch A Stopwatch instance
41 * @param LoggerInterface $logger A LoggerInterface instance
43 public function __construct(EventDispatcherInterface $dispatcher, Stopwatch $stopwatch, LoggerInterface $logger = null)
45 $this->dispatcher = $dispatcher;
46 $this->stopwatch = $stopwatch;
47 $this->logger = $logger;
48 $this->called = array();
49 $this->wrappedListeners = array();
55 public function addListener($eventName, $listener, $priority = 0)
57 $this->dispatcher->addListener($eventName, $listener, $priority);
63 public function addSubscriber(EventSubscriberInterface $subscriber)
65 $this->dispatcher->addSubscriber($subscriber);
71 public function removeListener($eventName, $listener)
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]);
83 return $this->dispatcher->removeListener($eventName, $listener);
89 public function removeSubscriber(EventSubscriberInterface $subscriber)
91 return $this->dispatcher->removeSubscriber($subscriber);
97 public function getListeners($eventName = null)
99 return $this->dispatcher->getListeners($eventName);
105 public function getListenerPriority($eventName, $listener)
107 if (!method_exists($this->dispatcher, 'getListenerPriority')) {
111 return $this->dispatcher->getListenerPriority($eventName, $listener);
117 public function hasListeners($eventName = null)
119 return $this->dispatcher->hasListeners($eventName);
125 public function dispatch($eventName, Event $event = null)
127 if (null === $event) {
128 $event = new Event();
131 if (null !== $this->logger && $event->isPropagationStopped()) {
132 $this->logger->debug(sprintf('The "%s" event is already stopped. No listeners have been called.', $eventName));
135 $this->preProcess($eventName);
136 $this->preDispatch($eventName, $event);
138 $e = $this->stopwatch->start($eventName, 'section');
140 $this->dispatcher->dispatch($eventName, $event);
142 if ($e->isStarted()) {
146 $this->postDispatch($eventName, $event);
147 $this->postProcess($eventName);
155 public function getCalledListeners()
158 foreach ($this->called as $eventName => $listeners) {
159 foreach ($listeners as $listener) {
160 $info = $this->getListenerInfo($listener->getWrappedListener(), $eventName);
161 $called[$eventName.'.'.$info['pretty']] = $info;
171 public function getNotCalledListeners()
174 $allListeners = $this->getListeners();
175 } catch (\Exception $e) {
176 if (null !== $this->logger) {
177 $this->logger->info('An exception was thrown while getting the uncalled listeners.', array('exception' => $e));
180 // unable to retrieve the uncalled listeners
184 $notCalled = array();
185 foreach ($allListeners as $eventName => $listeners) {
186 foreach ($listeners as $listener) {
188 if (isset($this->called[$eventName])) {
189 foreach ($this->called[$eventName] as $l) {
190 if ($l->getWrappedListener() === $listener) {
199 $info = $this->getListenerInfo($listener, $eventName);
200 $notCalled[$eventName.'.'.$info['pretty']] = $info;
205 uasort($notCalled, array($this, 'sortListenersByPriority'));
211 * Proxies all method calls to the original event dispatcher.
213 * @param string $method The method name
214 * @param array $arguments The method arguments
218 public function __call($method, $arguments)
220 return call_user_func_array(array($this->dispatcher, $method), $arguments);
224 * Called before dispatching the event.
226 * @param string $eventName The event name
227 * @param Event $event The event
229 protected function preDispatch($eventName, Event $event)
234 * Called after dispatching the event.
236 * @param string $eventName The event name
237 * @param Event $event The event
239 protected function postDispatch($eventName, Event $event)
243 private function preProcess($eventName)
245 foreach ($this->dispatcher->getListeners($eventName) as $listener) {
246 $info = $this->getListenerInfo($listener, $eventName);
247 $name = isset($info['class']) ? $info['class'] : $info['type'];
248 $wrappedListener = new WrappedListener($listener, $name, $this->stopwatch, $this);
249 $this->wrappedListeners[$eventName][] = $wrappedListener;
250 $this->dispatcher->removeListener($eventName, $listener);
251 $this->dispatcher->addListener($eventName, $wrappedListener, $info['priority']);
255 private function postProcess($eventName)
257 unset($this->wrappedListeners[$eventName]);
259 foreach ($this->dispatcher->getListeners($eventName) as $listener) {
260 if (!$listener instanceof WrappedListener) { // #12845: a new listener was added during dispatch.
264 $priority = $this->getListenerPriority($eventName, $listener);
265 $this->dispatcher->removeListener($eventName, $listener);
266 $this->dispatcher->addListener($eventName, $listener->getWrappedListener(), $priority);
268 $info = $this->getListenerInfo($listener->getWrappedListener(), $eventName);
269 if ($listener->wasCalled()) {
270 if (null !== $this->logger) {
271 $this->logger->debug(sprintf('Notified event "%s" to listener "%s".', $eventName, $info['pretty']));
274 if (!isset($this->called[$eventName])) {
275 $this->called[$eventName] = new \SplObjectStorage();
278 $this->called[$eventName]->attach($listener);
281 if (null !== $this->logger && $skipped) {
282 $this->logger->debug(sprintf('Listener "%s" was not called for event "%s".', $info['pretty'], $eventName));
285 if ($listener->stoppedPropagation()) {
286 if (null !== $this->logger) {
287 $this->logger->debug(sprintf('Listener "%s" stopped propagation of the event "%s".', $info['pretty'], $eventName));
296 * Returns information about the listener.
298 * @param object $listener The listener
299 * @param string $eventName The event name
301 * @return array Information about the listener
303 private function getListenerInfo($listener, $eventName)
306 'event' => $eventName,
307 'priority' => $this->getListenerPriority($eventName, $listener),
310 // unwrap for correct listener info
311 if ($listener instanceof WrappedListener) {
312 $listener = $listener->getWrappedListener();
315 if ($listener instanceof \Closure) {
318 'pretty' => 'closure',
320 } elseif (is_string($listener)) {
322 $r = new \ReflectionFunction($listener);
323 $file = $r->getFileName();
324 $line = $r->getStartLine();
325 } catch (\ReflectionException $e) {
330 'type' => 'Function',
331 'function' => $listener,
334 'pretty' => $listener,
336 } elseif (is_array($listener) || (is_object($listener) && is_callable($listener))) {
337 if (!is_array($listener)) {
338 $listener = array($listener, '__invoke');
340 $class = is_object($listener[0]) ? get_class($listener[0]) : $listener[0];
342 $r = new \ReflectionMethod($class, $listener[1]);
343 $file = $r->getFileName();
344 $line = $r->getStartLine();
345 } catch (\ReflectionException $e) {
352 'method' => $listener[1],
355 'pretty' => $class.'::'.$listener[1],
362 private function sortListenersByPriority($a, $b)
364 if (is_int($a['priority']) && !is_int($b['priority'])) {
368 if (!is_int($a['priority']) && is_int($b['priority'])) {
372 if ($a['priority'] === $b['priority']) {
376 if ($a['priority'] > $b['priority']) {