src/EventSubscriber/UserLocaleSubscriber.php line 25

Open in your IDE?
  1. <?php
  2. // src/EventSubscriber/UserLocaleSubscriber.php
  3. namespace App\EventSubscriber;
  4. use App\Entity\User;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  8. use Symfony\Component\Security\Http\SecurityEvents;
  9. /**
  10.  * Stores the locale of the user in the session after the
  11.  * login. This can be used by the LocaleSubscriber afterwards.
  12.  */
  13. class UserLocaleSubscriber implements EventSubscriberInterface
  14. {
  15.     private $requestStack;
  16.     public function __construct(RequestStack $requestStack)
  17.     {
  18.         $this->requestStack $requestStack;
  19.     }
  20.     public function onInteractiveLogin(InteractiveLoginEvent $event)
  21.     {
  22.         /** @var User $user */
  23.         $user $event->getAuthenticationToken()->getUser();
  24.         if (null !== $user->getLocale()) {
  25.             $this->requestStack->getSession()->set('_locale'$user->getLocale());
  26.         }
  27.     }
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
  32.         ];
  33.     }
  34. }