<?php
namespace App\EventSubscriber;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Twig\Environment;
class TwigGlobalSubscriber implements EventSubscriberInterface {
/**
* @var \Twig\Environment
*/
private $twig;
/**
* @var \Doctrine\ORM\EntityManagerInterface
*/
private $manager;
public function __construct( Environment $twig, EntityManagerInterface $manager ) {
$this->twig = $twig;
$this->manager = $manager;
}
public function injectGlobalVariables( ControllerEvent $event ) {
//$whatYouWantAsGlobal = $this->manager->getRepository( 'SomeClass' )->findBy( [ 'some' => 'criteria' ] );
//$this->twig->addGlobal( 'veryGlobal', $whatYouWantAsGlobal[0]->getName() );
}
public static function getSubscribedEvents() {
return [ KernelEvents::CONTROLLER => 'injectGlobalVariables' ];
}
}