src/EventSubscriber/LocaleSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\Cookie;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. class LocaleSubscriber implements EventSubscriberInterface
  10. {
  11.     private $markets;
  12.     private $domains = [];
  13.     private $market null;
  14.     private $locale null;
  15.     public function __construct(ParameterBagInterface $config)
  16.     {
  17.         $this->markets $config->get('markets');
  18.     }
  19.     public function defineMarket($request$resource)
  20.     {
  21.         foreach ($this->markets as $keyMarket => $market) {
  22.             foreach ($market['domains'] as $keyLocale => $domainByLocales) {
  23.                 foreach ($domainByLocales as $domain) {
  24.                     $this->domains[] = $domain;
  25.                     if ($resource == $domain) {
  26.                         $request->getSession()->set('market'$this->markets[$keyMarket]);
  27.                         $this->setMarket($this->markets[$keyMarket]);
  28.                         $this->setLocale($keyLocale);
  29.                     }
  30.                 }
  31.             }
  32.         }
  33.     }
  34.     public function getHostAndLocale(RequestEvent $event)
  35.     {
  36.         $request $event->getRequest();
  37.         if (!$request->getHttpHost()) {
  38.             return;
  39.         }
  40.         $resource $request->getHttpHost();
  41.         $resourceWithScheme $request->getSchemeAndHttpHost();
  42.         $scheme $request->getScheme();
  43.         $this->defineMarket($request$resource);
  44.         if (!is_null($this->getMarket()) && !is_null($this->getLocale())) {
  45.             $request->setLocale($this->getLocale());
  46.             putenv('HOST_URL_'.strtoupper($this->getLocale()).'='.$resource);
  47.             putenv('SITE_URL_'.strtoupper($this->getLocale()).'='.$resourceWithScheme);
  48.             $key array_search($resource$this->getMarket()['domains'][$this->getLocale()]);
  49.             foreach ($this->getMarket()['domains'] as $keyLocale => $domainByLocale) {
  50.                 if ($keyLocale != $this->getLocale()) {
  51.                     putenv('HOST_URL_'.strtoupper($keyLocale).'='.$resource);
  52.                     putenv('SITE_URL_'.strtoupper($keyLocale).'='.$scheme.'://'.$domainByLocale[$key]);
  53.                 }
  54.             }
  55.         } else {
  56.             $request->setLocale('ru');
  57.             dd('Pwel otsedova, prilo>|<enie rabotaet tok dlya:'$this->domains);
  58.         }
  59.     }
  60.     public function clearFavoriteLocale(RequestEvent $event)
  61.     {
  62.         $request $event->getRequest();
  63.         $favoriteLocale 'favorite_locale_'.strtolower($this->getMarket()['name']);
  64.         if ($request->get('change-locale')) {
  65.             $domain getenv('HOST_URL_'.strtoupper($request->get('change-locale')));
  66.             $response = new Response(null301);
  67.             $cookie = new Cookie($favoriteLocale$request->get('change-locale'), (time() + 31536000), '/'$domainfalsefalse);
  68.             $response->headers->setCookie($cookie);
  69.             $requestQueryParams $request->query->all();
  70.             if (array_key_exists('change-locale'$requestQueryParams)) {
  71.                 unset($requestQueryParams['change-locale']);
  72.             }
  73.             $newLocation $request->getBaseUrl() . $request->getPathInfo();
  74.             if (count($requestQueryParams) > 0) {
  75.                 $newLocation .= '?' http_build_query($requestQueryParams);
  76.             }
  77.             $response->headers->set('Location'$newLocation);
  78.             $response->send();
  79.         }
  80.     }
  81.     public function checkRedirect(RequestEvent $event)
  82.     {
  83.         $request $event->getRequest();
  84.         $favoriteLocale 'favorite_locale_'.strtolower($this->getMarket()['name']);
  85.         if ($request->cookies->has($favoriteLocale)) {
  86.             if ($request->cookies->get($favoriteLocale) != $request->getLocale()) {
  87.                 $url getenv('SITE_URL_'.strtoupper($request->cookies->get($favoriteLocale)));
  88.                 $url .= $request->getBaseUrl() . $request->getPathInfo();
  89.                 $response = new Response();
  90.                 $response->headers->set('Location'$url);
  91.                 $response->send();
  92.             }
  93.         }
  94.     }
  95.     public function authAutoLogin(RequestEvent $event)
  96.     {
  97.         $request $event->getRequest();
  98.         if ($request->get('autologin')) {
  99.             $domain getenv('HOST_URL_'.strtoupper($request->get('autologin')));
  100.             $response = new Response(null301);
  101.             if ($request->get('authautologin')) {
  102.                 $cookie = new Cookie('authautologin'$request->get('authautologin'), (time() + 31536000), '/'$domainfalsefalse);
  103.                 $response->headers->setCookie($cookie);
  104.             } else {
  105.                 $response->headers->removeCookie('authautologin''/'$domain);
  106.             }
  107.             $requestQueryParams $request->query->all();
  108.             if (array_key_exists('autologin'$requestQueryParams)) {
  109.                 unset($requestQueryParams['autologin']);
  110.             }
  111.             if (array_key_exists('authautologin'$requestQueryParams)) {
  112.                 unset($requestQueryParams['authautologin']);
  113.             }
  114.             $newLocation $request->getBaseUrl() . $request->getPathInfo();
  115.             if (count($requestQueryParams) > 0) {
  116.                 $newLocation .= '?' http_build_query($requestQueryParams);
  117.             }
  118.             $response->headers->set('Location'$newLocation);
  119.             $response->send();
  120.         }
  121.     }
  122.     public static function getSubscribedEvents()
  123.     {
  124.         return [
  125.             KernelEvents::REQUEST => [
  126.                 ['getHostAndLocale'103],
  127.                 ['clearFavoriteLocale'102],
  128. //                ['checkRedirect', 101],
  129.                 ['authAutoLogin'101],
  130.             ],
  131.         ];
  132.     }
  133.     public function getMarket(): ?array
  134.     {
  135.         return $this->market;
  136.     }
  137.     public function setMarket(?array $market): void
  138.     {
  139.         $this->market $market;
  140.     }
  141.     public function getLocale(): ?string
  142.     {
  143.         return $this->locale;
  144.     }
  145.     public function setLocale(?string $locale): void
  146.     {
  147.         $this->locale $locale;
  148.     }
  149. }