Skip to content

Commit

Permalink
FEATURE: Allow *Action to return psr response
Browse files Browse the repository at this point in the history
  • Loading branch information
mhsdesign committed Jan 29, 2024
1 parent bd4c70e commit e50a1d9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
14 changes: 9 additions & 5 deletions Neos.Flow/Classes/Mvc/ActionResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ public function getContentType(): string
}

/**
* Use this if you want build your own HTTP Response inside your action
* Unsafe. Please avoid the use of this escape hatch as the behaviour is partly unspecified
* https://github.com/neos/flow-development-collection/issues/2492
*
* @param ResponseInterface $response
*/
public function replaceHttpResponse(ResponseInterface $response): void
Expand Down Expand Up @@ -275,10 +277,12 @@ public function mergeIntoParentResponse(ActionResponse $actionResponse): ActionR
}

/**
* Note this is a special use case method that will apply the internal properties (Content-Type, StatusCode, Location, Set-Cookie and Content)
* to the given PSR-7 Response and return a modified response. This is used to merge the ActionResponse properties into a possible HttpResponse
* created in a View (see ActionController::renderView()) because those would be overwritten otherwise. Note that any component parameters will
* still run through the component chain and will not be propagated here.
* Note this is a special use case method that will apply the internal properties
* (Content-Type, StatusCode, Location, Set-Cookie and Content)
* to a new or replaced PSR-7 Response and return it.
*
* Possibly unsafe when used in combination with {@see self::replaceHttpResponse()}
* https://github.com/neos/flow-development-collection/issues/2492
*
* @return ResponseInterface
* @internal
Expand Down
10 changes: 7 additions & 3 deletions Neos.Flow/Classes/Mvc/Controller/ActionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* source code.
*/

use GuzzleHttp\Psr7\Utils;
use Neos\Error\Messages\Result;
use Neos\Flow\Annotations as Flow;
use Neos\Error\Messages as Error;
Expand Down Expand Up @@ -558,13 +559,16 @@ protected function callActionMethod(ActionRequest $request, ActionResponse $resp
}
}

if ($actionResult instanceof ResponseInterface) {
return $actionResult;
}

if ($actionResult === null && $this->view instanceof ViewInterface) {
return $this->renderView($this->response);
} else {
$this->response->setContent($actionResult);
}

return $this->response->buildHttpResponse();
$httpResponse = $this->response->buildHttpResponse();
return $httpResponse->withBody(Utils::streamFor($actionResult));
}

/**
Expand Down

0 comments on commit e50a1d9

Please sign in to comment.