src/Package/Toolkit/Listener/LocaleListener/LocaleListener.php line 61

Open in your IDE?
  1. <?php
  2. namespace App\Package\Toolkit\Listener\LocaleListener;
  3. // use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  4. use Symfony\Component\HttpKernel\Event\RequestEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. /**
  8.  * LocaleListener
  9.  * 
  10.  * Executed on KernelEvents::REQUEST
  11.  * - copies locale from route to session 
  12.  * - if locale attribute does not exist in route then default locale is set in session
  13.  * 
  14.  * @see        include.yml
  15.  * 
  16.  * @author     Unknown
  17.  * @author     Daniel Balowski <d.balowski@openform.pl> (_refactorer, _developer)
  18.  * @copyright  Openform
  19.  * @since      03.2019
  20.  */
  21. class LocaleListener implements EventSubscriberInterface
  22. {
  23.     /**
  24.      * @var string
  25.      */
  26.     protected $frontDefaultLocale;
  27.     /**
  28.      * @var string
  29.      */
  30.     protected $adminDefaultLocale;
  31.     /**
  32.      * @var string
  33.      */
  34.     protected $adminSubdomain;
  35.     /**
  36.      * @param string  $frontDefaultLocale
  37.      * @param string  $adminDefaultLocale
  38.      * @param string  $adminSubdomain
  39.      */
  40.     public function __construct(string $frontDefaultLocalestring $adminDefaultLocalestring $adminSubdomain
  41.     {
  42.         $this->frontDefaultLocale $frontDefaultLocale;
  43.         $this->adminDefaultLocale $adminDefaultLocale;
  44.         $this->adminSubdomain     $adminSubdomain;
  45.     }
  46.     /**
  47.      * Sets locale in session based on route
  48.      * 
  49.      * @param RequestEvent $event
  50.      * 
  51.      * @return void
  52.      */
  53.     public function onKernelRequest(RequestEvent $event) : void
  54.     {    
  55.         $request $event->getRequest();
  56.         // Ommit webkit toolbar route 
  57.         // (executed twice when does not ommit _wdt)
  58.         if (
  59.             ! $request->attributes->get('_route') || 
  60.             $request->attributes->get('_route') == '_wdt'
  61.         ) {
  62.             return;
  63.         }
  64.         $locale $request->attributes->get('locale') ?? 
  65.             (
  66.                 strpos($request->getHost(), $this->adminSubdomain) !== false ?
  67.                 $this->adminDefaultLocale 
  68.                 $this->frontDefaultLocale
  69.             );
  70.         $request->getSession()->set('locale'$locale);
  71.         $request->setLocale($locale);
  72.  
  73.         return;
  74.     }
  75.     /**
  76.      * {@inheritDoc}
  77.      */
  78.     public static function getSubscribedEvents() : array
  79.     {
  80.         return [
  81.             KernelEvents::REQUEST => [
  82.                 [
  83.                     'onKernelRequest'17
  84.                 ]
  85.             ],
  86.         ];
  87.     }
  88. }