bundles/Bgt/WebBundle/EventSubscriber/WebEventSubscriber.php line 37

  1. <?php
  2. namespace Bgt\WebBundle\EventSubscriber;
  3. use App\Services\LanguageService;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Symfony\Component\Translation\LocaleSwitcher;
  11. use Twig\Environment;
  12. class WebEventSubscriber implements EventSubscriberInterface
  13. {
  14.     protected ?Environment $twigEnv;
  15.     protected LocaleSwitcher $localeSwitcher;
  16.     protected LanguageService $languageService;
  17.     public function __construct(Environment $twigEnvLocaleSwitcher $localeSwitcherLanguageService $languageService)
  18.     {
  19.         $this->twigEnv $twigEnv;
  20.         $this->localeSwitcher $localeSwitcher;
  21.         $this->languageService $languageService;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             KernelEvents::EXCEPTION => 'onKernelException',
  27.             KernelEvents::REQUEST => 'onKernelRequest',
  28.         ];
  29.     }
  30.     public function onKernelRequest(RequestEvent $event): void
  31.     {
  32.         $route $event->getRequest()->get('_route');
  33.         if ($route) {
  34.             $prefix "web_";
  35.             if (!str_starts_with($route$prefix)) {
  36.                 return;
  37.             }
  38.         }
  39.         $request $event->getRequest();
  40.         $locale $request->cookies->get('_locale''en');
  41.         if (!$this->languageService->isLangValid($locale)) {
  42.             $locale 'en';
  43.         }
  44.         $request->setLocale($locale);
  45.         $this->localeSwitcher->setLocale($locale);
  46.     }
  47.     public function onKernelException(ExceptionEvent $event): void
  48.     {
  49.         if ($_ENV['APP_ENV'] === 'dev') {
  50.             return;
  51.         }
  52.         $route $event->getRequest()->get('_route');
  53.         if ($route) {
  54.             $prefix "web_";
  55.             if (!str_starts_with($route$prefix)) {
  56.                 return;
  57.             }
  58.         }
  59.         $exception $event->getThrowable();
  60.         $response = new Response();
  61.         if ($exception instanceof NotFoundHttpException) {
  62.             $response->setStatusCode(404);
  63.             $response->setContent($this->twigEnv->render('@BgtWeb/exception/error.html.twig', [
  64.                 'status' => [
  65.                     'code' => 404,
  66.                     'message' => "Таны хайсан мэдээлэл олдсонгүй",
  67.                 ],
  68.             ]));
  69.         } else {
  70.             $response->setStatusCode(500);
  71.             $response->setContent($this->twigEnv->render('@BgtWeb/exception/error.html.twig', [
  72.                 'status' => [
  73.                     'code' => 500,
  74.                     'message' => "Something is wrong contact admin",
  75.                 ],
  76.             ]));
  77.         }
  78.         $event->setResponse($response);
  79.     }
  80. }