<?php
// src/EventListener/LocaleListener.php
namespace App\EventListener;
use phpDocumentor\Reflection\Types\Array_;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Q;
use Symfony\Component\HttpKernel\KernelInterface;
use App\Entity\Pagina;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Psr\Log\LoggerInterface;
class LocaleListener implements EventSubscriberInterface
{
private $routeCollection;
/**
* @var urlMatcher \Symfony\Component\Routing\Matcher\UrlMatcher;
*/
private $urlMatcher;
private $oldUrl;
private $newUrl;
private $languages;
private $defaultLanguage;
private $router;
private $log;
public function __construct(RouterInterface $router, $defaultLanguage = 'it',EntityManagerInterface $_em,LoggerInterface $logger)
{
$this->em=$_em;
$this->routeCollection = $router->getRouteCollection();
$this->languages = Array("it","en","fr");
$this->defaultLanguage = $defaultLanguage;
$this->router=$router;
$this->log = $logger;
$context = new RequestContext("/");
}
public function onKernelRequest(GetResponseEvent $event)
{
//GOAL:
// Redirect all incoming requests to their /locale/route equivalent when exists.
// Do nothing if it already has /locale/ in the route to prevent redirect loops
// Do nothing if the route requested has no locale param
$request = $event->getRequest();
$this->newUrl = $request->getPathInfo();
$url=$this->newUrl;
$this->oldUrl = $this->newUrl;
$locale = $this->checkLanguage();
//var_dump($locale);
if($locale===null)
$locale="it";
$locales=$locale;
$this->newUrl = $request->getPathInfo();
$pos=strrpos($this->newUrl,"/");
$id= substr($this->newUrl,$pos+1);
if(strpos($url,"/it/pagina")>-1 || strpos($url,"/en/page")>-1 || strpos($url,"/fr/page")>-1) {
{
$repopagina = $this->em->getRepository(Pagina::class);
$pagina = $repopagina->find($id);
$url = "";
$pathLocale = $this->newUrl;
//We have to catch the ResourceNotFoundException
//Try to match the path with the local prefi
if ($locale == "it") {
$url = "https://www.eurobagno.com/public/index.php/__locale__/pagina/" . $pagina->translate($locale)->geturl() . "/__id__";
} elseif ($locale == "en") {
$url = "https://www.eurobagno.com/public/index.php/__locale__/page/" . $pagina->translate($locale)->geturl() . "/__id__";
} else {
$url = "https://www.eurobagno.com/public/index.php/__locale__/page/" . $pagina->translate($locale)->geturl() . "/__id__";
}
$url = str_replace("__id__", $pagina->getId(), $url);
$url = str_replace("__locale__", $locale, $url);
/* if ($locale == "it") {
$url = "http://localhost/eurobagno/public/index.php/__locale__/pagina/" . $pagina->translate($locale)->geturl(). "/__id__";
} elseif ($locale == "en") {
$url = "http://localhost/eurobagno/public/index.php/__locale__/page/" . $pagina->translate($locale)->geturl(). "/__id__";
} else {
$url = "http://localhost/eurobagno/public/index.php/__locale__/page/" . $pagina->translate($locale)->geturl() . "/__id__";
}*/
$url = str_replace("__id__", $pagina->getId(), $url);
$url = str_replace("__locale__", $locale, $url);
if ($locale == "/en/")
$locale = "en";
else
$locale == "it";
$session = $request->getSession();
if ($session->get("oldlocale") == null || $session->get("oldlocale") == 1) {
$session->set("oldlocale", 2);
// $this->log->critical($request->getLocale());
$event->setResponse(new RedirectResponse($url, 301));
}
}
}
}
private function checkLanguage(){
foreach($this->languages as $language) {
if (preg_match_all("/\/$language\//", $this->oldUrl)) {
return $language;
}
}
return $this->defaultLanguage;
}
public static function getSubscribedEvents()
{
return array(
// must be registered before the default Locale listener
KernelEvents::REQUEST => array(array('onKernelRequest', 17)),
);
}
}