Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\EventDispatcher\Debug; Chris@0: Chris@17: use Psr\Log\LoggerInterface; Chris@17: use Symfony\Component\EventDispatcher\Event; Chris@0: use Symfony\Component\EventDispatcher\EventDispatcherInterface; Chris@0: use Symfony\Component\EventDispatcher\EventSubscriberInterface; Chris@0: use Symfony\Component\Stopwatch\Stopwatch; Chris@0: Chris@0: /** Chris@0: * Collects some data about event listeners. Chris@0: * Chris@0: * This event dispatcher delegates the dispatching to another one. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class TraceableEventDispatcher implements TraceableEventDispatcherInterface Chris@0: { Chris@0: protected $logger; Chris@0: protected $stopwatch; Chris@0: Chris@17: private $callStack; Chris@0: private $dispatcher; Chris@0: private $wrappedListeners; Chris@0: Chris@0: public function __construct(EventDispatcherInterface $dispatcher, Stopwatch $stopwatch, LoggerInterface $logger = null) Chris@0: { Chris@0: $this->dispatcher = $dispatcher; Chris@0: $this->stopwatch = $stopwatch; Chris@0: $this->logger = $logger; Chris@17: $this->wrappedListeners = []; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function addListener($eventName, $listener, $priority = 0) Chris@0: { Chris@0: $this->dispatcher->addListener($eventName, $listener, $priority); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function addSubscriber(EventSubscriberInterface $subscriber) Chris@0: { Chris@0: $this->dispatcher->addSubscriber($subscriber); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function removeListener($eventName, $listener) Chris@0: { Chris@0: if (isset($this->wrappedListeners[$eventName])) { Chris@0: foreach ($this->wrappedListeners[$eventName] as $index => $wrappedListener) { Chris@0: if ($wrappedListener->getWrappedListener() === $listener) { Chris@0: $listener = $wrappedListener; Chris@0: unset($this->wrappedListeners[$eventName][$index]); Chris@0: break; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return $this->dispatcher->removeListener($eventName, $listener); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function removeSubscriber(EventSubscriberInterface $subscriber) Chris@0: { Chris@0: return $this->dispatcher->removeSubscriber($subscriber); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getListeners($eventName = null) Chris@0: { Chris@0: return $this->dispatcher->getListeners($eventName); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getListenerPriority($eventName, $listener) Chris@0: { Chris@0: // we might have wrapped listeners for the event (if called while dispatching) Chris@0: // in that case get the priority by wrapper Chris@0: if (isset($this->wrappedListeners[$eventName])) { Chris@0: foreach ($this->wrappedListeners[$eventName] as $index => $wrappedListener) { Chris@0: if ($wrappedListener->getWrappedListener() === $listener) { Chris@0: return $this->dispatcher->getListenerPriority($eventName, $wrappedListener); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return $this->dispatcher->getListenerPriority($eventName, $listener); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function hasListeners($eventName = null) Chris@0: { Chris@0: return $this->dispatcher->hasListeners($eventName); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function dispatch($eventName, Event $event = null) Chris@0: { Chris@17: if (null === $this->callStack) { Chris@17: $this->callStack = new \SplObjectStorage(); Chris@17: } Chris@17: Chris@0: if (null === $event) { Chris@0: $event = new Event(); Chris@0: } Chris@0: Chris@0: if (null !== $this->logger && $event->isPropagationStopped()) { Chris@0: $this->logger->debug(sprintf('The "%s" event is already stopped. No listeners have been called.', $eventName)); Chris@0: } Chris@0: Chris@0: $this->preProcess($eventName); Chris@17: try { Chris@17: $this->preDispatch($eventName, $event); Chris@17: try { Chris@17: $e = $this->stopwatch->start($eventName, 'section'); Chris@17: try { Chris@17: $this->dispatcher->dispatch($eventName, $event); Chris@17: } finally { Chris@17: if ($e->isStarted()) { Chris@17: $e->stop(); Chris@17: } Chris@17: } Chris@17: } finally { Chris@17: $this->postDispatch($eventName, $event); Chris@17: } Chris@17: } finally { Chris@17: $this->postProcess($eventName); Chris@0: } Chris@0: Chris@0: return $event; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getCalledListeners() Chris@0: { Chris@17: if (null === $this->callStack) { Chris@17: return []; Chris@17: } Chris@17: Chris@17: $called = []; Chris@17: foreach ($this->callStack as $listener) { Chris@17: list($eventName) = $this->callStack->getInfo(); Chris@17: Chris@17: $called[] = $listener->getInfo($eventName); Chris@0: } Chris@0: Chris@0: return $called; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getNotCalledListeners() Chris@0: { Chris@0: try { Chris@0: $allListeners = $this->getListeners(); Chris@0: } catch (\Exception $e) { Chris@0: if (null !== $this->logger) { Chris@17: $this->logger->info('An exception was thrown while getting the uncalled listeners.', ['exception' => $e]); Chris@0: } Chris@0: Chris@0: // unable to retrieve the uncalled listeners Chris@17: return []; Chris@0: } Chris@0: Chris@17: $notCalled = []; Chris@0: foreach ($allListeners as $eventName => $listeners) { Chris@0: foreach ($listeners as $listener) { Chris@0: $called = false; Chris@17: if (null !== $this->callStack) { Chris@17: foreach ($this->callStack as $calledListener) { Chris@17: if ($calledListener->getWrappedListener() === $listener) { Chris@0: $called = true; Chris@0: Chris@0: break; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: if (!$called) { Chris@0: if (!$listener instanceof WrappedListener) { Chris@0: $listener = new WrappedListener($listener, null, $this->stopwatch, $this); Chris@0: } Chris@17: $notCalled[] = $listener->getInfo($eventName); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@17: uasort($notCalled, [$this, 'sortNotCalledListeners']); Chris@0: Chris@0: return $notCalled; Chris@0: } Chris@0: Chris@14: public function reset() Chris@14: { Chris@17: $this->callStack = null; Chris@14: } Chris@14: Chris@0: /** Chris@0: * Proxies all method calls to the original event dispatcher. Chris@0: * Chris@0: * @param string $method The method name Chris@0: * @param array $arguments The method arguments Chris@0: * Chris@0: * @return mixed Chris@0: */ Chris@0: public function __call($method, $arguments) Chris@0: { Chris@17: return \call_user_func_array([$this->dispatcher, $method], $arguments); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Called before dispatching the event. Chris@0: * Chris@0: * @param string $eventName The event name Chris@0: * @param Event $event The event Chris@0: */ Chris@0: protected function preDispatch($eventName, Event $event) Chris@0: { Chris@0: } Chris@0: Chris@0: /** Chris@0: * Called after dispatching the event. Chris@0: * Chris@0: * @param string $eventName The event name Chris@0: * @param Event $event The event Chris@0: */ Chris@0: protected function postDispatch($eventName, Event $event) Chris@0: { Chris@0: } Chris@0: Chris@0: private function preProcess($eventName) Chris@0: { Chris@0: foreach ($this->dispatcher->getListeners($eventName) as $listener) { Chris@0: $priority = $this->getListenerPriority($eventName, $listener); Chris@17: $wrappedListener = new WrappedListener($listener instanceof WrappedListener ? $listener->getWrappedListener() : $listener, null, $this->stopwatch, $this); Chris@0: $this->wrappedListeners[$eventName][] = $wrappedListener; Chris@0: $this->dispatcher->removeListener($eventName, $listener); Chris@0: $this->dispatcher->addListener($eventName, $wrappedListener, $priority); Chris@17: $this->callStack->attach($wrappedListener, [$eventName]); Chris@0: } Chris@0: } Chris@0: Chris@0: private function postProcess($eventName) Chris@0: { Chris@0: unset($this->wrappedListeners[$eventName]); Chris@0: $skipped = false; Chris@0: foreach ($this->dispatcher->getListeners($eventName) as $listener) { Chris@0: if (!$listener instanceof WrappedListener) { // #12845: a new listener was added during dispatch. Chris@0: continue; Chris@0: } Chris@0: // Unwrap listener Chris@0: $priority = $this->getListenerPriority($eventName, $listener); Chris@0: $this->dispatcher->removeListener($eventName, $listener); Chris@0: $this->dispatcher->addListener($eventName, $listener->getWrappedListener(), $priority); Chris@0: Chris@0: if (null !== $this->logger) { Chris@17: $context = ['event' => $eventName, 'listener' => $listener->getPretty()]; Chris@0: } Chris@0: Chris@0: if ($listener->wasCalled()) { Chris@0: if (null !== $this->logger) { Chris@0: $this->logger->debug('Notified event "{event}" to listener "{listener}".', $context); Chris@0: } Chris@17: } else { Chris@17: $this->callStack->detach($listener); Chris@0: } Chris@0: Chris@0: if (null !== $this->logger && $skipped) { Chris@0: $this->logger->debug('Listener "{listener}" was not called for event "{event}".', $context); Chris@0: } Chris@0: Chris@0: if ($listener->stoppedPropagation()) { Chris@0: if (null !== $this->logger) { Chris@0: $this->logger->debug('Listener "{listener}" stopped propagation of the event "{event}".', $context); Chris@0: } Chris@0: Chris@0: $skipped = true; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@17: private function sortNotCalledListeners(array $a, array $b) Chris@0: { Chris@17: if (0 !== $cmp = strcmp($a['event'], $b['event'])) { Chris@17: return $cmp; Chris@17: } Chris@17: Chris@17: if (\is_int($a['priority']) && !\is_int($b['priority'])) { Chris@0: return 1; Chris@0: } Chris@0: Chris@17: if (!\is_int($a['priority']) && \is_int($b['priority'])) { Chris@0: return -1; Chris@0: } Chris@0: Chris@0: if ($a['priority'] === $b['priority']) { Chris@0: return 0; Chris@0: } Chris@0: Chris@0: if ($a['priority'] > $b['priority']) { Chris@0: return -1; Chris@0: } Chris@0: Chris@0: return 1; Chris@0: } Chris@0: }