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

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\FilterResponseEvent;
  15. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  16. use Symfony\Component\HttpKernel\Event\PostResponseEvent;
  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 since Symfony 4.3
  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 Profiler                     $profiler           A Profiler instance
  38.      * @param RequestStack                 $requestStack       A RequestStack instance
  39.      * @param RequestMatcherInterface|null $matcher            A RequestMatcher instance
  40.      * @param bool                         $onlyException      True if the profiler only collects data when an exception occurs, false otherwise
  41.      * @param bool                         $onlyMasterRequests True if the profiler only collects data when the request is a master request, false otherwise
  42.      */
  43.     public function __construct(Profiler $profilerRequestStack $requestStackRequestMatcherInterface $matcher nullbool $onlyException falsebool $onlyMasterRequests false)
  44.     {
  45.         $this->profiler $profiler;
  46.         $this->matcher $matcher;
  47.         $this->onlyException $onlyException;
  48.         $this->onlyMasterRequests $onlyMasterRequests;
  49.         $this->profiles = new \SplObjectStorage();
  50.         $this->parents = new \SplObjectStorage();
  51.         $this->requestStack $requestStack;
  52.     }
  53.     /**
  54.      * Handles the onKernelException event.
  55.      */
  56.     public function onKernelException(GetResponseForExceptionEvent $event)
  57.     {
  58.         if ($this->onlyMasterRequests && !$event->isMasterRequest()) {
  59.             return;
  60.         }
  61.         $this->exception $event->getException();
  62.     }
  63.     /**
  64.      * Handles the onKernelResponse event.
  65.      */
  66.     public function onKernelResponse(FilterResponseEvent $event)
  67.     {
  68.         $master $event->isMasterRequest();
  69.         if ($this->onlyMasterRequests && !$master) {
  70.             return;
  71.         }
  72.         if ($this->onlyException && null === $this->exception) {
  73.             return;
  74.         }
  75.         $request $event->getRequest();
  76.         $exception $this->exception;
  77.         $this->exception null;
  78.         if (null !== $this->matcher && !$this->matcher->matches($request)) {
  79.             return;
  80.         }
  81.         if (!$profile $this->profiler->collect($request$event->getResponse(), $exception)) {
  82.             return;
  83.         }
  84.         $this->profiles[$request] = $profile;
  85.         $this->parents[$request] = $this->requestStack->getParentRequest();
  86.     }
  87.     public function onKernelTerminate(PostResponseEvent $event)
  88.     {
  89.         // attach children to parents
  90.         foreach ($this->profiles as $request) {
  91.             if (null !== $parentRequest $this->parents[$request]) {
  92.                 if (isset($this->profiles[$parentRequest])) {
  93.                     $this->profiles[$parentRequest]->addChild($this->profiles[$request]);
  94.                 }
  95.             }
  96.         }
  97.         // save profiles
  98.         foreach ($this->profiles as $request) {
  99.             $this->profiler->saveProfile($this->profiles[$request]);
  100.         }
  101.         $this->profiles = new \SplObjectStorage();
  102.         $this->parents = new \SplObjectStorage();
  103.     }
  104.     public static function getSubscribedEvents()
  105.     {
  106.         return [
  107.             KernelEvents::RESPONSE => ['onKernelResponse', -100],
  108.             KernelEvents::EXCEPTION => ['onKernelException'0],
  109.             KernelEvents::TERMINATE => ['onKernelTerminate', -1024],
  110.         ];
  111.     }
  112. }