src/EventSubscriber/TwigGlobalSubscriber.php line 49

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. private bool $enableWebHarmony;
  19. private string $webHarmonyJsUrl;
  20. private string $webHarmonyCssUrl;
  21. public function __construct(
  22. Environment $twig,
  23. RequestStack $requestStack,
  24. NetworkService $networkService,
  25. bool|string $enableWebHarmony,
  26. string $webHarmonyJsUrl,
  27. string $webHarmonyCssUrl,
  28. ) {
  29. $this->twig = $twig;
  30. $this->requestStack = $requestStack;
  31. $this->networkService = $networkService;
  32. $this->enableWebHarmony = filter_var($enableWebHarmony, FILTER_VALIDATE_BOOLEAN);
  33. $this->webHarmonyJsUrl = $webHarmonyJsUrl;
  34. $this->webHarmonyCssUrl = $webHarmonyCssUrl;
  35. }
  36. public function injectGlobalVariables() {
  37. $displayMode = $this->requestStack->getSession()->get('display_mode');
  38. $displayMode = in_array($displayMode, self::DISPLAY_MODES) ? $displayMode : self::DISPLAY_MODE_DEFAULT;
  39. $this->twig->addGlobal( 'display_mode', $displayMode);
  40. $this->twig->addGlobal('web_harmony_enabled', $this->isWebHarmonyEnabled($displayMode));
  41. $this->twig->addGlobal( 'use_latency_in_places_search', $this->networkService->getUseLatencyInPlacesSearch());
  42. }
  43. public static function getSubscribedEvents(): array {
  44. return [
  45. KernelEvents::CONTROLLER => 'injectGlobalVariables'
  46. ];
  47. }
  48. private function isWebHarmonyEnabled(string $displayMode): bool {
  49. return $this->enableWebHarmony
  50. && '' !== $this->webHarmonyJsUrl
  51. && '' !== $this->webHarmonyCssUrl
  52. && self::DISPLAY_MODE_WEBVIEW !== $displayMode;
  53. }
  54. }