From 144ff7e5a09dfcf5e9467cb782d18b736214bc85 Mon Sep 17 00:00:00 2001 From: MGatner Date: Sat, 7 Nov 2020 18:55:03 +0000 Subject: [PATCH] Standardize response type and handling --- system/CodeIgniter.php | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 070c85426fcb..302e19d2275b 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -21,7 +21,6 @@ use CodeIgniter\HTTP\RedirectResponse; use CodeIgniter\HTTP\Request; use CodeIgniter\HTTP\RequestInterface; -use CodeIgniter\HTTP\Response; use CodeIgniter\HTTP\ResponseInterface; use CodeIgniter\HTTP\URI; use CodeIgniter\Router\Exceptions\RedirectException; @@ -297,7 +296,7 @@ protected function initializeKint() * @param RouteCollectionInterface|null $routes * @param boolean $returnResponse * - * @return boolean|RequestInterface|Response|ResponseInterface|mixed + * @return boolean|RequestInterface|ResponseInterface|mixed * @throws RedirectException * @throws Exception */ @@ -378,7 +377,7 @@ public function useSafeOutput(bool $safe = true) * @param Cache $cacheConfig * @param boolean $returnResponse * - * @return RequestInterface|\CodeIgniter\HTTP\Response|ResponseInterface|mixed + * @return RequestInterface|ResponseInterface|mixed * @throws RedirectException */ protected function handleRequest(RouteCollectionInterface $routes = null, Cache $cacheConfig, bool $returnResponse = false) @@ -401,15 +400,12 @@ protected function handleRequest(RouteCollectionInterface $routes = null, Cache // Never run filters when running through Spark cli if (! defined('SPARKED')) { - $possibleRedirect = $filters->run($uri, 'before'); - if ($possibleRedirect instanceof RedirectResponse) - { - return $possibleRedirect->send(); - } - // If a Response instance is returned, the Response will be sent back to the client and script execution will stop - if ($possibleRedirect instanceof ResponseInterface) + $possibleResponse = $filters->run($uri, 'before'); + + // If a ResponseInterface instance is returned then send it back to the client and stop + if ($possibleResponse instanceof ResponseInterface) { - return $possibleRedirect->send(); + return $returnResponse ? $possibleResponse : $possibleResponse->pretend($this->useSafeOutput)->send(); } } @@ -454,7 +450,7 @@ protected function handleRequest(RouteCollectionInterface $routes = null, Cache } } - if ($response instanceof Response) + if ($response instanceof ResponseInterface) { $this->response = $response; } @@ -1038,7 +1034,7 @@ protected function gatherOutput(Cache $cacheConfig = null, $returned = null) // echoed already. // We also need to save the instance locally // so that any status code changes, etc, take place. - if ($returned instanceof Response) + if ($returned instanceof ResponseInterface) { $this->response = $returned; $returned = $returned->getBody();