src/Event/UserLocaleSubscriber.php line 90

Open in your IDE?
  1. <?php
  2. namespace App\Event;
  3. use App\Entity\Language;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  8. use Symfony\Component\HttpKernel\Event\RequestEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Symfony\Component\Security\Core\Security;
  11. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  12. use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
  13. use Symfony\Component\Security\Http\SecurityEvents;
  14. /**
  15.  * Stores the locale of the user in the session after the
  16.  * login. This can be used by the LocaleSubscriber afterwards.
  17.  */
  18. class UserLocaleSubscriber implements EventSubscriberInterface
  19. {
  20.     private $session;
  21.     private $request_stack;
  22.     private $security;
  23.     public function __construct(EntityManagerInterface $entity_managerRequestStack $requestStackSecurity $security)
  24.     {
  25.         $this->entity_manager $entity_manager;
  26.         $this->request_stack $requestStack;
  27.         $this->security $security;
  28.     }
  29.     public function onLoginSuccess(LoginSuccessEvent $event)
  30.     {
  31.         $User $event->getUser();
  32.         $Customer $User->getCustomer();
  33.         $Settings $Customer->getSettings();
  34.         $customerLanguagesArray = (isset($Settings['languages'])) ? $Settings['languages'] : [];
  35.         $request $event->getRequest();
  36.         $session $request->getSession();
  37.         $userLocale $this->entity_manager->getRepository(Language::class)->findOneBy(array('Code'=>$User->getLocale()));
  38.         $userLocaleId $userLocale->getId();
  39.         if (in_array($userLocaleId$customerLanguagesArray)) {
  40.             $defaultLanguageCode $User->getLocale();
  41.         } else {
  42.             $defaultLanguageId $Customer->getDefaultLanguage();
  43.             $defaultLanguage =  $this->entity_manager->getRepository(Language::class)->find($defaultLanguageId);
  44.             $defaultLanguageCode $defaultLanguage->getCode();
  45.         }
  46.         if ($defaultLanguageCode !== null) {
  47.             $request->getSession()->set('_locale'$defaultLanguageCode);
  48.             $request->setLocale($defaultLanguageCode);
  49.             $request->setDefaultLocale($defaultLanguageCode);
  50.             //dump($session);
  51.             //dd($request->getSession());
  52.         }
  53.     }
  54.     public function onKernelRequest(RequestEvent $event)
  55.     {
  56.         $request $event->getRequest();
  57.         //$bag = $request->getSession()->getBag('attributes');
  58.         //dd($request->getSession()->get('_locale'));
  59.         if (!$request->hasPreviousSession()) {
  60.             return;
  61.         }
  62.         // try to see if the locale has been set as a _locale routing parameter
  63.         if ($locale $request->getSession()->get('_locale')) {
  64.             $request->setLocale($request->getSession()->get('_locale'));
  65.             $request->setDefaultLocale($request->getSession()->get('_locale'));
  66.         } else {
  67.             $request->setLocale('en');
  68.             $request->setDefaultLocale('en');
  69.         }
  70.     }
  71.     public static function getSubscribedEvents(): ?array
  72.     {
  73.         return [
  74.             LoginSuccessEvent::class => 'onLoginSuccess',
  75.             KernelEvents::REQUEST => [['onKernelRequest'20]],
  76.         ];
  77.     }
  78. }