From c96fd84431318e55297045beac984c73a01d94a1 Mon Sep 17 00:00:00 2001 From: mhsdesign <85400359+mhsdesign@users.noreply.github.com> Date: Sat, 3 Feb 2024 13:53:37 +0100 Subject: [PATCH] TASK: Revert introduction of `SpecialResponsesSupport` Will be split up into an own pr https://github.com/neos/flow-development-collection/pull/3298 --- .../Mvc/Controller/AbstractController.php | 41 ++++++--- .../Classes/Mvc/Controller/RestController.php | 19 ++-- .../Controller/SpecialResponsesSupport.php | 88 ------------------- .../Mvc/Exception/ForwardException.php | 3 +- .../Mvc/Exception/StopActionException.php | 4 +- 5 files changed, 43 insertions(+), 112 deletions(-) delete mode 100644 Neos.Flow/Classes/Mvc/Controller/SpecialResponsesSupport.php diff --git a/Neos.Flow/Classes/Mvc/Controller/AbstractController.php b/Neos.Flow/Classes/Mvc/Controller/AbstractController.php index e4452524ac..69268ff928 100644 --- a/Neos.Flow/Classes/Mvc/Controller/AbstractController.php +++ b/Neos.Flow/Classes/Mvc/Controller/AbstractController.php @@ -42,8 +42,6 @@ */ abstract class AbstractController implements ControllerInterface { - use SpecialResponsesSupport; - /** * @var UriBuilder */ @@ -232,6 +230,23 @@ protected function forward(string $actionName, string $controllerName = null, st $this->forwardToRequest($nextRequest); } + /** + * Forwards the request to another action and / or controller. + * + * Request is directly transfered to the other action / controller + * + * @param ActionRequest $request The request to redirect to + * @return void + * @throws ForwardException + * @see redirectToRequest() + * @api + */ + protected function forwardToRequest(ActionRequest $request) + { + $nextRequest = clone $request; + throw ForwardException::createForNextRequest($nextRequest, ''); + } + /** * Redirects the request to another action and / or controller. * @@ -308,12 +323,16 @@ protected function redirectToRequest(ActionRequest $request, int $delay = 0, int */ protected function redirectToUri(string|UriInterface $uri, int $delay = 0, int $statusCode = 303): never { - if (!$uri instanceof UriInterface) { - $uri = new Uri($uri); + if ($delay === 0) { + if (!$uri instanceof UriInterface) { + $uri = new Uri($uri); + } + $this->response->setRedirectUri($uri, $statusCode); + } else { + $this->response->setStatusCode($statusCode); + $this->response->setContent('
'); } - - $response = $this->responseRedirectsToUri($uri, $delay, $statusCode, $this->response); - $this->throwStopActionWithResponse($response, ''); + throw StopActionException::createForResponse($this->response, ''); } /** @@ -325,11 +344,11 @@ protected function redirectToUri(string|UriInterface $uri, int $delay = 0, int $ * @param string $statusMessage A custom HTTP status message * @param string $content Body content which further explains the status * @throws StopActionException - * @deprecated Use SpecialResponsesSupport::responseThrowsStatus - * @see SpecialResponsesSupport::responseThrowsStatus + * @api */ protected function throwStatus(int $statusCode, $statusMessage = null, $content = null): never { + $this->response->setStatusCode($statusCode); if ($content === null) { $content = sprintf( '%s %s', @@ -337,8 +356,8 @@ protected function throwStatus(int $statusCode, $statusMessage = null, $content $statusMessage ?? ResponseInformationHelper::getStatusMessageByCode($statusCode) ); } - - $this->responseThrowsStatus($statusCode, $content, $this->response); + $this->response->setContent($content); + throw StopActionException::createForResponse($this->response, $content); } /** diff --git a/Neos.Flow/Classes/Mvc/Controller/RestController.php b/Neos.Flow/Classes/Mvc/Controller/RestController.php index a6eb0c1a42..79ec113edf 100644 --- a/Neos.Flow/Classes/Mvc/Controller/RestController.php +++ b/Neos.Flow/Classes/Mvc/Controller/RestController.php @@ -128,16 +128,15 @@ protected function initializeUpdateAction() */ protected function redirectToUri(string|UriInterface $uri, int $delay = 0, int $statusCode = 303): never { - if (!$uri instanceof UriInterface) { - $uri = new Uri($uri); - } - - $response = $this->responseRedirectsToUri($uri, $delay, $statusCode, $this->response); - if ($this->request->getFormat() === 'json') { - // send empty body on redirects for JSON requests - $response->setContent(''); + // the parent method throws the exception, but we need to act afterwards + // thus the code in catch - it's the expected state + try { + parent::redirectToUri($uri, $delay, $statusCode); + } catch (StopActionException $exception) { + if ($this->request->getFormat() === 'json') { + $exception->response->setContent(''); + } + throw $exception; } - - $this->throwStopActionWithResponse($response, ''); } } diff --git a/Neos.Flow/Classes/Mvc/Controller/SpecialResponsesSupport.php b/Neos.Flow/Classes/Mvc/Controller/SpecialResponsesSupport.php deleted file mode 100644 index 733500c78c..0000000000 --- a/Neos.Flow/Classes/Mvc/Controller/SpecialResponsesSupport.php +++ /dev/null @@ -1,88 +0,0 @@ -setStatusCode($statusCode); - if ($content !== '') { - $response->setContent($content); - } - - $this->throwStopActionWithResponse($response, $content); - } - - /** - * Redirects to another URI - * - * @param UriInterface $uri Either a string representation of a URI or a UriInterface object - * @param integer $delay (optional) The delay in seconds. Default is no delay. - * @param integer $statusCode (optional) The HTTP status code for the redirect. Default is "303 See Other" - * @param ActionResponse|null $response response that will have status, and location or body overwritten. - * @return ActionResponse - * @throws StopActionException - */ - final protected function responseRedirectsToUri(UriInterface $uri, int $delay = 0, int $statusCode = 303, ?ActionResponse $response = null): ActionResponse - { - $nextResponse = $response !== null ? clone $response : new ActionResponse(); - - if ($delay < 1) { - $nextResponse->setRedirectUri($uri, $statusCode); - $this->throwStopActionWithResponse($nextResponse, ''); - } - - $nextResponse->setStatusCode($statusCode); - $content = sprintf('