src/EventSubscriber/System/TwigGlobalSubscriber.php line 43

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\System;
  3. use App\Entity\Addressing\Country;
  4. use App\Entity\Addressing\County;
  5. use App\Entity\System\CampaignStatus;
  6. use App\Entity\System\ProductStatus;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. use Symfony\Component\Routing\RouterInterface;
  14. use Twig\Environment;
  15. use App\Entity\System\TemplateStatus;
  16. class TwigGlobalSubscriber implements EventSubscriberInterface {
  17.     /**
  18.      * @var \Twig\Environment
  19.      */
  20.     private $twig;
  21.     /**
  22.      * @var EntityManagerInterface
  23.      */
  24.     private $entityManager;
  25.     /** @var RouterInterface */
  26.     private $router;
  27.     /** @var Request|null */
  28.     private $request;
  29.     public function __construct(Environment $twigEntityManagerInterface $entityManagerRouterInterface $routerRequestStack $requestStack) {
  30.         $this->twig    $twig;
  31.         $this->entityManager $entityManager;
  32.         $this->router $router;
  33.         $this->request $requestStack->getCurrentRequest();
  34.     }
  35.     public function injectGlobalVariables(ControllerEvent $event) {
  36.         $lastProductStatus $this->entityManager->getRepository(ProductStatus::class)->findLastRecord();
  37.         $lastProductStatusArray $this->entityManager->getRepository(ProductStatus::class)->findLastRecordAsArray();
  38.         $lastTemplateStatus $this->entityManager->getRepository(TemplateStatus::class)->findLastRecord();
  39.         $lastTemplateStatusArray $this->entityManager->getRepository(TemplateStatus::class)->findLastRecordAsArray();
  40.         $lastCampaignStatus $this->entityManager->getRepository(CampaignStatus::class)->findLastRecord();
  41.         $lastCampaignStatusArray $this->entityManager->getRepository(CampaignStatus::class)->findLastRecordAsArray();
  42.         $country $this->entityManager->getRepository(Country::class)->find(Country::RO_COUNTRY_ID);
  43.         $countiesForMenu $this->entityManager->getRepository(County::class)->findAllCountiesByCountry($country);
  44.         $request $event->getRequest();
  45.         $isCurrentPathRoVignette $request && $request->getPathInfo() == $this->router->generate('app_shop_vignette');
  46.         $isCurrentPathHuVignette $request && $request->getPathInfo() == $this->router->generate('app_shop_hu_vignette');
  47.         $isCurrentPathRca $request && $request->getPathInfo() == $this->router->generate('app_shop_rca_details');
  48.         $this->twig->addGlobal('lastProductStatus'$lastProductStatus);
  49.         $this->twig->addGlobal('countiesForMenu'$countiesForMenu);
  50.         $this->twig->addGlobal('lastProductStatusArray'$lastProductStatusArray);
  51.         $this->twig->addGlobal('lastTemplateStatus'$lastTemplateStatus);
  52.         $this->twig->addGlobal('lastTemplateStatusArray'$lastTemplateStatusArray);
  53.         $this->twig->addGlobal('lastCampaignStatus'$lastCampaignStatus);
  54.         $this->twig->addGlobal('lastCampaignStatusArray'$lastCampaignStatusArray);
  55.         $this->twig->addGlobal('isCurrentPathRoVignette'$isCurrentPathRoVignette);
  56.         $this->twig->addGlobal('isCurrentPathHuVignette'$isCurrentPathHuVignette);
  57.         $this->twig->addGlobal('isCurrentPathRca'$isCurrentPathRca);
  58.         $this->twig->addGlobal('rcaSiteUrl'$_ENV['RCA_SITE_URL']);
  59.         $this->twig->addGlobal('roVignetteSiteUrl'$_ENV['ROVINIETE_SITE_URL']);
  60.         $this->twig->addGlobal('isTicketing'$_ENV['IS_TICKETING']);
  61.     }
  62.     public static function getSubscribedEvents() {
  63.         return [KernelEvents::CONTROLLER =>  'injectGlobalVariables'];
  64.     }
  65. }