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 // we might have wrapped listeners for the event (if called while dispatching)
108 // in that case get the priority by wrapper
109 if (isset($this->wrappedListeners[$eventName])) {
110 foreach ($this->wrappedListeners[$eventName] as $index => $wrappedListener) {
111 if ($wrappedListener->getWrappedListener() === $listener) {
112 return $this->dispatcher->getListenerPriority($eventName, $wrappedListener);
117 return $this->dispatcher->getListenerPriority($eventName, $listener);
123 public function hasListeners($eventName = null)
125 return $this->dispatcher->hasListeners($eventName);
131 public function dispatch($eventName, Event $event = null)
133 if (null === $event) {
134 $event = new Event();
137 if (null !== $this->logger && $event->isPropagationStopped()) {
138 $this->logger->debug(sprintf('The "%s" event is already stopped. No listeners have been called.', $eventName));
141 $this->preProcess($eventName);
142 $this->preDispatch($eventName, $event);
144 $e = $this->stopwatch->start($eventName, 'section');
146 $this->dispatcher->dispatch($eventName, $event);
148 if ($e->isStarted()) {
152 $this->postDispatch($eventName, $event);
153 $this->postProcess($eventName);
161 public function getCalledListeners()
164 foreach ($this->called as $eventName => $listeners) {
165 foreach ($listeners as $listener) {
166 $called[$eventName.'.'.$listener->getPretty()] = $listener->getInfo($eventName);
176 public function getNotCalledListeners()
179 $allListeners = $this->getListeners();
180 } catch (\Exception $e) {
181 if (null !== $this->logger) {
182 $this->logger->info('An exception was thrown while getting the uncalled listeners.', array('exception' => $e));
185 // unable to retrieve the uncalled listeners
189 $notCalled = array();
190 foreach ($allListeners as $eventName => $listeners) {
191 foreach ($listeners as $listener) {
193 if (isset($this->called[$eventName])) {
194 foreach ($this->called[$eventName] as $l) {
195 if ($l->getWrappedListener() === $listener) {
204 if (!$listener instanceof WrappedListener) {
205 $listener = new WrappedListener($listener, null, $this->stopwatch, $this);
207 $notCalled[$eventName.'.'.$listener->getPretty()] = $listener->getInfo($eventName);
212 uasort($notCalled, array($this, 'sortListenersByPriority'));
218 * Proxies all method calls to the original event dispatcher.
220 * @param string $method The method name
221 * @param array $arguments The method arguments
225 public function __call($method, $arguments)
227 return call_user_func_array(array($this->dispatcher, $method), $arguments);
231 * Called before dispatching the event.
233 * @param string $eventName The event name
234 * @param Event $event The event
236 protected function preDispatch($eventName, Event $event)
241 * Called after dispatching the event.
243 * @param string $eventName The event name
244 * @param Event $event The event
246 protected function postDispatch($eventName, Event $event)
250 private function preProcess($eventName)
252 foreach ($this->dispatcher->getListeners($eventName) as $listener) {
253 $priority = $this->getListenerPriority($eventName, $listener);
254 $wrappedListener = new WrappedListener($listener, null, $this->stopwatch, $this);
255 $this->wrappedListeners[$eventName][] = $wrappedListener;
256 $this->dispatcher->removeListener($eventName, $listener);
257 $this->dispatcher->addListener($eventName, $wrappedListener, $priority);
261 private function postProcess($eventName)
263 unset($this->wrappedListeners[$eventName]);
265 foreach ($this->dispatcher->getListeners($eventName) as $listener) {
266 if (!$listener instanceof WrappedListener) { // #12845: a new listener was added during dispatch.
270 $priority = $this->getListenerPriority($eventName, $listener);
271 $this->dispatcher->removeListener($eventName, $listener);
272 $this->dispatcher->addListener($eventName, $listener->getWrappedListener(), $priority);
274 if (null !== $this->logger) {
275 $context = array('event' => $eventName, 'listener' => $listener->getPretty());
278 if ($listener->wasCalled()) {
279 if (null !== $this->logger) {
280 $this->logger->debug('Notified event "{event}" to listener "{listener}".', $context);
283 if (!isset($this->called[$eventName])) {
284 $this->called[$eventName] = new \SplObjectStorage();
287 $this->called[$eventName]->attach($listener);
290 if (null !== $this->logger && $skipped) {
291 $this->logger->debug('Listener "{listener}" was not called for event "{event}".', $context);
294 if ($listener->stoppedPropagation()) {
295 if (null !== $this->logger) {
296 $this->logger->debug('Listener "{listener}" stopped propagation of the event "{event}".', $context);
304 private function sortListenersByPriority($a, $b)
306 if (is_int($a['priority']) && !is_int($b['priority'])) {
310 if (!is_int($a['priority']) && is_int($b['priority'])) {
314 if ($a['priority'] === $b['priority']) {
318 if ($a['priority'] > $b['priority']) {