src/EventSubscriber/ModuleSubscriber.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. use Twig\Environment;
  8. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  9. class ModuleSubscriber implements EventSubscriberInterface
  10. {
  11.     private Environment $twig;
  12.     private TokenStorageInterface $tokenStorage;
  13.     public function __construct(
  14.         Environment $twig,
  15.         TokenStorageInterface $tokenStorage
  16.     ) {
  17.         $this->twig $twig;
  18.         $this->tokenStorage $tokenStorage;
  19.     }
  20.     public function onKernelController(ControllerEvent $event)
  21.     {
  22.         $token $this->tokenStorage->getToken();
  23.         if ($token === null) {
  24.             return false;
  25.         }
  26.         $user $token->getUser();
  27.         $moduleActive false;
  28.         if ($user instanceof UserInterface) {
  29.             $controller $event->getController();
  30.             if (is_array($controller)) {
  31.                 if (isset($controller[0]->module)) {
  32.                     $moduleActive $controller[0]->module;
  33.                 }
  34.             }
  35.         }
  36.         $this->twig->addGlobal('module_active'$moduleActive);
  37.     }
  38.     public static function getSubscribedEvents(): array
  39.     {
  40.         return array(
  41.             KernelEvents::CONTROLLER => 'onKernelController',
  42.         );
  43.     }
  44. }