src/AppBundle/EventListener/ResponseExceptionListener.php line 49

Open in your IDE?
  1. <?php
  2. namespace AppBundle\EventListener;
  3. use Pimcore\Http\Request\Resolver\PimcoreContextResolver;
  4. use Pimcore\Model\Document;
  5. use Pimcore\Templating\Renderer\ActionRenderer;
  6. use Pimcore\Bundle\CoreBundle\EventListener\Traits\PimcoreContextAwareTrait;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  9. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. class ResponseExceptionListener
  12. {
  13.     use PimcoreContextAwareTrait;
  14.     /**
  15.      * @var ActionRenderer
  16.      */
  17.     protected $actionRenderer;
  18.     /**
  19.      * @var Document\Service
  20.      */
  21.     protected $documentService;
  22.     /**
  23.      * @param ActionRenderer       $actionRenderer
  24.      * @param Document\Service     $documentService
  25.      * @param array                $pimcoreConfig
  26.      */
  27.     public function __construct(
  28.         ActionRenderer $actionRenderer,
  29.         Document\Service $documentService,
  30.         array $pimcoreConfig
  31.     ) {
  32.         $this->actionRenderer $actionRenderer;
  33.         $this->documentService $documentService;
  34.         $this->pimcoreConfig $pimcoreConfig;
  35.     }
  36.     /**
  37.      * @param ExceptionEvent $event
  38.      *
  39.      * @throws \Exception
  40.      */
  41.     public function onKernelException(ExceptionEvent $event)
  42.     {
  43.         // $exception = $event->getException();
  44.         // // handle ResponseException (can be used from any context)
  45.         // if ($exception instanceof ResponseException) {
  46.         //     $event->setResponse($exception->getResponse());
  47.         //     return;
  48.         // }
  49.         // $request = $event->getRequest();
  50.         // if ($this->matchesPimcoreContext($request, PimcoreContextResolver::CONTEXT_DEFAULT)) {
  51.         //     $this->handleErrorPage($event);
  52.         // }
  53.     }
  54.     /**
  55.      * @param ExceptionEvent $event
  56.      *
  57.      * @throws \Exception
  58.      */
  59.     protected function handleErrorPage(ExceptionEvent $event)
  60.     {
  61.         if (\Pimcore::inDebugMode()) {
  62.             return;
  63.         }
  64.         $exception $event->getThrowable();
  65.         $request $event->getRequest();
  66.         if (\Pimcore\Model\Site::isSiteRequest()) {
  67.             // We expect all requests to be site requests as all the content is part of a site
  68.             $site \Pimcore\Model\Site::getCurrentSite();
  69.             $siteDocument $site->getRootDocument();
  70.             if ($siteDocument) {
  71.                 // The nearest document of the request is use to extract the language from the path
  72.                 $internalPath $siteDocument->getRealFullPath() . $request->getPathInfo();
  73.                 $nearestDocument $this->documentService->getNearestDocumentByPath($internalPath);
  74.                 if ($nearestDocument) {
  75.                     // Now we search for the error document based on the locale and status code
  76.                     // (this is the structure of the corporate website and mylorch)
  77.                     $exceptionStatusCode 500;
  78.                     if (method_exists$exception'getStatusCode') ) {
  79.                         $exceptionStatusCode $exception->getStatusCode();
  80.                     }
  81.                     $requestLocale $nearestDocument->getProperty('language') ?? 'en'// Use en as fallback
  82.                     $searchErrorDocument $siteDocument->getRealFullPath() . '/' $requestLocale '/meta/' $exceptionStatusCode;
  83.                     $errorDocument Document::getByPath($searchErrorDocument);
  84.                     if (!$errorDocument) {
  85.                         // Search again without the locale whitch matches the structure of the microsites
  86.                         $searchErrorDocument $siteDocument->getRealFullPath() . '/meta/' $exceptionStatusCode;
  87.                         $errorDocument Document::getByPath($searchErrorDocument);
  88.                     }
  89.                     if ($errorDocument) {
  90.                         try {
  91.                             // Try to render the localized error document
  92.                             $response $this->actionRenderer->render($errorDocument);
  93.                         } catch (\Exception $e) {
  94.                             // we are even not able to render the error page, so we send the client a unicorn
  95.                             if ($e) {
  96.                                 $response 'Page not found (' $e->getMessage() . ') ðŸ¦„';
  97.                             } else {
  98.                                 $response 'Page not found ðŸ¦„';
  99.                             }
  100.                         }
  101.                         $headers = [];
  102.                         if ($exception instanceof HttpExceptionInterface) {
  103.                             $statusCode $exceptionStatusCode;
  104.                             $headers $exception->getHeaders();
  105.                         }
  106.                         $event->setResponse(new Response($response$statusCode$headers));
  107.                     }
  108.                 }
  109.             }
  110.         }
  111.     }
  112. }