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

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