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

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <[email protected]>
  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 <[email protected]>
  20.  *
  21.  * @final
  22.  */
  23. class EventDataCollector extends DataCollector implements LateDataCollectorInterface
  24. {
  25.     protected $dispatcher;
  26.     private $requestStack;
  27.     private $currentRequest;
  28.     public function __construct(EventDispatcherInterface $dispatcher nullRequestStack $requestStack null)
  29.     {
  30.         $this->dispatcher $dispatcher;
  31.         $this->requestStack $requestStack;
  32.     }
  33.     /**
  34.      * {@inheritdoc}
  35.      */
  36.     public function collect(Request $requestResponse $response\Throwable $exception null)
  37.     {
  38.         $this->currentRequest $this->requestStack && $this->requestStack->getMainRequest() !== $request $request null;
  39.         $this->data = [
  40.             'called_listeners' => [],
  41.             'not_called_listeners' => [],
  42.             'orphaned_events' => [],
  43.         ];
  44.     }
  45.     public function reset()
  46.     {
  47.         $this->data = [];
  48.         if ($this->dispatcher instanceof ResetInterface) {
  49.             $this->dispatcher->reset();
  50.         }
  51.     }
  52.     public function lateCollect()
  53.     {
  54.         if ($this->dispatcher instanceof TraceableEventDispatcher) {
  55.             $this->setCalledListeners($this->dispatcher->getCalledListeners($this->currentRequest));
  56.             $this->setNotCalledListeners($this->dispatcher->getNotCalledListeners($this->currentRequest));
  57.             $this->setOrphanedEvents($this->dispatcher->getOrphanedEvents($this->currentRequest));
  58.         }
  59.         $this->data $this->cloneVar($this->data);
  60.     }
  61.     /**
  62.      * @param array $listeners An array of called listeners
  63.      *
  64.      * @see TraceableEventDispatcher
  65.      */
  66.     public function setCalledListeners(array $listeners)
  67.     {
  68.         $this->data['called_listeners'] = $listeners;
  69.     }
  70.     /**
  71.      * @see TraceableEventDispatcher
  72.      *
  73.      * @return array|Data
  74.      */
  75.     public function getCalledListeners()
  76.     {
  77.         return $this->data['called_listeners'];
  78.     }
  79.     /**
  80.      * @see TraceableEventDispatcher
  81.      */
  82.     public function setNotCalledListeners(array $listeners)
  83.     {
  84.         $this->data['not_called_listeners'] = $listeners;
  85.     }
  86.     /**
  87.      * @see TraceableEventDispatcher
  88.      *
  89.      * @return array|Data
  90.      */
  91.     public function getNotCalledListeners()
  92.     {
  93.         return $this->data['not_called_listeners'];
  94.     }
  95.     /**
  96.      * @param array $events An array of orphaned events
  97.      *
  98.      * @see TraceableEventDispatcher
  99.      */
  100.     public function setOrphanedEvents(array $events)
  101.     {
  102.         $this->data['orphaned_events'] = $events;
  103.     }
  104.     /**
  105.      * @see TraceableEventDispatcher
  106.      *
  107.      * @return array|Data
  108.      */
  109.     public function getOrphanedEvents()
  110.     {
  111.         return $this->data['orphaned_events'];
  112.     }
  113.     /**
  114.      * {@inheritdoc}
  115.      */
  116.     public function getName(): string
  117.     {
  118.         return 'events';
  119.     }
  120. }