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

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.     public static function getSubscribedEvents()
  98.     {
  99.         return [
  100.             KernelEvents::EXCEPTION => ['onKernelException', -5],
  101.         ];
  102.     }
  103.     /**
  104.      * @param ExceptionEvent $exceptionEvent
  105.      *
  106.      * @throws \Twig\Error\LoaderError
  107.      * @throws \Twig\Error\RuntimeError
  108.      * @throws \Twig\Error\SyntaxError
  109.      */
  110.     public function onKernelException(ExceptionEvent $exceptionEvent)
  111.     {
  112.         if ($this->portalConfigService->isPortalEngineSite()) {
  113.             $throwable $exceptionEvent->getThrowable(); // @phpstan-ignore-line
  114.             if ($throwable instanceof AccessDeniedHttpException) {
  115.                 $this->setErrorPageResponse(
  116.                     $exceptionEvent,
  117.                     '@PimcorePortalEngine/error_page/access_denied.html.twig',
  118.                     true
  119.                 );
  120.             } elseif ($throwable instanceof PublicShareExpiredException) {
  121.                 $this->setErrorPageResponse(
  122.                     $exceptionEvent,
  123.                     '@PimcorePortalEngine/error_page/public_share_expired.html.twig',
  124.                     true,
  125.                     [
  126.                         'publicShare' => $throwable->getPublicShare()
  127.                     ]
  128.                 );
  129.             } elseif ($throwable instanceof NotFoundHttpException) {
  130.                 $this->setErrorPageResponse(
  131.                     $exceptionEvent,
  132.                     '@PimcorePortalEngine/error_page/404.html.twig',
  133.                     true
  134.                 );
  135.             } else {
  136.                 $this->setErrorPageResponse(
  137.                     $exceptionEvent,
  138.                     '@PimcorePortalEngine/error_page/exception.html.twig',
  139.                     false
  140.                 );
  141.             }
  142.         }
  143.     }
  144.     /**
  145.      * @throws \Twig\Error\LoaderError
  146.      * @throws \Twig\Error\RuntimeError
  147.      * @throws \Twig\Error\SyntaxError
  148.      */
  149.     protected function setErrorPageResponse(ExceptionEvent $exceptionEventstring $templatebool $displayInDebugMode, array $additionalViewParams = [])
  150.     {
  151.         if (!$displayInDebugMode && \Pimcore::inDebugMode()) {
  152.             return;
  153.         }
  154.         $this->prefixService->setupRoutingPrefix();
  155.         if (!$document $this->documentResolver->getDocument($exceptionEvent->getRequest())) {
  156.             $site Site::getCurrentSite();
  157.             $document $this->documentService->getNearestDocumentByPath($site->getRootDocument()->getRealFullPath() . $exceptionEvent->getRequest()->getPathInfo());
  158.             if ($lang $document->getProperty('language')) {
  159.                 $this->localeService->setLocale($lang);
  160.                 if ($this->translator instanceof LocaleAwareInterface) {
  161.                     $this->translator->setLocale($lang);
  162.                 }
  163.             }
  164.             $this->documentResolver->setDocument($exceptionEvent->getRequest(), $document);
  165.         }
  166.         $exceptionEvent->setResponse(
  167.              new Response(
  168.                 $this->twig->render($templatearray_merge([
  169.                     'document' => $document,
  170.                     'editmode' => $this->editmodeResolver->isEditmode($exceptionEvent->getRequest()),
  171.                     'inDebugMode' => \Pimcore::inDebugMode(),
  172.                     'debugInfo' => $exceptionEvent->getThrowable()->getMessage() . '<hr>' $exceptionEvent->getThrowable()->getTraceAsString() // @phpstan-ignore-line
  173.                 ], $additionalViewParams))
  174.             )
  175.         );
  176.     }
  177. }