src/EventSubscriber/EnvSwitcherSubscriber.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\RequestEvent;
  5. use Symfony\Component\HttpKernel\Profiler\Profiler;
  6. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  7. use Symfony\Component\Security\Core\Security;
  8. class EnvSwitcherSubscriber implements EventSubscriberInterface
  9. {
  10.     private Security $security;
  11.     private ?Profiler $profiler;
  12.     public function __construct(Security $security, ?Profiler $profiler)
  13.     {
  14.         $this->security $security;
  15.         $this->profiler $profiler;
  16.     }
  17.     public function onKernelRequest(RequestEvent $event): void
  18.     {
  19.         $request $event->getRequest();
  20.        // var_dump($request->headers);
  21.         $user $this->security->getUser();
  22.         if (!$user || !in_array('ROLE_ADMIN'$user->getRoles())) {
  23.             //$this->profiler->disable();
  24.         }
  25.        // dd($event->getRequest()->server->get('APP_ENV'));
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             'kernel.request' => 'onKernelRequest',
  31.         ];
  32.     }
  33. }