src/Utils/Listeners/LocaleListener.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Utils\Listeners;
  3. use Symfony\Component\HttpFoundation\RedirectResponse;
  4. use Symfony\Component\Routing\RouterInterface;
  5. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Routing\Matcher\UrlMatcher;
  9. use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
  10. use Symfony\Component\Routing\RequestContext;
  11. use Symfony\Component\HttpKernel\Event\RequestEvent;
  12. class LocaleListener implements EventSubscriberInterface
  13. {
  14.     /**
  15.     * @var routeCollection \Symfony\Component\Routing\RouteCollection
  16.     */
  17.     private $routeCollection;
  18.     /**
  19.     * @var urlMatcher \Symfony\Component\Routing\Matcher\UrlMatcher;
  20.     */
  21.     private $urlMatcher;
  22.     private $oldUrl;
  23.     private $newUrl;
  24.     private $languages;
  25.     private $defaultLanguage;
  26.     public function __construct(RouterInterface $routerUrlMatcherInterface $urlMatcher)
  27.     {
  28.         $this->routeCollection $router->getRouteCollection();
  29.         $this->languages = ["en","ar"];
  30.         $this->defaultLanguage "en";
  31.         $context = new RequestContext("/");
  32.         $this->urlMatcher $urlMatcher;
  33.     }
  34.     public function onKernelRequest(RequestEvent $event)
  35.     {
  36.         //GOAL:
  37.         // Redirect all incoming requests to their /locale/route equivalent when exists.
  38.         // Do nothing if it already has /locale/ in the route to prevent redirect loops
  39.         // Do nothing if the route requested has no locale param
  40.         $request $event->getRequest();
  41.         $this->newUrl  $request->getPathInfo();
  42.         $this->oldUrl $request->headers->get('referer');
  43.         $locale $this->checkLanguage();
  44.         if ($locale === null) {
  45.             return;
  46.         }
  47.         
  48.         $request->setLocale($locale);
  49.         $pathLocale "/".$locale.$this->newUrl;
  50.         //We have to catch the ResourceNotFoundException
  51.         try {
  52.             //Try to match the path with the local prefix
  53.             $this->urlMatcher->match($pathLocale);
  54.             $event->setResponse(new RedirectResponse($pathLocale));
  55.         } catch (\Symfony\Component\Routing\Exception\ResourceNotFoundException $e) {
  56.         } catch (\Symfony\Component\Routing\Exception\MethodNotAllowedException $e) {
  57.         }
  58.     }
  59.     private function checkLanguage()
  60.     {
  61.         foreach ($this->languages as $language) {
  62.             if (preg_match_all("/\/$language\//"$this->newUrl)) {
  63.                 return null;
  64.             }
  65.             if (preg_match_all("/\/$language\//"$this->oldUrl)) {
  66.                 return $language;
  67.             }
  68.         }
  69.         return $this->defaultLanguage;
  70.     }
  71.     public static function getSubscribedEvents()
  72.     {
  73.         return array(
  74.             // must be registered before the default Locale listener
  75.             KernelEvents::REQUEST => array(array('onKernelRequest'17)),
  76.         );
  77.     }
  78. }