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@0: use Symfony\Component\EventDispatcher\EventDispatcherInterface; Chris@0: use Symfony\Component\EventDispatcher\EventSubscriberInterface; Chris@0: use Symfony\Component\EventDispatcher\Event; Chris@0: use Symfony\Component\Stopwatch\Stopwatch; Chris@0: use Psr\Log\LoggerInterface; 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@0: private $called; Chris@0: private $dispatcher; Chris@0: private $wrappedListeners; Chris@0: Chris@0: /** Chris@0: * Constructor. Chris@0: * Chris@0: * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance Chris@0: * @param Stopwatch $stopwatch A Stopwatch instance Chris@0: * @param LoggerInterface $logger A LoggerInterface instance 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@0: $this->called = array(); Chris@0: $this->wrappedListeners = array(); 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@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@0: $this->preDispatch($eventName, $event); Chris@0: Chris@0: $e = $this->stopwatch->start($eventName, 'section'); Chris@0: Chris@0: $this->dispatcher->dispatch($eventName, $event); Chris@0: Chris@0: if ($e->isStarted()) { Chris@0: $e->stop(); Chris@0: } Chris@0: Chris@0: $this->postDispatch($eventName, $event); Chris@0: $this->postProcess($eventName); 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@0: $called = array(); Chris@0: foreach ($this->called as $eventName => $listeners) { Chris@0: foreach ($listeners as $listener) { Chris@0: $called[$eventName.'.'.$listener->getPretty()] = $listener->getInfo($eventName); Chris@0: } 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@0: $this->logger->info('An exception was thrown while getting the uncalled listeners.', array('exception' => $e)); Chris@0: } Chris@0: Chris@0: // unable to retrieve the uncalled listeners Chris@0: return array(); Chris@0: } Chris@0: Chris@0: $notCalled = array(); Chris@0: foreach ($allListeners as $eventName => $listeners) { Chris@0: foreach ($listeners as $listener) { Chris@0: $called = false; Chris@0: if (isset($this->called[$eventName])) { Chris@0: foreach ($this->called[$eventName] as $l) { Chris@0: if ($l->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@0: $notCalled[$eventName.'.'.$listener->getPretty()] = $listener->getInfo($eventName); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: uasort($notCalled, array($this, 'sortListenersByPriority')); Chris@0: Chris@0: return $notCalled; Chris@0: } Chris@0: 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@0: return call_user_func_array(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@0: $wrappedListener = new WrappedListener($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@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@0: $context = array('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@0: Chris@0: if (!isset($this->called[$eventName])) { Chris@0: $this->called[$eventName] = new \SplObjectStorage(); Chris@0: } Chris@0: Chris@0: $this->called[$eventName]->attach($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@0: private function sortListenersByPriority($a, $b) Chris@0: { Chris@0: if (is_int($a['priority']) && !is_int($b['priority'])) { Chris@0: return 1; Chris@0: } Chris@0: Chris@0: 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: }