Explorar el Código

add the http ErrorHandler class

Olivier Massot hace 5 años
padre
commit
0a405223eb
Se han modificado 1 ficheros con 83 adiciones y 0 borrados
  1. 83 0
      ot_templating/Classes/Page/ErrorHandler.php

+ 83 - 0
ot_templating/Classes/Page/ErrorHandler.php

@@ -0,0 +1,83 @@
+<?php
+
+namespace Opentalent\OtTemplating\Page;
+
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
+use TYPO3\CMS\Core\Error\PageErrorHandler\PageErrorHandlerInterface;
+use TYPO3\CMS\Core\Http\HtmlResponse;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Fluid\View\TemplateView;
+use TYPO3Fluid\Fluid\View\ViewInterface;
+
+class ErrorHandler implements PageErrorHandlerInterface
+{
+    const TEMPLATES_ROOT_PATHS = 'EXT:ot_templating/Resources/Private/Templates';
+    const LAYOUTS_ROOT_PATHS = 'EXT:ot_templating/Resources/Private/Layouts';
+    const PARTIALS_ROOT_PATHS = 'EXT:ot_templating/Resources/Private/Partials';
+
+    const TEMPLATE_FILES = [
+        403 => self::TEMPLATES_ROOT_PATHS . '/Page/Error/403.html',
+        404 => self::TEMPLATES_ROOT_PATHS . '/Page/Error/404.html',
+        500 => self::TEMPLATES_ROOT_PATHS . '/Page/Error/500.html'
+    ];
+
+    /**
+     * Status code of the response
+     * @var int
+     */
+    protected $statusCode;
+
+    /**
+     * Configuration of the error handler as set in site configuration.
+     * @var array
+     */
+    protected $errorHandlerConfiguration;
+
+    /**
+     * @var ViewInterface
+     */
+    protected $view;
+
+    public function __construct(int $statusCode, array $configuration)
+    {
+        $this->statusCode = $statusCode;
+        $this->errorHandlerConfiguration = $configuration;
+
+        $this->view = GeneralUtility::makeInstance(TemplateView::class);
+        $this->view->setTemplateRootPaths([self::TEMPLATES_ROOT_PATHS]);
+        $this->view->setLayoutRootPaths([self::LAYOUTS_ROOT_PATHS]);
+        $this->view->setPartialRootPaths([self::PARTIALS_ROOT_PATHS]);
+        $this->view->setTemplatePathAndFilename(
+            GeneralUtility::getFileAbsFileName(self::TEMPLATE_FILES[$statusCode])
+        );
+    }
+
+    /**
+     * @param ServerRequestInterface $request
+     * @param string $message
+     * @param array $reasons
+     * @return ResponseInterface
+     */
+    public function handlePageError(
+        ServerRequestInterface $request,
+        string $message,
+        array $reasons = []
+    ): ResponseInterface {
+
+        $pageRepository = GeneralUtility::makeInstance(OtPageRepository::class);
+        $site = $pageRepository->getCurrentSite();
+        $baseUri = $site->getBase();
+
+        $this->view->assignMultiple([
+            'request' => $request,
+            'message' => $message,
+            'reasons' => $reasons,
+            'homeUri' => $baseUri,
+            'siteTitle' => $site->getConfiguration()['websiteTitle']
+        ]);
+
+        return new HtmlResponse($this->view->render(), $this->statusCode);
+    }
+}