src/EventSubscriber/TwigGlobalSubscriber.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Twig\Environment;
  8. class TwigGlobalSubscriber implements EventSubscriberInterface {
  9.     /**
  10.      * @var \Twig\Environment
  11.      */
  12.     private $twig;
  13.     /**
  14.      * @var \Doctrine\ORM\EntityManagerInterface
  15.      */
  16.     private $manager;
  17.     public function __constructEnvironment $twigEntityManagerInterface $manager ) {
  18.         $this->twig    $twig;
  19.         $this->manager $manager;
  20.     }
  21.     public function injectGlobalVariablesControllerEvent $event ) {
  22.         //$whatYouWantAsGlobal = $this->manager->getRepository( 'SomeClass' )->findBy( [ 'some' => 'criteria' ] );
  23.         //$this->twig->addGlobal( 'veryGlobal', $whatYouWantAsGlobal[0]->getName() );
  24.     }
  25.     public static function getSubscribedEvents() {
  26.         return [ KernelEvents::CONTROLLER =>  'injectGlobalVariables' ];
  27.     }
  28. }