app/Service/EventSubscriber/MainRequestProfileSubscriber.php line 55

Open in your IDE?
  1. <?php
  2. namespace Sq\Service\EventSubscriber;
  3. use Sq\Event\Kernel\InitialiseContainerEvent;
  4. use Sq\Service\Profiler\ProfilerInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\Event;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. class MainRequestProfileSubscriber implements EventSubscriberInterface
  9. {
  10.     /** @var \Sq\Service\Profiler\ProfilerInterface */
  11.     private $profiler;
  12.     /** @var \Sq\Entity\Profiler\ProfileInterface */
  13.     private $mainProfile;
  14.     /**
  15.      * @param \Sq\Service\Profiler\ProfilerInterface $profiler
  16.      */
  17.     public function __construct(ProfilerInterface $profiler)
  18.     {
  19.         $this->profiler $profiler;
  20.     }
  21.     /** @inheritDoc */
  22.     public static function getSubscribedEvents()
  23.     {
  24.         return [
  25.             InitialiseContainerEvent::class => ['onInitializeContainer'],
  26.             // For the request and controller events, set the priority really low so that they are executed after the
  27.             // route and controller information has been calculated by first-party Symfony event listeners/subscribers.
  28.             KernelEvents::REQUEST => ['onRequest', -64],
  29.             KernelEvents::CONTROLLER => ['onController', -64],
  30.             KernelEvents::TERMINATE => ['onTerminate'],
  31.         ];
  32.     }
  33.     /**
  34.      * Start profiling the "main" application as soon as the container has been initialized.
  35.      *
  36.      * @param \Sq\Event\Kernel\InitialiseContainerEvent $event
  37.      */
  38.     public function onInitializeContainer(InitialiseContainerEvent $event): void
  39.     {
  40.         $this->mainProfile $this->profiler->start('main');
  41.     }
  42.     /**
  43.      * When the route has been determined, add that and information about the request to the main profile.
  44.      *
  45.      * @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
  46.      */
  47.     public function onRequest(Event\RequestEvent $event): void
  48.     {
  49.         if ($this->mainProfile !== null)
  50.         {
  51.             $request $event->getRequest();
  52.             $this->mainProfile->addDataField('request_method'$request->getMethod());
  53.             $this->mainProfile->addDataField('request_uri'$request->getPathInfo());
  54.             $this->mainProfile->addDataField('query_string'$request->getQueryString() ?? "");
  55.             $this->mainProfile->addDataField('route_name'$request->attributes->get('_route'''));
  56.             $this->mainProfile->addDataField('route_params'json_encode($request->attributes->get('_route_params', [])));
  57.         }
  58.     }
  59.     /**
  60.      * When the controller has been determined, add that information to the main profile.
  61.      *
  62.      * @param \Symfony\Component\HttpKernel\Event\ControllerEvent $event
  63.      */
  64.     public function onController(Event\ControllerEvent $event): void
  65.     {
  66.         if ($this->mainProfile !== null)
  67.         {
  68.             $request $event->getRequest();
  69.             $this->mainProfile->addDataField('controller'$request->attributes->get('_controller'''));
  70.         }
  71.     }
  72.     /**
  73.      * Stop profiling when the kernel terminates, instead of when the PHP script ends, for accuracy.
  74.      *
  75.      * @param \Symfony\Component\HttpKernel\Event\TerminateEvent $event
  76.      */
  77.     public function onTerminate(Event\TerminateEvent $event): void
  78.     {
  79.         if ($this->mainProfile !== null)
  80.         {
  81.             $this->mainProfile->stop();
  82.         }
  83.     }
  84. }