pimcore/lib/Pimcore/Bundle/CoreBundle/EventListener/ResponseExceptionListener.php line 66

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  * @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace Pimcore\Bundle\CoreBundle\EventListener;
  15. use Pimcore\Bundle\CoreBundle\EventListener\Traits\PimcoreContextAwareTrait;
  16. use Pimcore\Config;
  17. use Pimcore\Http\Exception\ResponseException;
  18. use Pimcore\Http\Request\Resolver\PimcoreContextResolver;
  19. use Pimcore\Model\Document;
  20. use Pimcore\Model\Site;
  21. use Pimcore\Templating\Renderer\ActionRenderer;
  22. use Psr\Log\LoggerAwareTrait;
  23. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  24. use Symfony\Component\HttpFoundation\Response;
  25. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  26. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  27. use Symfony\Component\HttpKernel\KernelEvents;
  28. class ResponseExceptionListener implements EventSubscriberInterface
  29. {
  30.     use LoggerAwareTrait;
  31.     use PimcoreContextAwareTrait;
  32.     /**
  33.      * @var ActionRenderer
  34.      */
  35.     protected $actionRenderer;
  36.     /**
  37.      * @var bool
  38.      */
  39.     protected $renderErrorPage true;
  40.     /**
  41.      * @param ActionRenderer $actionRenderer
  42.      * @param bool $renderErrorPage
  43.      */
  44.     public function __construct(ActionRenderer $actionRenderer$renderErrorPage true)
  45.     {
  46.         $this->actionRenderer  $actionRenderer;
  47.         $this->renderErrorPage = (bool)$renderErrorPage;
  48.     }
  49.     /**
  50.      * @inheritDoc
  51.      */
  52.     public static function getSubscribedEvents()
  53.     {
  54.         return [
  55.             KernelEvents::EXCEPTION => 'onKernelException'
  56.         ];
  57.     }
  58.     public function onKernelException(GetResponseForExceptionEvent $event)
  59.     {
  60.         $exception $event->getException();
  61.         // handle ResponseException (can be used from any context)
  62.         if ($exception instanceof ResponseException) {
  63.             $event->setResponse($exception->getResponse());
  64.             // a response was explicitely set -> do not continue to error page
  65.             return;
  66.         }
  67.         // further checks are only valid for default context
  68.         $request $event->getRequest();
  69.         if ($this->matchesPimcoreContext($requestPimcoreContextResolver::CONTEXT_DEFAULT)) {
  70.             if ($this->renderErrorPage) {
  71.                 $this->handleErrorPage($event);
  72.             }
  73.         }
  74.     }
  75.     protected function handleErrorPage(GetResponseForExceptionEvent $event)
  76.     {
  77.         if (\Pimcore::inDebugMode() || PIMCORE_DEVMODE) {
  78.             return;
  79.         }
  80.         $exception $event->getException();
  81.         $statusCode 500;
  82.         $headers    = [];
  83.         if ($exception instanceof HttpExceptionInterface) {
  84.             $statusCode $exception->getStatusCode();
  85.             $header     $exception->getHeaders();
  86.         }
  87.         $errorPath Config::getSystemConfig()->documents->error_pages->default;
  88.         if (Site::isSiteRequest()) {
  89.             $site      Site::getCurrentSite();
  90.             $errorPath $site->getErrorDocument();
  91.         }
  92.         if (empty($errorPath)) {
  93.             $errorPath '/';
  94.         }
  95.         $document Document::getByPath($errorPath);
  96.         if (!$document instanceof Document\Page) {
  97.             // default is home
  98.             $document Document::getById(1);
  99.         }
  100.         try {
  101.             $response $this->actionRenderer->render($document);
  102.         } catch (\Exception $e) {
  103.             // we are even not able to render the error page, so we send the client a unicorn
  104.             $response 'Page not found. ðŸ¦„';
  105.             $this->logger->emergency('Unable to render error page, exception thrown');
  106.             $this->logger->emergency($e);
  107.         }
  108.         $event->setResponse(new Response($response$statusCode$headers));
  109.     }
  110. }