vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php line 135

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