vendor/moka/cms/src/Admin/EventSubscriber/UserLocaleSubscriber.php line 28

Open in your IDE?
  1. <?php
  2. namespace Moka\Cms\Admin\EventSubscriber;
  3. use Moka\Cms\Entity\UserInterface;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  9. use Symfony\Component\Security\Http\SecurityEvents;
  10. class UserLocaleSubscriber implements EventSubscriberInterface
  11. {
  12.     public function __construct(private RequestStack $requestStack)
  13.     {
  14.     }
  15.     public function onInteractiveLogin(InteractiveLoginEvent $event)
  16.     {
  17.         $user $event->getAuthenticationToken()->getUser();
  18.         if ($user instanceof UserInterface and $user->getLocale()) {
  19.             $this->requestStack->getSession()->set('_locale'$user->getLocale());
  20.         }
  21.     }
  22.     public function onKernelRequest(RequestEvent $event)
  23.     {
  24.         $request $event->getRequest();
  25.         if (!$request->hasPreviousSession()) {
  26.             return;
  27.         }
  28.         if ($locale $this->requestStack->getSession()->get('_locale')) {
  29.             $request->setLocale($locale);
  30.         }
  31.     }
  32.     public static function getSubscribedEvents()
  33.     {
  34.         return [
  35.             SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
  36.             KernelEvents::REQUEST => [['onKernelRequest'20]],
  37.         ];
  38.     }
  39. }