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

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\TranslatorInterface;
  27. use Twig\Environment;
  28. /**
  29.  * Class IndexUpdateListener
  30.  *
  31.  * @package Pimcore\Bundle\PortalEngineBundle\EventListener
  32.  */
  33. class ResponseExceptionSubscriber implements EventSubscriberInterface
  34. {
  35.     /**
  36.      * @var PortalConfigService
  37.      */
  38.     protected $portalConfigService;
  39.     /**
  40.      * @var Environment
  41.      */
  42.     protected $twig;
  43.     /**
  44.      * @var DocumentResolver
  45.      */
  46.     protected $documentResolver;
  47.     /**
  48.      * @var EditmodeResolver
  49.      */
  50.     protected $editmodeResolver;
  51.     /**
  52.      * @var PrefixService
  53.      */
  54.     protected $prefixService;
  55.     /**
  56.      * @var Service
  57.      */
  58.     protected $documentService;
  59.     /**
  60.      * @var LocaleServiceInterface
  61.      */
  62.     protected $localeService;
  63.     /**
  64.      * @var TranslatorInterface
  65.      */
  66.     protected $translator;
  67.     /**
  68.      * ResponseExceptionSubscriber constructor.
  69.      *
  70.      * @param PortalConfigService $portalConfigService
  71.      * @param Environment $twig
  72.      * @param DocumentResolver $documentResolver
  73.      * @param EditmodeResolver $editmodeResolver
  74.      * @param PrefixService $prefixService
  75.      * @param Service $documentService
  76.      */
  77.     public function __construct(
  78.         PortalConfigService $portalConfigService,
  79.         Environment $twig,
  80.         DocumentResolver $documentResolver,
  81.         EditmodeResolver $editmodeResolver,
  82.         PrefixService $prefixService,
  83.         Service $documentService,
  84.         LocaleServiceInterface $localeService,
  85.         TranslatorInterface $translator
  86.     ) {
  87.         $this->portalConfigService $portalConfigService;
  88.         $this->twig $twig;
  89.         $this->documentResolver $documentResolver;
  90.         $this->editmodeResolver $editmodeResolver;
  91.         $this->prefixService $prefixService;
  92.         $this->documentService $documentService;
  93.         $this->localeService $localeService;
  94.         $this->translator $translator;
  95.     }
  96.     public static function getSubscribedEvents()
  97.     {
  98.         return [
  99.             KernelEvents::EXCEPTION => ['onKernelException', -5],
  100.         ];
  101.     }
  102.     /**
  103.      * @param ExceptionEvent $exceptionEvent
  104.      *
  105.      * @throws \Twig\Error\LoaderError
  106.      * @throws \Twig\Error\RuntimeError
  107.      * @throws \Twig\Error\SyntaxError
  108.      */
  109.     public function onKernelException(ExceptionEvent $exceptionEvent)
  110.     {
  111.         if ($this->portalConfigService->isPortalEngineSite()) {
  112.             $throwable $exceptionEvent->getThrowable();
  113.             if ($throwable instanceof AccessDeniedHttpException) {
  114.                 $this->setErrorPageResponse(
  115.                     $exceptionEvent,
  116.                     '@PimcorePortalEngine/error_page/access_denied.html.twig',
  117.                     true
  118.                 );
  119.             } elseif ($throwable instanceof PublicShareExpiredException) {
  120.                 $this->setErrorPageResponse(
  121.                     $exceptionEvent,
  122.                     '@PimcorePortalEngine/error_page/public_share_expired.html.twig',
  123.                     true,
  124.                     [
  125.                         'publicShare' => $throwable->getPublicShare()
  126.                     ]
  127.                 );
  128.             } elseif ($throwable instanceof NotFoundHttpException) {
  129.                 $this->setErrorPageResponse(
  130.                     $exceptionEvent,
  131.                     '@PimcorePortalEngine/error_page/404.html.twig',
  132.                     true
  133.                 );
  134.             } else {
  135.                 $this->setErrorPageResponse(
  136.                     $exceptionEvent,
  137.                     '@PimcorePortalEngine/error_page/exception.html.twig',
  138.                     false
  139.                 );
  140.             }
  141.         }
  142.     }
  143.     /**
  144.      * @throws \Twig\Error\LoaderError
  145.      * @throws \Twig\Error\RuntimeError
  146.      * @throws \Twig\Error\SyntaxError
  147.      */
  148.     protected function setErrorPageResponse(ExceptionEvent $exceptionEventstring $templatebool $displayInDebugMode, array $additionalViewParams = [])
  149.     {
  150.         if (!$displayInDebugMode && \Pimcore::inDebugMode()) {
  151.             return;
  152.         }
  153.         $this->prefixService->setupRoutingPrefix();
  154.         if (!$document $this->documentResolver->getDocument($exceptionEvent->getRequest())) {
  155.             $site Site::getCurrentSite();
  156.             $document $this->documentService->getNearestDocumentByPath($site->getRootDocument()->getRealFullPath() . $exceptionEvent->getRequest()->getPathInfo());
  157.             if ($lang $document->getProperty('language')) {
  158.                 $this->localeService->setLocale($lang);
  159.                 $this->translator->setLocale($lang);
  160.             }
  161.             $this->documentResolver->setDocument($exceptionEvent->getRequest(), $document);
  162.         }
  163.         $exceptionEvent->setResponse(
  164.              new Response(
  165.                 $this->twig->render($templatearray_merge([
  166.                     'document' => $document,
  167.                     'editmode' => $this->editmodeResolver->isEditmode($exceptionEvent->getRequest()),
  168.                     'inDebugMode' => \Pimcore::inDebugMode(),
  169.                     'debugInfo' => $exceptionEvent->getThrowable()->getMessage() . '<hr>' $exceptionEvent->getThrowable()->getTraceAsString()
  170.                 ], $additionalViewParams))
  171.             )
  172.         );
  173.     }
  174. }