src/EventSubscriber/TwigGlobalSubscriber.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Repository\SettingRepository;
  4. use App\Repository\UserRepository;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Twig\Environment;
  9. class TwigGlobalSubscriber implements EventSubscriberInterface
  10. {
  11.     private UserRepository $userRepository;
  12.     private Environment $twig;
  13.    
  14.     private SettingRepository $settingRepository;
  15.     public function __construct(UserRepository $userRepositorySettingRepository $settingRepositoryEnvironment $twig)
  16.     {
  17.         $this->userRepository $userRepository;
  18.         $this->settingRepository $settingRepository;
  19.         $this->twig $twig;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             KernelEvents::CONTROLLER => 'onKernelController',
  25.         ];
  26.     }
  27.     public function onKernelController(ControllerEvent $event): void
  28.     {
  29.         $setting $this->settingRepository->find(1);
  30.         $this->twig->addGlobal('setting'$setting);
  31.         
  32.     }
  33. }