src/Locale/LocaleEventSubscriber.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Locale;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. class LocaleEventSubscriber implements EventSubscriberInterface
  9. {
  10.     public function __construct(private readonly LocaleSelector $localeSelector)
  11.     {
  12.     }
  13.     public static function getSubscribedEvents(): array
  14.     {
  15.         return [
  16.             KernelEvents::REQUEST => [
  17.                 ['onKernelRequest'10],
  18.             ],
  19.         ];
  20.     }
  21.     public function onKernelRequest(RequestEvent $event): void
  22.     {
  23.         if (!$event->isMainRequest()) {
  24.             return;
  25.         }
  26.         if (!$this->localeSelector->shouldShowLanguagePopin()) {
  27.             return;
  28.         }
  29.         /** @var Request $request */
  30.         $request $event->getRequest();
  31.         if ($request->get(LocaleSelector::LOCALE_SELECT_URL_KEYnull)) {
  32.             $this->localeSelector->setMustShowLanguagePopin(false);
  33.             $url rtrim((string) preg_replace(
  34.                 '#setUserLocale=[^&]*#',
  35.                 '',
  36.                 $request->getUri()
  37.             ), '?');
  38.             $event->setResponse(new RedirectResponse($url));
  39.         }
  40.     }
  41. }