/
FusionExceptionView.php
241 lines (206 loc) · 8.06 KB
/
FusionExceptionView.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/*
* This file is part of the Neos.Neos package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
declare(strict_types=1);
namespace Neos\Neos\View;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepository\Core\Projection\ContentGraph\VisibilityConstraints;
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceName;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Core\Bootstrap;
use Neos\Flow\Http\RequestHandler as HttpRequestHandler;
use Neos\Flow\Mvc\ActionRequest;
use Neos\Flow\Mvc\ActionResponse;
use Neos\Flow\Mvc\Controller\Arguments;
use Neos\Flow\Mvc\Controller\ControllerContext;
use Neos\Flow\Mvc\Routing\UriBuilder;
use Neos\Flow\Mvc\View\AbstractView;
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
use Neos\Flow\Security\Context as SecurityContext;
use Neos\Fusion\Core\FusionGlobals;
use Neos\Fusion\Core\Runtime as FusionRuntime;
use Neos\Fusion\Core\RuntimeFactory;
use Neos\Fusion\Exception\RuntimeException;
use Neos\Neos\Domain\Model\RenderingMode;
use Neos\Neos\Domain\Repository\DomainRepository;
use Neos\Neos\Domain\Repository\SiteRepository;
use Neos\Neos\Domain\Service\FusionService;
use Neos\Neos\Domain\Service\SiteNodeUtility;
use Neos\Neos\FrontendRouting\SiteDetection\SiteDetectionFailedException;
use Neos\Neos\FrontendRouting\SiteDetection\SiteDetectionResult;
class FusionExceptionView extends AbstractView
{
use FusionViewI18nTrait;
/**
* This contains the supported options, their default values, descriptions and types.
* @var array<string,mixed>
*/
protected $supportedOptions = [
'enableContentCache' => ['defaultValue', true, 'boolean'],
];
/**
* @Flow\Inject
* @var Bootstrap
*/
protected $bootstrap;
/**
* @var ObjectManagerInterface
* @Flow\Inject
*/
protected $objectManager;
/**
* @var FusionService
* @Flow\Inject
*/
protected $fusionService;
/**
* @var FusionRuntime
*/
protected $fusionRuntime;
#[Flow\Inject]
protected RuntimeFactory $runtimeFactory;
#[Flow\Inject]
protected SiteRepository $siteRepository;
#[Flow\Inject]
protected SiteNodeUtility $siteNodeUtility;
#[Flow\Inject]
protected ContentRepositoryRegistry $contentRepositoryRegistry;
#[Flow\Inject]
protected DomainRepository $domainRepository;
/**
* @return mixed
* @throws \Neos\Flow\I18n\Exception\InvalidLocaleIdentifierException
* @throws \Neos\Fusion\Exception
* @throws \Neos\Neos\Domain\Exception
* @throws \Neos\Flow\Security\Exception
*/
public function render()
{
$requestHandler = $this->bootstrap->getActiveRequestHandler();
if (!$requestHandler instanceof HttpRequestHandler) {
throw new \RuntimeException('The FusionExceptionView only works in web requests.', 1695975353);
}
$httpRequest = $requestHandler->getHttpRequest();
try {
$siteDetectionResult = SiteDetectionResult::fromRequest($httpRequest);
} catch (SiteDetectionFailedException) {
return $this->renderErrorWelcomeScreen();
}
$contentRepository = $this->contentRepositoryRegistry->get($siteDetectionResult->contentRepositoryId);
$fusionExceptionViewInternals = $this->contentRepositoryRegistry->buildService(
$siteDetectionResult->contentRepositoryId,
new FusionExceptionViewInternalsFactory()
);
$dimensionSpacePoint = $fusionExceptionViewInternals->getArbitraryDimensionSpacePoint();
$liveWorkspace = $contentRepository->getWorkspaceFinder()->findOneByName(WorkspaceName::forLive());
$currentSiteNode = null;
$site = $this->siteRepository->findOneByNodeName($siteDetectionResult->siteNodeName);
if ($liveWorkspace && $site) {
$currentSiteNode = $this->siteNodeUtility->findSiteNodeBySite(
$site,
$liveWorkspace->currentContentStreamId,
$dimensionSpacePoint,
VisibilityConstraints::frontend()
);
}
if (!$currentSiteNode) {
return $this->renderErrorWelcomeScreen();
}
$request = ActionRequest::fromHttpRequest($httpRequest);
$request->setControllerPackageKey('Neos.Neos');
$request->setFormat('html');
$uriBuilder = new UriBuilder();
$uriBuilder->setRequest($request);
$controllerContext = new ControllerContext(
$request,
new ActionResponse(),
new Arguments([]),
$uriBuilder
);
/** @var SecurityContext $securityContext */
$securityContext = $this->objectManager->get(SecurityContext::class);
$securityContext->setRequest($request);
$fusionRuntime = $this->getFusionRuntime($currentSiteNode, $controllerContext);
$this->setFallbackRuleFromDimension($dimensionSpacePoint);
$fusionRuntime->pushContextArray(array_merge(
$this->variables,
[
'node' => $currentSiteNode,
'documentNode' => $currentSiteNode,
'site' => $currentSiteNode
]
));
try {
$output = $fusionRuntime->render('error');
return $this->extractBodyFromOutput($output);
} catch (RuntimeException $exception) {
throw $exception->getPrevious() ?: $exception;
} finally {
$fusionRuntime->popContext();
}
}
/**
* @param string $output
* @return string The message body without the message head
*/
protected function extractBodyFromOutput(string $output): string
{
if (substr($output, 0, 5) === 'HTTP/') {
$endOfHeader = strpos($output, "\r\n\r\n");
if ($endOfHeader !== false) {
$output = substr($output, $endOfHeader + 4);
}
}
return $output;
}
/**
* @param Node $currentSiteNode
* @param ControllerContext $controllerContext
* @return FusionRuntime
* @throws \Neos\Fusion\Exception
* @throws \Neos\Neos\Domain\Exception
*/
protected function getFusionRuntime(
Node $currentSiteNode,
ControllerContext $controllerContext
): FusionRuntime {
if ($this->fusionRuntime === null) {
$site = $this->siteRepository->findSiteBySiteNode($currentSiteNode);
$fusionConfiguration = $this->fusionService->createFusionConfigurationFromSite($site);
$fusionGlobals = FusionGlobals::fromArray([
'request' => $controllerContext->getRequest(),
'renderingMode' => RenderingMode::createFrontend()
]);
$this->fusionRuntime = $this->runtimeFactory->createFromConfiguration(
$fusionConfiguration,
$fusionGlobals
);
$this->fusionRuntime->setControllerContext($controllerContext);
if (isset($this->options['enableContentCache']) && $this->options['enableContentCache'] !== null) {
$this->fusionRuntime->setEnableContentCache($this->options['enableContentCache']);
}
}
return $this->fusionRuntime;
}
private function renderErrorWelcomeScreen(): mixed
{
// in case no neos site being there or no site node we cannot continue with the fusion exception view,
// as we wouldn't know the site and cannot get the site's root.fusion
// instead we render the welcome screen directly
$view = \Neos\Fusion\View\FusionView::createWithOptions([
'fusionPath' => 'Neos/Fusion/NotFoundExceptions',
'fusionPathPatterns' => ['resource://Neos.Neos/Private/Fusion/Error/Root.fusion'],
'enableContentCache' => false,
]);
$view->assignMultiple($this->variables);
return $view->render();
}
}