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

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. class ProfilerListener implements EventSubscriberInterface {
  25.     protected $profiler;
  26.     protected $matcher;
  27.     protected $onlyException;
  28.     protected $onlyMasterRequests;
  29.     protected $exception;
  30.     protected $profiles;
  31.     protected $requestStack;
  32.     protected $parents;
  33.     /**
  34.      * @param Profiler                     $profiler           A Profiler instance
  35.      * @param RequestStack                 $requestStack       A RequestStack instance
  36.      * @param RequestMatcherInterface|null $matcher            A RequestMatcher instance
  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.         $this->profiler $profiler;
  42.         $this->matcher $matcher;
  43.         $this->onlyException $onlyException;
  44.         $this->onlyMasterRequests $onlyMasterRequests;
  45.         $this->profiles = new \SplObjectStorage();
  46.         $this->parents = new \SplObjectStorage();
  47.         $this->requestStack $requestStack;
  48.     }
  49.     /**
  50.      * Handles the onKernelException event.
  51.      */
  52.     public function onKernelException(GetResponseForExceptionEvent $event) {
  53.         if ($this->onlyMasterRequests && !$event->isMasterRequest()) {
  54.             return;
  55.         }
  56.         $this->exception $event->getException();
  57.     }
  58.     /**
  59.      * Handles the onKernelResponse event.
  60.      */
  61.     public function onKernelResponse(FilterResponseEvent $event) {
  62.         $master $event->isMasterRequest();
  63.         if ($this->onlyMasterRequests && !$master) {
  64.             return;
  65.         }
  66.         if ($this->onlyException && null === $this->exception) {
  67.             return;
  68.         }
  69.         $request $event->getRequest();
  70.         $exception $this->exception;
  71.         $this->exception null;
  72.         if (null !== $this->matcher && !$this->matcher->matches($request)) {
  73.             return;
  74.         }
  75.         if (!$profile $this->profiler->collect($request$event->getResponse(), $exception)) {
  76.             return;
  77.         }
  78.         $this->profiles[$request] = $profile;
  79.         $this->parents[$request] = $this->requestStack->getParentRequest();
  80.     }
  81.     public function onKernelTerminate(PostResponseEvent $event) {
  82.         // attach children to parents
  83.         foreach ($this->profiles as $request) {
  84.             if (null !== $parentRequest $this->parents[$request]) {
  85.                 if (isset($this->profiles[$parentRequest])) {
  86.                     $this->profiles[$parentRequest]->addChild($this->profiles[$request]);
  87.                 }
  88.             }
  89.         }
  90.         // save profiles
  91.         foreach ($this->profiles as $request) {
  92.             $this->profiler->saveProfile($this->profiles[$request]);
  93.         }
  94.         $this->profiles = new \SplObjectStorage();
  95.         $this->parents = new \SplObjectStorage();
  96.     }
  97.     public static function getSubscribedEvents() {
  98.         return [
  99.             KernelEvents::RESPONSE => ['onKernelResponse', -100],
  100.             KernelEvents::EXCEPTION => ['onKernelException'0],
  101.             KernelEvents::TERMINATE => ['onKernelTerminate', -1024],
  102.         ];
  103.     }
  104. }