<?php
declare(strict_types=1);
namespace Hitso\Bundle\CommonBundle\EventListener;
use Doctrine\Common\Util\Debug;
use Hitso\Bundle\MultiSiteBundle\MultiSite\SiteContext;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class HttpSubscriber implements EventSubscriberInterface
{
/**
* @var SiteContext
*/
protected $siteContext;
public function __construct(SiteContext $siteContext)
{
$this->siteContext = $siteContext;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::RESPONSE => 'onKernelResponse',
KernelEvents::REQUEST => ['onKernelRequest', 100],
];
}
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
$site = $this->siteContext->getRunningSite();
if ($site->isContent()) {
$request->setLocale($site->getLocale());
}
}
public function onKernelResponse(FilterResponseEvent $event): void
{
if (!$event->isMasterRequest()) {
return;
}
$event->getResponse()->setCharset('UTF-8');
}
}