<?php
namespace App\EventSubscriber;
use DateInterval;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ControllerEvent ;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Security;
use Twig\Environment;
Use App\Entity\Setting;
class TwigGlobalSubscriber implements EventSubscriberInterface {
private $twig;
private $manager;
private $requestStack;
private $security;
private $tokenStorage;
private $authorizationChecker;
public function __construct(Environment $twig, EntityManagerInterface $manager, Security $security, RequestStack $requestStack, TokenStorageInterface $tokenStorage, AuthorizationCheckerInterface $authorizationChecker) {
$this->twig = $twig;
$this->manager = $manager;
$this->requestStack = $requestStack;
$this->security = $security;
$this->tokenStorage = $tokenStorage;
$this->authorizationChecker = $authorizationChecker;
}
public function injectSettings( ControllerEvent $event ) {
$global_settings = $this->manager->getRepository( Setting::class )->findAllGlobal();
foreach ($global_settings as $global_setting) {
$this->twig->addGlobal('global_' . $global_setting['name'], $global_setting['value']);
}
$user_settings = $this->manager->getRepository( Setting::class )->findByUser();
foreach ($user_settings as $user_setting) {
$this->twig->addGlobal('user_' . $user_setting['name'], $user_setting['value']);
}
}
public static function getSubscribedEvents(): array
{
return [ KernelEvents::CONTROLLER => 'injectSettings' ];
}
}