vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php line 139

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\EventDispatcher\Debug;
  11. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\EventDispatcher\Event;
  14. use Symfony\Component\Stopwatch\Stopwatch;
  15. use Psr\Log\LoggerInterface;
  16. /**
  17.  * Collects some data about event listeners.
  18.  *
  19.  * This event dispatcher delegates the dispatching to another one.
  20.  *
  21.  * @author Fabien Potencier <fabien@symfony.com>
  22.  */
  23. class TraceableEventDispatcher implements TraceableEventDispatcherInterface
  24. {
  25.     protected $logger;
  26.     protected $stopwatch;
  27.     private $called;
  28.     private $dispatcher;
  29.     private $wrappedListeners;
  30.     public function __construct(EventDispatcherInterface $dispatcherStopwatch $stopwatchLoggerInterface $logger null)
  31.     {
  32.         $this->dispatcher $dispatcher;
  33.         $this->stopwatch $stopwatch;
  34.         $this->logger $logger;
  35.         $this->called = array();
  36.         $this->wrappedListeners = array();
  37.     }
  38.     /**
  39.      * {@inheritdoc}
  40.      */
  41.     public function addListener($eventName$listener$priority 0)
  42.     {
  43.         $this->dispatcher->addListener($eventName$listener$priority);
  44.     }
  45.     /**
  46.      * {@inheritdoc}
  47.      */
  48.     public function addSubscriber(EventSubscriberInterface $subscriber)
  49.     {
  50.         $this->dispatcher->addSubscriber($subscriber);
  51.     }
  52.     /**
  53.      * {@inheritdoc}
  54.      */
  55.     public function removeListener($eventName$listener)
  56.     {
  57.         if (isset($this->wrappedListeners[$eventName])) {
  58.             foreach ($this->wrappedListeners[$eventName] as $index => $wrappedListener) {
  59.                 if ($wrappedListener->getWrappedListener() === $listener) {
  60.                     $listener $wrappedListener;
  61.                     unset($this->wrappedListeners[$eventName][$index]);
  62.                     break;
  63.                 }
  64.             }
  65.         }
  66.         return $this->dispatcher->removeListener($eventName$listener);
  67.     }
  68.     /**
  69.      * {@inheritdoc}
  70.      */
  71.     public function removeSubscriber(EventSubscriberInterface $subscriber)
  72.     {
  73.         return $this->dispatcher->removeSubscriber($subscriber);
  74.     }
  75.     /**
  76.      * {@inheritdoc}
  77.      */
  78.     public function getListeners($eventName null)
  79.     {
  80.         return $this->dispatcher->getListeners($eventName);
  81.     }
  82.     /**
  83.      * {@inheritdoc}
  84.      */
  85.     public function getListenerPriority($eventName$listener)
  86.     {
  87.         // we might have wrapped listeners for the event (if called while dispatching)
  88.         // in that case get the priority by wrapper
  89.         if (isset($this->wrappedListeners[$eventName])) {
  90.             foreach ($this->wrappedListeners[$eventName] as $index => $wrappedListener) {
  91.                 if ($wrappedListener->getWrappedListener() === $listener) {
  92.                     return $this->dispatcher->getListenerPriority($eventName$wrappedListener);
  93.                 }
  94.             }
  95.         }
  96.         return $this->dispatcher->getListenerPriority($eventName$listener);
  97.     }
  98.     /**
  99.      * {@inheritdoc}
  100.      */
  101.     public function hasListeners($eventName null)
  102.     {
  103.         return $this->dispatcher->hasListeners($eventName);
  104.     }
  105.     /**
  106.      * {@inheritdoc}
  107.      */
  108.     public function dispatch($eventNameEvent $event null)
  109.     {
  110.         if (null === $event) {
  111.             $event = new Event();
  112.         }
  113.         if (null !== $this->logger && $event->isPropagationStopped()) {
  114.             $this->logger->debug(sprintf('The "%s" event is already stopped. No listeners have been called.'$eventName));
  115.         }
  116.         $this->preProcess($eventName);
  117.         $this->preDispatch($eventName$event);
  118.         $e $this->stopwatch->start($eventName'section');
  119.         $this->dispatcher->dispatch($eventName$event);
  120.         if ($e->isStarted()) {
  121.             $e->stop();
  122.         }
  123.         $this->postDispatch($eventName$event);
  124.         $this->postProcess($eventName);
  125.         return $event;
  126.     }
  127.     /**
  128.      * {@inheritdoc}
  129.      */
  130.     public function getCalledListeners()
  131.     {
  132.         $called = array();
  133.         foreach ($this->called as $eventName => $listeners) {
  134.             foreach ($listeners as $listener) {
  135.                 $called[$eventName.'.'.$listener->getPretty()] = $listener->getInfo($eventName);
  136.             }
  137.         }
  138.         return $called;
  139.     }
  140.     /**
  141.      * {@inheritdoc}
  142.      */
  143.     public function getNotCalledListeners()
  144.     {
  145.         try {
  146.             $allListeners $this->getListeners();
  147.         } catch (\Exception $e) {
  148.             if (null !== $this->logger) {
  149.                 $this->logger->info('An exception was thrown while getting the uncalled listeners.', array('exception' => $e));
  150.             }
  151.             // unable to retrieve the uncalled listeners
  152.             return array();
  153.         }
  154.         $notCalled = array();
  155.         foreach ($allListeners as $eventName => $listeners) {
  156.             foreach ($listeners as $listener) {
  157.                 $called false;
  158.                 if (isset($this->called[$eventName])) {
  159.                     foreach ($this->called[$eventName] as $l) {
  160.                         if ($l->getWrappedListener() === $listener) {
  161.                             $called true;
  162.                             break;
  163.                         }
  164.                     }
  165.                 }
  166.                 if (!$called) {
  167.                     if (!$listener instanceof WrappedListener) {
  168.                         $listener = new WrappedListener($listenernull$this->stopwatch$this);
  169.                     }
  170.                     $notCalled[$eventName.'.'.$listener->getPretty()] = $listener->getInfo($eventName);
  171.                 }
  172.             }
  173.         }
  174.         uasort($notCalled, array($this'sortListenersByPriority'));
  175.         return $notCalled;
  176.     }
  177.     /**
  178.      * Proxies all method calls to the original event dispatcher.
  179.      *
  180.      * @param string $method    The method name
  181.      * @param array  $arguments The method arguments
  182.      *
  183.      * @return mixed
  184.      */
  185.     public function __call($method$arguments)
  186.     {
  187.         return call_user_func_array(array($this->dispatcher$method), $arguments);
  188.     }
  189.     /**
  190.      * Called before dispatching the event.
  191.      *
  192.      * @param string $eventName The event name
  193.      * @param Event  $event     The event
  194.      */
  195.     protected function preDispatch($eventNameEvent $event)
  196.     {
  197.     }
  198.     /**
  199.      * Called after dispatching the event.
  200.      *
  201.      * @param string $eventName The event name
  202.      * @param Event  $event     The event
  203.      */
  204.     protected function postDispatch($eventNameEvent $event)
  205.     {
  206.     }
  207.     private function preProcess($eventName)
  208.     {
  209.         foreach ($this->dispatcher->getListeners($eventName) as $listener) {
  210.             $priority $this->getListenerPriority($eventName$listener);
  211.             $wrappedListener = new WrappedListener($listenernull$this->stopwatch$this);
  212.             $this->wrappedListeners[$eventName][] = $wrappedListener;
  213.             $this->dispatcher->removeListener($eventName$listener);
  214.             $this->dispatcher->addListener($eventName$wrappedListener$priority);
  215.         }
  216.     }
  217.     private function postProcess($eventName)
  218.     {
  219.         unset($this->wrappedListeners[$eventName]);
  220.         $skipped false;
  221.         foreach ($this->dispatcher->getListeners($eventName) as $listener) {
  222.             if (!$listener instanceof WrappedListener) { // #12845: a new listener was added during dispatch.
  223.                 continue;
  224.             }
  225.             // Unwrap listener
  226.             $priority $this->getListenerPriority($eventName$listener);
  227.             $this->dispatcher->removeListener($eventName$listener);
  228.             $this->dispatcher->addListener($eventName$listener->getWrappedListener(), $priority);
  229.             if (null !== $this->logger) {
  230.                 $context = array('event' => $eventName'listener' => $listener->getPretty());
  231.             }
  232.             if ($listener->wasCalled()) {
  233.                 if (null !== $this->logger) {
  234.                     $this->logger->debug('Notified event "{event}" to listener "{listener}".'$context);
  235.                 }
  236.                 if (!isset($this->called[$eventName])) {
  237.                     $this->called[$eventName] = new \SplObjectStorage();
  238.                 }
  239.                 $this->called[$eventName]->attach($listener);
  240.             }
  241.             if (null !== $this->logger && $skipped) {
  242.                 $this->logger->debug('Listener "{listener}" was not called for event "{event}".'$context);
  243.             }
  244.             if ($listener->stoppedPropagation()) {
  245.                 if (null !== $this->logger) {
  246.                     $this->logger->debug('Listener "{listener}" stopped propagation of the event "{event}".'$context);
  247.                 }
  248.                 $skipped true;
  249.             }
  250.         }
  251.     }
  252.     private function sortListenersByPriority($a$b)
  253.     {
  254.         if (is_int($a['priority']) && !is_int($b['priority'])) {
  255.             return 1;
  256.         }
  257.         if (!is_int($a['priority']) && is_int($b['priority'])) {
  258.             return -1;
  259.         }
  260.         if ($a['priority'] === $b['priority']) {
  261.             return 0;
  262.         }
  263.         if ($a['priority'] > $b['priority']) {
  264.             return -1;
  265.         }
  266.         return 1;
  267.     }
  268. }