vendor/symfony/http-kernel/EventListener/ProfilerListener.php line 112

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\HttpKernel\EventListener;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\RequestMatcherInterface;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  15. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  16. use Symfony\Component\HttpKernel\Event\TerminateEvent;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. use Symfony\Component\HttpKernel\Profiler\Profiler;
  19. /**
  20.  * ProfilerListener collects data for the current request by listening to the kernel events.
  21.  *
  22.  * @author Fabien Potencier <fabien@symfony.com>
  23.  *
  24.  * @final
  25.  */
  26. class ProfilerListener implements EventSubscriberInterface
  27. {
  28.     protected $profiler;
  29.     protected $matcher;
  30.     protected $onlyException;
  31.     protected $onlyMasterRequests;
  32.     protected $exception;
  33.     protected $profiles;
  34.     protected $requestStack;
  35.     protected $parents;
  36.     /**
  37.      * @param bool $onlyException      True if the profiler only collects data when an exception occurs, false otherwise
  38.      * @param bool $onlyMasterRequests True if the profiler only collects data when the request is a master request, false otherwise
  39.      */
  40.     public function __construct(Profiler $profilerRequestStack $requestStackRequestMatcherInterface $matcher nullbool $onlyException falsebool $onlyMasterRequests false)
  41.     {
  42.         $this->profiler $profiler;
  43.         $this->matcher $matcher;
  44.         $this->onlyException $onlyException;
  45.         $this->onlyMasterRequests $onlyMasterRequests;
  46.         $this->profiles = new \SplObjectStorage();
  47.         $this->parents = new \SplObjectStorage();
  48.         $this->requestStack $requestStack;
  49.     }
  50.     /**
  51.      * Handles the onKernelException event.
  52.      */
  53.     public function onKernelException(ExceptionEvent $event)
  54.     {
  55.         if ($this->onlyMasterRequests && !$event->isMasterRequest()) {
  56.             return;
  57.         }
  58.         $this->exception $event->getThrowable();
  59.     }
  60.     /**
  61.      * Handles the onKernelResponse event.
  62.      */
  63.     public function onKernelResponse(ResponseEvent $event)
  64.     {
  65.         $master $event->isMasterRequest();
  66.         if ($this->onlyMasterRequests && !$master) {
  67.             return;
  68.         }
  69.         if ($this->onlyException && null === $this->exception) {
  70.             return;
  71.         }
  72.         $request $event->getRequest();
  73.         $exception $this->exception;
  74.         $this->exception null;
  75.         if (null !== $this->matcher && !$this->matcher->matches($request)) {
  76.             return;
  77.         }
  78.         if (!$profile $this->profiler->collect($request$event->getResponse(), $exception)) {
  79.             return;
  80.         }
  81.         $this->profiles[$request] = $profile;
  82.         $this->parents[$request] = $this->requestStack->getParentRequest();
  83.     }
  84.     public function onKernelTerminate(TerminateEvent $event)
  85.     {
  86.         // attach children to parents
  87.         foreach ($this->profiles as $request) {
  88.             if (null !== $parentRequest $this->parents[$request]) {
  89.                 if (isset($this->profiles[$parentRequest])) {
  90.                     $this->profiles[$parentRequest]->addChild($this->profiles[$request]);
  91.                 }
  92.             }
  93.         }
  94.         // save profiles
  95.         foreach ($this->profiles as $request) {
  96.             $this->profiler->saveProfile($this->profiles[$request]);
  97.         }
  98.         $this->profiles = new \SplObjectStorage();
  99.         $this->parents = new \SplObjectStorage();
  100.     }
  101.     public static function getSubscribedEvents(): array
  102.     {
  103.         return [
  104.             KernelEvents::RESPONSE => ['onKernelResponse', -100],
  105.             KernelEvents::EXCEPTION => ['onKernelException'0],
  106.             KernelEvents::TERMINATE => ['onKernelTerminate', -1024],
  107.         ];
  108.     }
  109. }