src/Service/UserLocale.php line 25

Open in your IDE?
  1. <?php
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. namespace App\Service;
  8. use Symfony\Component\HttpKernel\Event\RequestEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  11. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  12. use Symfony\Component\Security\Http\SecurityEvents;
  13. class UserLocale implements EventSubscriberInterface
  14. {
  15. private $session;
  16. public function __construct(SessionInterface $session)
  17. {
  18. $this->session = $session;
  19. }
  20. public function onInteractiveLogin(InteractiveLoginEvent $event)
  21. {
  22. $user = $event->getAuthenticationToken()->getUser();
  23. if (null !== $user->getLocale()) {
  24. $this->session->set('_locale', $user->getLocale());
  25. }
  26. }
  27. public static function getSubscribedEvents()
  28. {
  29. return [
  30. SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
  31. ];
  32. }
  33. }