src/EventSubscriber/LocaleSubscriber.php line 53

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use InvalidArgumentException;
  4. use Negotiation\LanguageNegotiator;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use Symfony\Component\HttpKernel\KernelInterface;
  10. /**
  11.  * Class LocaleSubscriber
  12.  * @package App\EventSubscriber
  13.  */
  14. class LocaleSubscriber implements EventSubscriberInterface
  15. {
  16.     /** @var array */
  17.     private $locales;
  18.     /** @var string */
  19.     private $defaultLocale;
  20.     /**
  21.      * LanguageListener constructor.
  22.      * @param $locales
  23.      * @param $defaultLocale
  24.      */
  25.     public function __construct($locales$defaultLocale)
  26.     {
  27.         $this->locales explode('|'trim($locales));
  28.         if (empty($this->locales)) {
  29.             throw new InvalidArgumentException('The list of supported locales must not be empty.');
  30.         }
  31.         $this->defaultLocale $defaultLocale ?: $this->locales[0];
  32.     }
  33.     /**
  34.      * @return array
  35.      */
  36.     public static function getSubscribedEvents()
  37.     {
  38.         return [
  39.             KernelEvents::REQUEST => ['redirectToLocalizedIndex'100],
  40.         ];
  41.     }
  42.     /**
  43.      * @param RequestEvent $event
  44.      */
  45.     public function redirectToLocalizedIndex(RequestEvent $event)
  46.     {
  47.         // Do not modify sub-requests
  48.         if (KernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
  49.             return;
  50.         }
  51.         // Assume all routes except the frontpage use the _locale parameter
  52.         if ($event->getRequest()->getPathInfo() !== '/' && $event->getRequest()->getPathInfo() !== '/calculator/redirect') {
  53.             return;
  54.         }
  55.         $language $this->defaultLocale;
  56.         if (null !== $acceptLanguage $event->getRequest()->headers->get('Accept-Language')) {
  57.             $negotiator = new LanguageNegotiator();
  58.             $best $negotiator->getBest(
  59.                 $event->getRequest()->headers->get('Accept-Language'),
  60.                 $this->locales
  61.             );
  62.             if (null !== $best) {
  63.                 $language $best->getType();
  64.             }
  65.             if (!in_array($language$this->locales)) {
  66.                 $language 'en';
  67.             }
  68.         }
  69.         if($event->getRequest()->getPathInfo() === '/calculator/redirect') {
  70.             $pathInfo '/calculator';
  71.         }else {
  72.             $pathInfo '/dashboard';
  73.         }
  74.         $response = new RedirectResponse('/' $language $pathInfo);
  75.         $event->setResponse($response);
  76.     }
  77. }