<?php
namespace App\EventSubscriber;
use App\Entity\TblSociete;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\KernelEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Twig\Environment;
class TwigSubscriber 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($event) {
$societe = $this->manager->getRepository(TblSociete::class)->find(1);
$nomSociete = $societe ? $societe->getRaisonSocial() : 'AGRONET';
$this->twig->addGlobal( 'nomSociete', $nomSociete);
}
public static function getSubscribedEvents() {
return [ KernelEvents::CONTROLLER => 'injectGlobalVariables' ];
}
}