src/EventListener/LocaleListener.php line 88

Open in your IDE?
  1. <?php
  2. // src/EventListener/LocaleListener.php
  3. namespace App\EventListener;
  4. use  phpDocumentor\Reflection\Types\Array_;
  5. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\Routing\RouterInterface;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Doctrine\ORM\Q;
  12. use Symfony\Component\HttpKernel\KernelInterface;
  13. use App\Entity\Pagina;
  14. use Symfony\Component\Routing\Matcher\UrlMatcher;
  15. use Symfony\Component\Routing\RequestContext;
  16. use Psr\Log\LoggerInterface;
  17. class LocaleListener implements EventSubscriberInterface
  18. {
  19.     private $routeCollection;
  20.     /**
  21.      * @var urlMatcher \Symfony\Component\Routing\Matcher\UrlMatcher;
  22.      */
  23.     private $urlMatcher;
  24.     private $oldUrl;
  25.     private $newUrl;
  26.     private $languages;
  27.     private $defaultLanguage;
  28.     private $router;
  29.     private $log;
  30.     public function __construct(RouterInterface $router,  $defaultLanguage 'it',EntityManagerInterface $_em,LoggerInterface $logger)
  31.     {
  32.         $this->em=$_em;
  33.         $this->routeCollection $router->getRouteCollection();
  34.         $this->languages =  Array("it","en","fr");
  35.         $this->defaultLanguage $defaultLanguage;
  36.         $this->router=$router;
  37.         $this->log $logger;
  38.         $context = new RequestContext("/");
  39.     }
  40.     public function onKernelRequest(GetResponseEvent $event)
  41.     {
  42.         
  43.         //GOAL:
  44.         // Redirect all incoming requests to their /locale/route equivalent when exists.
  45.         // Do nothing if it already has /locale/ in the route to prevent redirect loops
  46.         // Do nothing if the route requested has no locale param
  47.         $request $event->getRequest();
  48.         $this->newUrl  $request->getPathInfo();
  49.         $url=$this->newUrl;
  50.         $this->oldUrl $this->newUrl;
  51.         $locale $this->checkLanguage();
  52.         //var_dump($locale);
  53.         if($locale===null)
  54.             $locale="it";
  55.         $locales=$locale;
  56.         $this->newUrl  $request->getPathInfo();
  57.         $pos=strrpos($this->newUrl,"/");
  58.         $idsubstr($this->newUrl,$pos+1);
  59.         if(strpos($url,"/it/pagina")>-|| strpos($url,"/en/page")>-|| strpos($url,"/fr/page")>-1) {
  60.             {
  61.                 $repopagina $this->em->getRepository(Pagina::class);
  62.                 $pagina $repopagina->find($id);
  63.                 $url "";
  64.                 $pathLocale $this->newUrl;
  65.                 //We have to catch the ResourceNotFoundException
  66.                 //Try to match the path with the local prefi
  67.                           if ($locale == "it") {
  68.                               $url "https://www.eurobagno.com/public/index.php/__locale__/pagina/" $pagina->translate($locale)->geturl() . "/__id__";
  69.                           } elseif ($locale == "en") {
  70.                               $url "https://www.eurobagno.com/public/index.php/__locale__/page/" $pagina->translate($locale)->geturl() . "/__id__";
  71.                           } else {
  72.                               $url "https://www.eurobagno.com/public/index.php/__locale__/page/" $pagina->translate($locale)->geturl() . "/__id__";
  73.                           }
  74.                           $url str_replace("__id__"$pagina->getId(), $url);
  75.                           $url str_replace("__locale__"$locale$url);
  76.                 /* if ($locale == "it") {
  77.                     $url = "http://localhost/eurobagno/public/index.php/__locale__/pagina/" . $pagina->translate($locale)->geturl(). "/__id__";
  78.                 } elseif ($locale == "en") {
  79.                     $url = "http://localhost/eurobagno/public/index.php/__locale__/page/" . $pagina->translate($locale)->geturl(). "/__id__";
  80.                 } else {
  81.                     $url = "http://localhost/eurobagno/public/index.php/__locale__/page/" . $pagina->translate($locale)->geturl() . "/__id__";
  82.                 }*/
  83.                 $url str_replace("__id__"$pagina->getId(), $url);
  84.                 $url str_replace("__locale__"$locale$url);
  85.                 if ($locale == "/en/")
  86.                     $locale "en";
  87.                 else
  88.                     $locale == "it";
  89.                 $session $request->getSession();
  90.                 if ($session->get("oldlocale") == null || $session->get("oldlocale") == 1) {
  91.                     $session->set("oldlocale"2);
  92.                   //  $this->log->critical($request->getLocale());
  93.                     $event->setResponse(new RedirectResponse($url301));
  94.                 }
  95.             }
  96.         }
  97.     }
  98.     private function checkLanguage(){
  99.         foreach($this->languages as $language) {
  100.             if (preg_match_all("/\/$language\//"$this->oldUrl)) {
  101.                 return $language;
  102.             }
  103.         }
  104.         return $this->defaultLanguage;
  105.     }
  106.     public static function getSubscribedEvents()
  107.     {
  108.         return array(
  109.             // must be registered before the default Locale listener
  110.             KernelEvents::REQUEST => array(array('onKernelRequest'17)),
  111.         );
  112.     }
  113. }