vendor/symfony/symfony/src/Symfony/Component/HttpKernel/EventListener/ProfilerListener.php line 113

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