<?php
namespace App\EventSubscriber;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class LocaleSubscriber implements EventSubscriberInterface
{
private $markets;
private $domains = [];
private $market = null;
private $locale = null;
public function __construct(ParameterBagInterface $config)
{
$this->markets = $config->get('markets');
}
public function defineMarket($request, $resource)
{
foreach ($this->markets as $keyMarket => $market) {
foreach ($market['domains'] as $keyLocale => $domainByLocales) {
foreach ($domainByLocales as $domain) {
$this->domains[] = $domain;
if ($resource == $domain) {
$request->getSession()->set('market', $this->markets[$keyMarket]);
$this->setMarket($this->markets[$keyMarket]);
$this->setLocale($keyLocale);
}
}
}
}
}
public function getHostAndLocale(RequestEvent $event)
{
$request = $event->getRequest();
if (!$request->getHttpHost()) {
return;
}
$resource = $request->getHttpHost();
$resourceWithScheme = $request->getSchemeAndHttpHost();
$scheme = $request->getScheme();
$this->defineMarket($request, $resource);
if (!is_null($this->getMarket()) && !is_null($this->getLocale())) {
$request->setLocale($this->getLocale());
putenv('HOST_URL_'.strtoupper($this->getLocale()).'='.$resource);
putenv('SITE_URL_'.strtoupper($this->getLocale()).'='.$resourceWithScheme);
$key = array_search($resource, $this->getMarket()['domains'][$this->getLocale()]);
foreach ($this->getMarket()['domains'] as $keyLocale => $domainByLocale) {
if ($keyLocale != $this->getLocale()) {
putenv('HOST_URL_'.strtoupper($keyLocale).'='.$resource);
putenv('SITE_URL_'.strtoupper($keyLocale).'='.$scheme.'://'.$domainByLocale[$key]);
}
}
} else {
$request->setLocale('ru');
dd('Pwel otsedova, prilo>|<enie rabotaet tok dlya:', $this->domains);
}
}
public function clearFavoriteLocale(RequestEvent $event)
{
$request = $event->getRequest();
$favoriteLocale = 'favorite_locale_'.strtolower($this->getMarket()['name']);
if ($request->get('change-locale')) {
$domain = getenv('HOST_URL_'.strtoupper($request->get('change-locale')));
$response = new Response(null, 301);
$cookie = new Cookie($favoriteLocale, $request->get('change-locale'), (time() + 31536000), '/', $domain, false, false);
$response->headers->setCookie($cookie);
$requestQueryParams = $request->query->all();
if (array_key_exists('change-locale', $requestQueryParams)) {
unset($requestQueryParams['change-locale']);
}
$newLocation = $request->getBaseUrl() . $request->getPathInfo();
if (count($requestQueryParams) > 0) {
$newLocation .= '?' . http_build_query($requestQueryParams);
}
$response->headers->set('Location', $newLocation);
$response->send();
}
}
public function checkRedirect(RequestEvent $event)
{
$request = $event->getRequest();
$favoriteLocale = 'favorite_locale_'.strtolower($this->getMarket()['name']);
if ($request->cookies->has($favoriteLocale)) {
if ($request->cookies->get($favoriteLocale) != $request->getLocale()) {
$url = getenv('SITE_URL_'.strtoupper($request->cookies->get($favoriteLocale)));
$url .= $request->getBaseUrl() . $request->getPathInfo();
$response = new Response();
$response->headers->set('Location', $url);
$response->send();
}
}
}
public function authAutoLogin(RequestEvent $event)
{
$request = $event->getRequest();
if ($request->get('autologin')) {
$domain = getenv('HOST_URL_'.strtoupper($request->get('autologin')));
$response = new Response(null, 301);
if ($request->get('authautologin')) {
$cookie = new Cookie('authautologin', $request->get('authautologin'), (time() + 31536000), '/', $domain, false, false);
$response->headers->setCookie($cookie);
} else {
$response->headers->removeCookie('authautologin', '/', $domain);
}
$requestQueryParams = $request->query->all();
if (array_key_exists('autologin', $requestQueryParams)) {
unset($requestQueryParams['autologin']);
}
if (array_key_exists('authautologin', $requestQueryParams)) {
unset($requestQueryParams['authautologin']);
}
$newLocation = $request->getBaseUrl() . $request->getPathInfo();
if (count($requestQueryParams) > 0) {
$newLocation .= '?' . http_build_query($requestQueryParams);
}
$response->headers->set('Location', $newLocation);
$response->send();
}
}
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => [
['getHostAndLocale', 103],
['clearFavoriteLocale', 102],
// ['checkRedirect', 101],
['authAutoLogin', 101],
],
];
}
public function getMarket(): ?array
{
return $this->market;
}
public function setMarket(?array $market): void
{
$this->market = $market;
}
public function getLocale(): ?string
{
return $this->locale;
}
public function setLocale(?string $locale): void
{
$this->locale = $locale;
}
}