src/EventSubscriber/FeatureSubscriber.php line 29

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