src/Package/Toolkit/TwigServiceLoaderSubscriber/TwigServiceLoaderSubscriber.php line 91

Open in your IDE?
  1. <?php
  2. namespace App\Package\Toolkit\TwigServiceLoaderSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface,
  4.     Symfony\Component\HttpKernel\KernelEvents,
  5.     Symfony\Component\HttpKernel\Event\ControllerEvent,
  6.     Symfony\Component\DependencyInjection\ContainerInterface,
  7.     Twig\Environment;
  8. use App\Package\Toolkit\ApplicationMode\ApplicationMode,
  9.     App\Package\Toolkit\Exception\Exception\Exception;
  10. /**
  11.  * TwigServiceLoaderSubscriber
  12.  *
  13.  * Executed on KernelEvents::CONTROLLER
  14.  * - adds services to twig globals depending on application mode and
  15.  *   passed twig services configuration
  16.  *
  17.  * Twig is taken from container (class \Twig_Environment)
  18.  * IMPORTANT: It's not templating, it's raw twig environment
  19.  *
  20.  * @see        include.yml
  21.  *
  22.  * @author     Daniel Balowski <d.balowski@openform.pl> (_creator)
  23.  * @copyright  Openform
  24.  * @since      03.2019
  25.  */
  26. class TwigServiceLoaderSubscriber implements EventSubscriberInterface
  27. {
  28.     /**
  29.      * @var ContainerInterface
  30.      */
  31.     protected $container;
  32.     /**
  33.      * @var ApplicationMode
  34.      */
  35.     protected $applicationMode;
  36.     /**
  37.      * @var string[]
  38.      */
  39.     protected $twigServices;
  40.     /**
  41.      * @var Environment $twig
  42.      */
  43.     protected $twig;
  44.     /**
  45.      * @param ContainerInterface  $container
  46.      * @param ApplicationMode     $applicationMode
  47.      * @param string[]            $twigServices
  48.      */
  49.     public function __construct(
  50.         ContainerInterface $container,
  51.         ApplicationMode    $applicationMode,
  52.         Environment        $twig,
  53.         array              $twigServices
  54.     ) {
  55.         $this->container       $container;
  56.         $this->applicationMode $applicationMode;
  57.         $this->twigServices    $twigServices;
  58.         $this->twig            $twig;
  59.     }
  60.     /**
  61.      * {@inheritDoc}
  62.      */
  63.     public static function getSubscribedEvents() : array
  64.     {
  65.         return [
  66.             KernelEvents::CONTROLLER => [
  67.                 [ 'addServicesToTwigGlobals'255 ],
  68.             ]
  69.         ];
  70.     }
  71.     /**
  72.      * Adds services to twig globals
  73.      *
  74.      * @param ControllerEvent $event
  75.      *
  76.      * @throws Exception    if service does not exist in container
  77.      *
  78.      * @return void
  79.      */
  80.     public function addServicesToTwigGlobals(ControllerEvent $event) : void
  81.     {
  82.         if (! array_key_exists($this->applicationMode->getCurrentMode(), $this->twigServices)) {
  83.             return;
  84.         }
  85.         $servicesToLoad = [];
  86.         foreach($this->twigServices$this->applicationMode->getCurrentMode() ] as $services) {
  87.             if ($services) {
  88.                 foreach ($services as $serviceName => $service) {
  89.                     $servicesToLoad$serviceName ] =  $service;
  90.                 }
  91.             }
  92.         }
  93.         foreach($servicesToLoad as $globalName => $service) {
  94.             if (! $this->container->has($service)) {
  95.                 throw new Exception(
  96.                     ' >> Missing service in container: "' $service '"'
  97.                 );
  98.             }
  99.             $this->twig->addGlobal($globalName$this->container->get($service));
  100.         }
  101.         return;
  102.     }
  103. }