src/Juki/Bundle/AppBundle/EventListener/LocaleSubscriber.php line 44

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Juki\Bundle\AppBundle\EventListener;
  4. use Hitso\Bundle\MultiSiteBundle\MultiSite\SiteCollection;
  5. use Hitso\Bundle\MultiSiteBundle\MultiSite\SiteContext;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\HttpKernel\HttpKernelInterface;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. /**
  11.  * Class LocaleSubscriber
  12.  *
  13.  * @package Juki\Bundle\AppBundle\EventListener
  14.  */
  15. final class LocaleSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var SiteCollection
  19.      */
  20.     protected $siteCollection;
  21.     /**
  22.      * @var SiteContext
  23.      */
  24.     protected $siteContext;
  25.     public function __construct(SiteCollection $siteCollectionSiteContext $siteContext)
  26.     {
  27.         $this->siteCollection $siteCollection;
  28.         $this->siteContext    $siteContext;
  29.     }
  30.     public static function getSubscribedEvents()
  31.     {
  32.         return [
  33.             KernelEvents::REQUEST => ['onKernelRequest'8],
  34.         ];
  35.     }
  36.     public function onKernelRequest(RequestEvent $event)
  37.     {
  38.         if ($event->getRequestType() == HttpKernelInterface::SUB_REQUEST) {
  39.             return;
  40.         }
  41.         $request $event->getRequest();
  42.         if ($request->headers->has('locale')) {
  43.             $locale = (string) $request->headers->get('locale''pl');
  44.             if ($this->siteCollection->has($locale)) {
  45.                 $site $this->siteCollection->get($locale);
  46.                 $this->siteContext->setRunningSite($site);
  47.             }
  48.         }
  49.     }
  50. }