Skip to content

Commit

Permalink
TASK: Migrate further fusion objects to `$this->runtime->fusionGlobal…
Browse files Browse the repository at this point in the history
…s->get('request');`
  • Loading branch information
mhsdesign committed Jan 30, 2024
1 parent 94cb66c commit 428afca
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
20 changes: 10 additions & 10 deletions Neos.Fusion/Classes/Core/Runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ public function renderResponse(string $fusionPath, array $contextArray): Respons
);
}

/** unlike pushContextArray, we will only allow "legal" fusion global variables. {@see self::pushContext} */
foreach ($contextArray as $key => $_) {
if ($this->fusionGlobals->has($key)) {
throw new Exception(sprintf('Overriding Fusion global variable "%s" via @context is not allowed.', $key), 1706452063);
Expand All @@ -319,7 +320,7 @@ public function renderResponse(string $fusionPath, array $contextArray): Respons
if ($outputStringHasHttpPreamble) {
$response = Message::parseResponse($output);
if ($legacyActionResponse) {
$response = self::applyActionResponseToPsrResponse($legacyActionResponse, $response);
$response = self::applyActionResponseToHttpResponse($legacyActionResponse, $response);
$this->controllerContext = null;
}
return $response;
Expand All @@ -334,23 +335,22 @@ public function renderResponse(string $fusionPath, array $contextArray): Respons

$response = new Response(body: $stream);
if ($legacyActionResponse) {
$response = self::applyActionResponseToPsrResponse($legacyActionResponse, $response);
$response = self::applyActionResponseToHttpResponse($legacyActionResponse, $response);
$this->controllerContext = null;
}
return $response;
}

private static function applyActionResponseToPsrResponse(ActionResponse $actionResponse, ResponseInterface $response): ResponseInterface
private static function applyActionResponseToHttpResponse(ActionResponse $actionResponse, ResponseInterface $httpResponse): ResponseInterface
{
$actionResponseAsHttp = $actionResponse->buildHttpResponse();
foreach ($actionResponseAsHttp->getHeaders() as $name => $values) {
$response = $response->withAddedHeader($name, $values);
foreach ($actionResponse->buildHttpResponse()->getHeaders() as $name => $values) {
$httpResponse = $httpResponse->withAddedHeader($name, $values);
}
// preserve non 200 status codes that would otherwise be overwritten
if ($actionResponseAsHttp->getStatusCode() !== 200) {
$response = $response->withStatus($actionResponseAsHttp->getStatusCode());
// if the status code is 200 we assume it's the default and will not overrule it
if ($actionResponse->getStatusCode() !== 200) {
$httpResponse = $httpResponse->withStatus($actionResponse->getStatusCode());
}
return $response;
return $httpResponse;
}

/**
Expand Down
10 changes: 6 additions & 4 deletions Neos.Fusion/Classes/FusionObjects/ResourceUriImplementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,12 @@ public function evaluate()
} else {
$package = $this->getPackage();
if ($package === null) {
$controllerContext = $this->runtime->getControllerContext();
/** @var $actionRequest ActionRequest */
$actionRequest = $controllerContext->getRequest();
$package = $actionRequest->getControllerPackageKey();
$possibleRequest = $this->runtime->fusionGlobals->get('request');
if ($possibleRequest instanceof ActionRequest) {
$package = $possibleRequest->getControllerPackageKey();
} else {
throw new \RuntimeException('Could not infer package-key from action request. Please render Fusion with request or specify a package-key.', 1706624314);
}
}
}
$localize = $this->isLocalize();
Expand Down
5 changes: 4 additions & 1 deletion Neos.Neos/Classes/Fusion/ImageUriImplementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Neos\Neos\Fusion;

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Mvc\ActionRequest;
use Neos\Media\Domain\Model\AssetInterface;
use Neos\Media\Domain\Model\ThumbnailConfiguration;
use Neos\Media\Domain\Service\AssetService;
Expand Down Expand Up @@ -181,7 +182,9 @@ public function evaluate()
$this->getFormat()
);
}
$request = $this->getRuntime()->getControllerContext()->getRequest();

$possibleRequest = $this->runtime->fusionGlobals->get('request');
$request = $possibleRequest instanceof ActionRequest ? $possibleRequest : null;
$thumbnailData = $this->assetService->getThumbnailUriAndSizeForAsset($asset, $thumbnailConfiguration, $request);
if ($thumbnailData === null) {
return '';
Expand Down
15 changes: 14 additions & 1 deletion Neos.Neos/Classes/Fusion/NodeUriImplementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

namespace Neos\Neos\Fusion;

use GuzzleHttp\Psr7\ServerRequest;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\Flow\Mvc\ActionRequest;
use Neos\Neos\FrontendRouting\NodeAddressFactory;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Neos\FrontendRouting\NodeUriBuilder;
Expand Down Expand Up @@ -142,7 +144,18 @@ public function evaluate()
);*/

$uriBuilder = new UriBuilder();
$uriBuilder->setRequest($this->runtime->getControllerContext()->getRequest());
$possibleRequest = $this->runtime->fusionGlobals->get('request');
if ($possibleRequest instanceof ActionRequest) {
$uriBuilder->setRequest($possibleRequest);
} else {
// unfortunately, the uri-builder always needs a request at hand and cannot build uris without
// even, if the default param merging would not be required
// this will improve with a reformed uri building:
// https://github.com/neos/flow-development-collection/pull/2744
$uriBuilder->setRequest(
ActionRequest::fromHttpRequest(ServerRequest::fromGlobals())
);
}
$uriBuilder
->setAddQueryString($this->getAddQueryString())
->setArguments($this->getAdditionalParams())
Expand Down
5 changes: 4 additions & 1 deletion Neos.Neos/Classes/Fusion/PluginImplementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ public function getArgumentNamespace()
*/
protected function buildPluginRequest(): ActionRequest
{
$parentRequest = $this->runtime->getControllerContext()->getRequest();
$parentRequest = $this->runtime->fusionGlobals->get('request');
if (!$parentRequest instanceof ActionRequest) {
throw new \RuntimeException('Fusion Plugins must be rendered with an ActionRequest set as fusion-global.', 1706624581);
}
$pluginRequest = $parentRequest->createSubRequest();
$pluginRequest->setArgumentNamespace('--' . $this->getPluginNamespace());
$this->passArgumentsToPluginRequest($pluginRequest);
Expand Down

0 comments on commit 428afca

Please sign in to comment.