src/EventSubscriber/ConnectAsSubscriber.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpFoundation\Session\Session;
  5. use Symfony\Component\HttpKernel\Event\RequestEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Twig\Environment;
  8. class ConnectAsSubscriber implements EventSubscriberInterface
  9. {
  10.     private Environment $twig;
  11.     public function __construct(
  12.         Environment $twig
  13.     ) {
  14.         $this->twig $twig;
  15.     }
  16.     public function onKernelRequest(RequestEvent $event): void
  17.     {
  18.         if (!$event->isMasterRequest()) {
  19.             return;
  20.         }
  21.         $session = new Session();
  22.         $this->twig->addGlobal('connected_as', ($session->get('connect_as_master') !== null));
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return array(
  27.             KernelEvents::REQUEST => 'onKernelRequest',
  28.         );
  29.     }
  30. }