<?php
namespace App\EventSubscriber;
use App\Service\Internal\NetworkService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\KernelEvents;
use Twig\Environment;
class TwigGlobalSubscriber implements EventSubscriberInterface {
public const DISPLAY_MODE_DEFAULT = 'DEFAULT';
public const DISPLAY_MODE_WEBVIEW = 'WEBVIEW';
public const DISPLAY_MODES = [
self::DISPLAY_MODE_DEFAULT,
self::DISPLAY_MODE_WEBVIEW
];
private Environment $twig;
private RequestStack $requestStack;
private NetworkService $networkService;
private bool $enableWebHarmony;
private string $webHarmonyJsUrl;
private string $webHarmonyCssUrl;
public function __construct(
Environment $twig,
RequestStack $requestStack,
NetworkService $networkService,
bool|string $enableWebHarmony,
string $webHarmonyJsUrl,
string $webHarmonyCssUrl,
) {
$this->twig = $twig;
$this->requestStack = $requestStack;
$this->networkService = $networkService;
$this->enableWebHarmony = filter_var($enableWebHarmony, FILTER_VALIDATE_BOOLEAN);
$this->webHarmonyJsUrl = $webHarmonyJsUrl;
$this->webHarmonyCssUrl = $webHarmonyCssUrl;
}
public function injectGlobalVariables() {
$displayMode = $this->requestStack->getSession()->get('display_mode');
$displayMode = in_array($displayMode, self::DISPLAY_MODES) ? $displayMode : self::DISPLAY_MODE_DEFAULT;
$this->twig->addGlobal( 'display_mode', $displayMode);
$this->twig->addGlobal('web_harmony_enabled', $this->isWebHarmonyEnabled($displayMode));
$this->twig->addGlobal( 'use_latency_in_places_search', $this->networkService->getUseLatencyInPlacesSearch());
}
public static function getSubscribedEvents(): array {
return [
KernelEvents::CONTROLLER => 'injectGlobalVariables'
];
}
private function isWebHarmonyEnabled(string $displayMode): bool {
return $this->enableWebHarmony
&& '' !== $this->webHarmonyJsUrl
&& '' !== $this->webHarmonyCssUrl
&& self::DISPLAY_MODE_WEBVIEW !== $displayMode;
}
}