vendor/symfony/http-kernel/DataCollector/EventDataCollector.php line 64

  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\HttpKernel\DataCollector;
  11. use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\VarDumper\Cloner\Data;
  16. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  17. use Symfony\Contracts\Service\ResetInterface;
  18. /**
  19.  * @author Fabien Potencier <fabien@symfony.com>
  20.  *
  21.  * @see TraceableEventDispatcher
  22.  *
  23.  * @final
  24.  */
  25. class EventDataCollector extends DataCollector implements LateDataCollectorInterface
  26. {
  27.     private ?EventDispatcherInterface $dispatcher;
  28.     private ?RequestStack $requestStack;
  29.     private ?Request $currentRequest null;
  30.     public function __construct(EventDispatcherInterface $dispatcher nullRequestStack $requestStack null)
  31.     {
  32.         $this->dispatcher $dispatcher;
  33.         $this->requestStack $requestStack;
  34.     }
  35.     public function collect(Request $requestResponse $response\Throwable $exception null)
  36.     {
  37.         $this->currentRequest $this->requestStack && $this->requestStack->getMainRequest() !== $request $request null;
  38.         $this->data = [
  39.             'called_listeners' => [],
  40.             'not_called_listeners' => [],
  41.             'orphaned_events' => [],
  42.         ];
  43.     }
  44.     public function reset()
  45.     {
  46.         $this->data = [];
  47.         if ($this->dispatcher instanceof ResetInterface) {
  48.             $this->dispatcher->reset();
  49.         }
  50.     }
  51.     public function lateCollect()
  52.     {
  53.         if ($this->dispatcher instanceof TraceableEventDispatcher) {
  54.             $this->setCalledListeners($this->dispatcher->getCalledListeners($this->currentRequest));
  55.             $this->setNotCalledListeners($this->dispatcher->getNotCalledListeners($this->currentRequest));
  56.             $this->setOrphanedEvents($this->dispatcher->getOrphanedEvents($this->currentRequest));
  57.         }
  58.         $this->data $this->cloneVar($this->data);
  59.     }
  60.     /**
  61.      * @see TraceableEventDispatcher
  62.      */
  63.     public function setCalledListeners(array $listeners)
  64.     {
  65.         $this->data['called_listeners'] = $listeners;
  66.     }
  67.     /**
  68.      * @see TraceableEventDispatcher
  69.      */
  70.     public function getCalledListeners(): array|Data
  71.     {
  72.         return $this->data['called_listeners'];
  73.     }
  74.     /**
  75.      * @see TraceableEventDispatcher
  76.      */
  77.     public function setNotCalledListeners(array $listeners)
  78.     {
  79.         $this->data['not_called_listeners'] = $listeners;
  80.     }
  81.     /**
  82.      * @see TraceableEventDispatcher
  83.      */
  84.     public function getNotCalledListeners(): array|Data
  85.     {
  86.         return $this->data['not_called_listeners'];
  87.     }
  88.     /**
  89.      * @param array $events An array of orphaned events
  90.      *
  91.      * @see TraceableEventDispatcher
  92.      */
  93.     public function setOrphanedEvents(array $events)
  94.     {
  95.         $this->data['orphaned_events'] = $events;
  96.     }
  97.     /**
  98.      * @see TraceableEventDispatcher
  99.      */
  100.     public function getOrphanedEvents(): array|Data
  101.     {
  102.         return $this->data['orphaned_events'];
  103.     }
  104.     public function getName(): string
  105.     {
  106.         return 'events';
  107.     }
  108. }