src/EventSubscriber/TwigGlobalSubscriber.php line 36

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use DateInterval;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpKernel\Event\ControllerEvent ;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  11. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  12. use Symfony\Component\Security\Core\Security;
  13. use Twig\Environment;
  14. Use App\Entity\Setting;
  15. class TwigGlobalSubscriber implements EventSubscriberInterface {
  16.     private $twig;
  17.     private $manager;
  18.     private $requestStack;
  19.     private $security;
  20.     private $tokenStorage;
  21.     private $authorizationChecker;
  22.     public function __construct(Environment $twigEntityManagerInterface $managerSecurity $securityRequestStack $requestStackTokenStorageInterface $tokenStorageAuthorizationCheckerInterface $authorizationChecker) {
  23.         $this->twig    $twig;
  24.         $this->manager $manager;
  25.         $this->requestStack $requestStack;
  26.         $this->security $security;
  27.         $this->tokenStorage $tokenStorage;
  28.         $this->authorizationChecker $authorizationChecker;
  29.     }
  30.     public function injectSettingsControllerEvent  $event ) {
  31.         $global_settings $this->manager->getRepositorySetting::class )->findAllGlobal();
  32.         foreach ($global_settings as $global_setting) {
  33.             $this->twig->addGlobal('global_' $global_setting['name'], $global_setting['value']);
  34.         }
  35.         $user_settings $this->manager->getRepositorySetting::class )->findByUser();
  36.         foreach ($user_settings as $user_setting) {
  37.             $this->twig->addGlobal('user_' $user_setting['name'], $user_setting['value']);
  38.         }
  39.     }
  40.     public static function getSubscribedEvents(): array
  41.     {
  42.         return [ KernelEvents::CONTROLLER =>  'injectSettings' ];
  43.     }
  44. }