src/CmsBundle/Controller/ErrorController.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\CmsBundle\Controller;
  3. use App\CmsBundle\Enum\RobotsValue;
  4. use App\CmsBundle\Service\MenuService;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as SymfonyAbstractController;
  6. use Symfony\Component\ErrorHandler\Exception\FlattenException;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
  10. use App\CmsBundle\Service\OptionResolverService;
  11. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  12. class ErrorController extends SymfonyAbstractController
  13. {
  14.     protected const TEMPLATE '/exception/error.html.twig';
  15.     public function __construct(
  16.         protected readonly OptionResolverService $optionResolver,
  17.         protected readonly MenuService $menuService,
  18.         protected readonly TokenStorageInterface $tokenStorage
  19.     ) {}
  20.     public function index(
  21.         Request $request,
  22.         FlattenException $exception,
  23.         DebugLoggerInterface $logger null
  24.     ) {
  25.         $site $this->optionResolver->getSite();
  26.         $isNotFound = ($exception->getStatusCode() == 404);
  27.         $robots $isNotFound
  28.             RobotsValue::NOINDEX_FOLLOW
  29.             RobotsValue::INDEX_FOLLOW;
  30.         $parameters = [
  31.             'menu'      => $this->menuService->getMenu(),
  32.             'site'      => $site,
  33.             'template'  => $site->getTemplate(),
  34.             'exception' => $exception,
  35.             'clientLocation'  => null,
  36.             'seo'       => ['robots' => $robots],
  37.             'is_404_page' => $isNotFound,
  38.             'parameters' => [
  39.                 'user' => $this->tokenStorage->getToken()?->getUser()
  40.             ]
  41.         ];
  42.         if ($this->optionResolver->getSite()->getTemplate() === 'midori') {
  43.             $path $this->optionResolver->getProjectDir()
  44.                 . 'templates/'
  45.                 $this->optionResolver->getSite()->getTemplate()
  46.                 . '/exception/style.css';
  47.             if (file_exists($path)) {
  48.                 $cssContent file_get_contents($path);
  49.                 $response = new Response(
  50.                     $this->render(
  51.                         $this->optionResolver->getSite()->getTemplate() . static::TEMPLATE,
  52.                         $parameters
  53.                     )->getContent(),
  54.                     $exception->getStatusCode()
  55.                 );
  56.                 $response->headers->set('Content-Type''text/html');
  57.                 $response->setContent(str_replace('</head>'"<style>$cssContent</style></head>"$response->getContent()));
  58.                 return $response;
  59.             }
  60.         }
  61.         return new Response(
  62.             $this->render(
  63.                 $this->optionResolver->getSite()->getTemplate() . static::TEMPLATE,
  64.                 $parameters,
  65.             )->getContent(),
  66.             $exception->getStatusCode()
  67.         );
  68.     }
  69. }