src/V4/Dev/EventSubscriber/DisableProfilerOnProfilerEventSubscriber.php line 32

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\V4\Dev\EventSubscriber;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Symfony\Component\HttpKernel\Profiler\Profiler;
  9. class DisableProfilerOnProfilerEventSubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var Profiler|null
  13.      */
  14.     private $profiler;
  15.     public function __construct(?Profiler $profiler)
  16.     {
  17.         $this->profiler $profiler;
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             KernelEvents::REQUEST => ['onKernelRequest'2048],
  23.         ];
  24.     }
  25.     public function onKernelRequest(RequestEvent $requestEvent): void
  26.     {
  27.         if (!$this->profiler instanceof Profiler) {
  28.             return;
  29.         }
  30.         if ($this->shouldDisable($requestEvent->getRequest())) {
  31.             $this->profiler->disable();
  32.         }
  33.     }
  34.     private function shouldDisable(Request $request): bool
  35.     {
  36.         return Request::METHOD_OPTIONS === $request->getMethod()
  37.             || str_starts_with($request->getRequestUri(), '/dev/profiler');
  38.     }
  39. }