src/EventSubscriber/UserLocaleSubscriber.php line 23

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