<?php
namespace App\EventSubscriber;
use App\Tools\LocaleHelper;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class ExceptionSubscriber implements EventSubscriberInterface
{
private $localeHelper;
public function __construct(LocaleHelper $localeHelper)
{
$this->localeHelper = $localeHelper;
}
public function onKernelException(ExceptionEvent $event)
{
$request = $event->getRequest();
$locale = $this->localeHelper->guessLocaleFromRequest($request);
if (!empty($locale)) {
$request->setLocale($locale);
}
}
public static function getSubscribedEvents()
{
return [
KernelEvents::EXCEPTION => [
['onKernelException', 60]
]
];
}
}