src/EventSubscriber/TwigGlobalSubscriber.php line 36

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. private NetworkService $networkService;
  18. public function __construct(
  19. Environment $twig,
  20. RequestStack $requestStack,
  21. NetworkService $networkService
  22. ) {
  23. $this->twig = $twig;
  24. $this->requestStack = $requestStack;
  25. $this->networkService = $networkService;
  26. }
  27. public function injectGlobalVariables() {
  28. $displayMode = $this->requestStack->getSession()->get('display_mode');
  29. $this->twig->addGlobal( 'display_mode', in_array($displayMode, self::DISPLAY_MODES) ? $displayMode : self::DISPLAY_MODE_DEFAULT);
  30. $this->twig->addGlobal( 'use_latency_in_places_search', $this->networkService->getUseLatencyInPlacesSearch());
  31. }
  32. public static function getSubscribedEvents(): array {
  33. return [
  34. KernelEvents::CONTROLLER => 'injectGlobalVariables'
  35. ];
  36. }
  37. }