<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\RouterInterface;
class AdminSubscriber implements EventSubscriberInterface
{
public function __construct(protected readonly RouterInterface $router)
{
}
public const ADMIN_USER_LOCALE = 'admin_user_locale';
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => ['onRequest', 20],
];
}
public function onRequest(RequestEvent $event)
{
$request = $event->getRequest();
if (!str_starts_with($request->getPathInfo(), '/admin')) {
return;
}
if ($request->getSession()->has(self::ADMIN_USER_LOCALE)) {
$locale = $request->getSession()->get(self::ADMIN_USER_LOCALE);
$request->attributes->set('_locale', $locale);
$request->setLocale($locale);
$request->setDefaultLocale($locale);
}
$this->router->getContext()->setParameter('IS_ADMIN', true);
}
}