src/EventSubscriber/LocaleSubscriber.php line 69

Open in your IDE?
  1. <?php
  2. /**
  3.  * @license SILK SOFTWARE HOUSE SP Z O O
  4.  */
  5. namespace App\EventSubscriber;
  6. use App\Repository\UserSettingRepository;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpKernel\Event\RequestEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Symfony\Component\Security\Core\AuthenticationEvents;
  11. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  12. use Symfony\Component\Security\Http\SecurityEvents;
  13. /**
  14.  * Class LocaleSubscriber.
  15.  */
  16. class LocaleSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * Repository.
  20.      *
  21.      * @var UserSettingRepository
  22.      */
  23.     private $repository;
  24.     /**
  25.      * Default locale.
  26.      *
  27.      * @var string
  28.      */
  29.     private $defaultLocale 'en';
  30.     /**
  31.      * LocaleSubscriber constructor.
  32.      *
  33.      * @param UserSettingRepository $repository
  34.      * @param string                $defaultLocale
  35.      */
  36.     public function __construct(UserSettingRepository $repository$defaultLocale 'en')
  37.     {
  38.         $this->defaultLocale $defaultLocale;
  39.         $this->repository $repository;
  40.     }
  41.     /**
  42.      * @param RequestEvent $event
  43.      */
  44.     public function onKernelRequest(RequestEvent $event)
  45.     {
  46.         $request $event->getRequest();
  47.         if (!$request->hasPreviousSession()) {
  48.             return;
  49.         }
  50.         // try to see if the locale has been set as a _locale routing parameter
  51.         if (!($locale $request->attributes->get('_locale'))) {
  52.             // if no explicit locale has been set on this request, use one from the session
  53.             $request->setLocale($request->getSession()->get('_locale'$this->defaultLocale));
  54.         }
  55.     }
  56.     /**
  57.      * On security interactive login.
  58.      *
  59.      * @param InteractiveLoginEvent $event
  60.      */
  61.     public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
  62.     {
  63.         if (!$event->getAuthenticationToken() || !$event->getAuthenticationToken()->getUser()) {
  64.             return;
  65.         }
  66.         $user $event->getAuthenticationToken()->getUser();
  67.         if ($settings $this->repository->getOrCreateForUser($user)) {
  68.             $event->getRequest()->getSession()->set('_locale'$settings->getLanguage());
  69.         }
  70.     }
  71.     /**
  72.      * Get subscribed events.
  73.      *
  74.      * @return array
  75.      */
  76.     public static function getSubscribedEvents()
  77.     {
  78.         return [
  79.             // must be registered before (i.e. with a higher priority than) the default Locale listener
  80.             KernelEvents::REQUEST => [['onKernelRequest'20]],
  81.             SecurityEvents::INTERACTIVE_LOGIN => [['onSecurityInteractiveLogin'18]],
  82.         ];
  83.     }
  84. }