<?php
declare(strict_types=1);
namespace Juki\Bundle\AppBundle\EventListener;
use Hitso\Bundle\MultiSiteBundle\MultiSite\SiteCollection;
use Hitso\Bundle\MultiSiteBundle\MultiSite\SiteContext;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Class LocaleSubscriber
*
* @package Juki\Bundle\AppBundle\EventListener
*/
final class LocaleSubscriber implements EventSubscriberInterface
{
/**
* @var SiteCollection
*/
protected $siteCollection;
/**
* @var SiteContext
*/
protected $siteContext;
public function __construct(SiteCollection $siteCollection, SiteContext $siteContext)
{
$this->siteCollection = $siteCollection;
$this->siteContext = $siteContext;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => ['onKernelRequest', 8],
];
}
public function onKernelRequest(RequestEvent $event)
{
if ($event->getRequestType() == HttpKernelInterface::SUB_REQUEST) {
return;
}
$request = $event->getRequest();
if ($request->headers->has('locale')) {
$locale = (string) $request->headers->get('locale', 'pl');
if ($this->siteCollection->has($locale)) {
$site = $this->siteCollection->get($locale);
$this->siteContext->setRunningSite($site);
}
}
}
}