074af5a8f5d905b155a061a9f7078c5edebd991e
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / PageErrorHandler / FluidPageErrorHandler.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Frontend\PageErrorHandler;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use Psr\Http\Message\ResponseInterface;
20 use Psr\Http\Message\ServerRequestInterface;
21 use TYPO3\CMS\Core\Http\HtmlResponse;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Fluid\View\TemplateView;
24 use TYPO3Fluid\Fluid\View\ViewInterface;
25
26 /**
27 * An error handler that renders a fluid template.
28 * This is typically configured via the "Sites configuration" module in the backend.
29 */
30 class FluidPageErrorHandler implements PageErrorHandlerInterface
31 {
32 /**
33 * @var ViewInterface
34 */
35 protected $view;
36
37 /**
38 * @var int
39 */
40 protected $statusCode;
41
42 /**
43 * FluidPageErrorHandler constructor.
44 * @param int $statusCode
45 * @param array $configuration
46 */
47 public function __construct(int $statusCode, array $configuration)
48 {
49 $this->statusCode = $statusCode;
50 $this->view = GeneralUtility::makeInstance(TemplateView::class);
51 if (!empty($configuration['errorFluidTemplatesRootPath'])) {
52 $this->view->setTemplateRootPaths([$configuration['errorFluidTemplatesRootPath']]);
53 }
54 if (!empty($configuration['errorFluidLayoutsRootPath'])) {
55 $this->view->setLayoutRootPaths([$configuration['errorFluidLayoutsRootPath']]);
56 }
57 if (!empty($configuration['errorFluidPartialsRootPath'])) {
58 $this->view->setPartialRootPaths([$configuration['errorFluidPartialsRootPath']]);
59 }
60 $this->view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($configuration['errorFluidTemplate']));
61 }
62
63 /**
64 * @param ServerRequestInterface $request
65 * @param string $message
66 * @param array $reasons
67 * @return ResponseInterface
68 */
69 public function handlePageError(ServerRequestInterface $request, string $message, array $reasons = []): ResponseInterface
70 {
71 $this->view->assignMultiple([
72 'request' => $request,
73 'message' => $message,
74 'reasons' => $reasons
75 ]);
76 return new HtmlResponse($this->view->render());
77 }
78 }