src/Subscriber/ParameterSubscriber.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber;
  3. use App\Entity\Parameter;
  4. use App\Service\ParameterContainer;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. class ParameterSubscriber implements EventSubscriberInterface
  10. {
  11.     protected $parameters null;
  12.     protected $twig;
  13.     protected $parameterContainer;
  14.     protected $entityManager;
  15.     public function __construct(
  16.         \Twig_Environment $twig,
  17.         ParameterContainer $parameterContainer,
  18.         EntityManagerInterface $entityManager
  19.     ) {
  20.         $this->twig $twig;
  21.         $this->parameterContainer $parameterContainer;
  22.         $this->entityManager $entityManager;
  23.     }
  24.     public function onKernelRequest(RequestEvent $event)
  25.     {
  26.         $globalParameters $this->getParameters();
  27.         $this->storeParameter(ParameterContainer::SETTINGS_KEY$globalParameters);
  28.     }
  29.     protected function getParameters(): array
  30.     {
  31.         if (is_null($this->parameters)) {
  32.             $parameters $this->entityManager->getRepository(Parameter::class)->findAll();
  33.             $globalParameters = array();
  34.             /** @var Parameter $setting */
  35.             foreach ($parameters as $parameter) {
  36.                 $globalParameters[$parameter->getName()] = $parameter->getValue();
  37.             }
  38.             $this->parameters $globalParameters;
  39.         }
  40.         return $this->parameters;
  41.     }
  42.     protected function storeParameter($key$value)
  43.     {
  44.         $this->parameterContainer->setParameter($key$value);
  45.         $this->twig->addGlobal($key$value);
  46.     }
  47.     public static function getSubscribedEvents()
  48.     {
  49.         return [
  50.             KernelEvents::REQUEST => [['onKernelRequest'20]],
  51.         ];
  52.     }
  53. }