src/EventSubscriber/FeatureSubscriber.php line 26

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