src/EventSubscriber/TwigGlobalSubscriber.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Service\Internal\NetworkService;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Twig\Environment;
  8. class TwigGlobalSubscriber implements EventSubscriberInterface {
  9. public const DISPLAY_MODE_DEFAULT = 'DEFAULT';
  10. public const DISPLAY_MODE_WEBVIEW = 'WEBVIEW';
  11. public const DISPLAY_MODES = [
  12. self::DISPLAY_MODE_DEFAULT,
  13. self::DISPLAY_MODE_WEBVIEW
  14. ];
  15. private Environment $twig;
  16. private RequestStack $requestStack;
  17. public function __construct(
  18. Environment $twig,
  19. RequestStack $requestStack,
  20. NetworkService $networkService
  21. ) {
  22. $this->twig = $twig;
  23. $this->requestStack = $requestStack;
  24. $this->networkService = $networkService;
  25. }
  26. public function injectGlobalVariables() {
  27. $displayMode = $this->requestStack->getSession()->get('display_mode');
  28. $this->twig->addGlobal( 'display_mode', in_array($displayMode, self::DISPLAY_MODES) ? $displayMode : self::DISPLAY_MODE_DEFAULT);
  29. $this->twig->addGlobal( 'use_latency_in_places_search', $this->networkService->getUseLatencyInPlacesSearch());
  30. }
  31. public static function getSubscribedEvents() {
  32. return [
  33. KernelEvents::CONTROLLER => 'injectGlobalVariables'
  34. ];
  35. }
  36. }