vendor/pimcore/portal-engine/src/EventSubscriber/ResponseExceptionSubscriber.php line 127

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under following license:
  6.  * - Pimcore Commercial License (PCL)
  7.  *
  8.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  9.  *  @license    http://www.pimcore.org/license     PCL
  10.  */
  11. namespace Pimcore\Bundle\PortalEngineBundle\EventSubscriber;
  12. use Pimcore\Bundle\PortalEngineBundle\Exception\PublicShareExpiredException;
  13. use Pimcore\Bundle\PortalEngineBundle\Service\Document\PrefixService;
  14. use Pimcore\Bundle\PortalEngineBundle\Service\PortalConfig\PortalConfigService;
  15. use Pimcore\Http\Request\Resolver\DocumentResolver;
  16. use Pimcore\Http\Request\Resolver\EditmodeResolver;
  17. use Pimcore\Localization\LocaleServiceInterface;
  18. use Pimcore\Model\Document\Service;
  19. use Pimcore\Model\Site;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  23. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  24. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  25. use Symfony\Component\HttpKernel\KernelEvents;
  26. use Symfony\Contracts\Translation\LocaleAwareInterface;
  27. use Symfony\Contracts\Translation\TranslatorInterface;
  28. use Twig\Environment;
  29. /**
  30.  * Class IndexUpdateListener
  31.  *
  32.  * @package Pimcore\Bundle\PortalEngineBundle\EventListener
  33.  */
  34. class ResponseExceptionSubscriber implements EventSubscriberInterface
  35. {
  36.     /**
  37.      * @var PortalConfigService
  38.      */
  39.     protected $portalConfigService;
  40.     /**
  41.      * @var Environment
  42.      */
  43.     protected $twig;
  44.     /**
  45.      * @var DocumentResolver
  46.      */
  47.     protected $documentResolver;
  48.     /**
  49.      * @var EditmodeResolver
  50.      */
  51.     protected $editmodeResolver;
  52.     /**
  53.      * @var PrefixService
  54.      */
  55.     protected $prefixService;
  56.     /**
  57.      * @var Service
  58.      */
  59.     protected $documentService;
  60.     /**
  61.      * @var LocaleServiceInterface
  62.      */
  63.     protected $localeService;
  64.     /**
  65.      * @var TranslatorInterface
  66.      */
  67.     protected $translator;
  68.     /**
  69.      * ResponseExceptionSubscriber constructor.
  70.      *
  71.      * @param PortalConfigService $portalConfigService
  72.      * @param Environment $twig
  73.      * @param DocumentResolver $documentResolver
  74.      * @param EditmodeResolver $editmodeResolver
  75.      * @param PrefixService $prefixService
  76.      * @param Service $documentService
  77.      */
  78.     public function __construct(
  79.         PortalConfigService $portalConfigService,
  80.         Environment $twig,
  81.         DocumentResolver $documentResolver,
  82.         EditmodeResolver $editmodeResolver,
  83.         PrefixService $prefixService,
  84.         Service $documentService,
  85.         LocaleServiceInterface $localeService,
  86.         TranslatorInterface $translator
  87.     ) {
  88.         $this->portalConfigService $portalConfigService;
  89.         $this->twig $twig;
  90.         $this->documentResolver $documentResolver;
  91.         $this->editmodeResolver $editmodeResolver;
  92.         $this->prefixService $prefixService;
  93.         $this->documentService $documentService;
  94.         $this->localeService $localeService;
  95.         $this->translator $translator;
  96.     }
  97.     /**
  98.      * @return array
  99.      */
  100.     public static function getSubscribedEvents()
  101.     {
  102.         return [
  103.             KernelEvents::EXCEPTION => ['onKernelException', -5],
  104.         ];
  105.     }
  106.     /**
  107.      * @param ExceptionEvent $exceptionEvent
  108.      *
  109.      * @throws \Twig\Error\LoaderError
  110.      * @throws \Twig\Error\RuntimeError
  111.      * @throws \Twig\Error\SyntaxError
  112.      */
  113.     public function onKernelException(ExceptionEvent $exceptionEvent)
  114.     {
  115.         if ($this->portalConfigService->isPortalEngineSite()) {
  116.             $throwable $exceptionEvent->getThrowable(); // @phpstan-ignore-line
  117.             if ($throwable instanceof AccessDeniedHttpException) {
  118.                 $this->setErrorPageResponse(
  119.                     $exceptionEvent,
  120.                     '@PimcorePortalEngine/error_page/access_denied.html.twig',
  121.                     true
  122.                 );
  123.             } elseif ($throwable instanceof PublicShareExpiredException) {
  124.                 $this->setErrorPageResponse(
  125.                     $exceptionEvent,
  126.                     '@PimcorePortalEngine/error_page/public_share_expired.html.twig',
  127.                     true,
  128.                     [
  129.                         'publicShare' => $throwable->getPublicShare()
  130.                     ]
  131.                 );
  132.             } elseif ($throwable instanceof NotFoundHttpException) {
  133.                 $this->setErrorPageResponse(
  134.                     $exceptionEvent,
  135.                     '@PimcorePortalEngine/error_page/404.html.twig',
  136.                     true
  137.                 );
  138.             } else {
  139.                 $this->setErrorPageResponse(
  140.                     $exceptionEvent,
  141.                     '@PimcorePortalEngine/error_page/exception.html.twig',
  142.                     false
  143.                 );
  144.             }
  145.         }
  146.     }
  147.     /**
  148.      * @throws \Twig\Error\LoaderError
  149.      * @throws \Twig\Error\RuntimeError
  150.      * @throws \Twig\Error\SyntaxError
  151.      */
  152.     protected function setErrorPageResponse(ExceptionEvent $exceptionEventstring $templatebool $displayInDebugMode, array $additionalViewParams = [])
  153.     {
  154.         if (!$displayInDebugMode && \Pimcore::inDebugMode()) {
  155.             return;
  156.         }
  157.         $this->prefixService->setupRoutingPrefix();
  158.         if (!$document $this->documentResolver->getDocument($exceptionEvent->getRequest())) {
  159.             $site Site::getCurrentSite();
  160.             $document $this->documentService->getNearestDocumentByPath($site->getRootDocument()->getRealFullPath() . $exceptionEvent->getRequest()->getPathInfo());
  161.             if ($lang $document->getProperty('language')) {
  162.                 $this->localeService->setLocale($lang);
  163.                 if ($this->translator instanceof LocaleAwareInterface) {
  164.                     $this->translator->setLocale($lang);
  165.                 }
  166.             }
  167.             $this->documentResolver->setDocument($exceptionEvent->getRequest(), $document);
  168.         }
  169.         $exceptionEvent->setResponse(
  170.              new Response(
  171.                 $this->twig->render($templatearray_merge([
  172.                     'document' => $document,
  173.                     'editmode' => $this->editmodeResolver->isEditmode($exceptionEvent->getRequest()),
  174.                     'inDebugMode' => \Pimcore::inDebugMode(),
  175.                     'debugInfo' => $exceptionEvent->getThrowable()->getMessage() . '<hr>' $exceptionEvent->getThrowable()->getTraceAsString() // @phpstan-ignore-line
  176.                 ], $additionalViewParams))
  177.             )
  178.         );
  179.     }
  180. }