<?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;
public function __construct(
Environment $twig,
RequestStack $requestStack,
NetworkService $networkService
) {
$this->twig = $twig;
$this->requestStack = $requestStack;
$this->networkService = $networkService;
}
public function injectGlobalVariables() {
$displayMode = $this->requestStack->getSession()->get('display_mode');
$this->twig->addGlobal( 'display_mode', in_array($displayMode, self::DISPLAY_MODES) ? $displayMode : self::DISPLAY_MODE_DEFAULT);
$this->twig->addGlobal( 'use_latency_in_places_search', $this->networkService->getUseLatencyInPlacesSearch());
}
public static function getSubscribedEvents(): array {
return [
KernelEvents::CONTROLLER => 'injectGlobalVariables'
];
}
}