From 9f67962c05df5683d6b8a31bdac8cbbb6409ee81 Mon Sep 17 00:00:00 2001 From: Andrey Pyzhikov <5071@mail.ru> Date: Wed, 24 May 2023 18:00:04 +0800 Subject: [PATCH 1/2] feature: a single point of sending the Response. --- system/CodeIgniter.php | 83 ++++++--------------- tests/system/CodeIgniterTest.php | 7 +- user_guide_src/source/changelogs/v4.4.0.rst | 1 + 3 files changed, 28 insertions(+), 63 deletions(-) diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 68d70d2bc3e2..54e2f70f8872 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -162,6 +162,8 @@ class CodeIgniter /** * Whether to return Response object or send response. + * + * @deprecated No longer used. */ protected bool $returnResponse = false; @@ -321,8 +323,6 @@ private function configureKint(): void */ public function run(?RouteCollectionInterface $routes = null, bool $returnResponse = false) { - $this->returnResponse = $returnResponse; - if ($this->context === null) { throw new LogicException( 'Context must be set before run() is called. If you are upgrading from 4.1.x, ' @@ -341,37 +341,8 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon $this->spoofRequestMethod(); - if ($this->request instanceof IncomingRequest && strtolower($this->request->getMethod()) === 'cli') { - $this->response->setStatusCode(405)->setBody('Method Not Allowed'); - - if ($this->returnResponse) { - return $this->response; - } - - $this->sendResponse(); - - return; - } - - Events::trigger('pre_system'); - - // Check for a cached page. Execution will stop - // if the page has been cached. - $cacheConfig = config(Cache::class); - $response = $this->displayCache($cacheConfig); - if ($response instanceof ResponseInterface) { - if ($returnResponse) { - return $response; - } - - $this->response->send(); - $this->callExit(EXIT_SUCCESS); - - return; - } - try { - return $this->handleRequest($routes, $cacheConfig, $returnResponse); + $this->response = $this->handleRequest($routes, config(Cache::class), $returnResponse); } catch (RedirectException $e) { $this->outputBufferingEnd(); $logger = Services::logger(); @@ -380,27 +351,20 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon // If the route is a 'redirect' route, it throws // the exception with the $to as the message $this->response->redirect(base_url($e->getMessage()), 'auto', $e->getCode()); - - if ($this->returnResponse) { - return $this->response; - } - - $this->sendResponse(); - - $this->callExit(EXIT_SUCCESS); - - return; } catch (PageNotFoundException $e) { - $return = $this->display404errors($e); - - if ($return instanceof ResponseInterface) { - return $return; - } + $this->response = $this->display404errors($e); } catch (Throwable $e) { $this->outputBufferingEnd(); throw $e; } + + if ($returnResponse) { + return $this->response; + } + + $this->sendResponse(); + $this->callExit(EXIT_SUCCESS); } /** @@ -455,7 +419,17 @@ public function disableFilters(): void */ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cacheConfig, bool $returnResponse = false) { - $this->returnResponse = $returnResponse; + if ($this->request instanceof IncomingRequest && strtolower($this->request->getMethod()) === 'cli') { + return $this->response->setStatusCode(405)->setBody('Method Not Allowed'); + } + + Events::trigger('pre_system'); + + // Check for a cached page. Execution will stop + // if the page has been cached. + if (($response = $this->displayCache($cacheConfig)) instanceof ResponseInterface) { + return $response; + } $routeFilter = $this->tryToRouteIt($routes); @@ -486,7 +460,7 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache // If a ResponseInterface instance is returned then send it back to the client and stop if ($possibleResponse instanceof ResponseInterface) { - return $this->returnResponse ? $possibleResponse : $possibleResponse->send(); + return $possibleResponse; } if ($possibleResponse instanceof Request) { @@ -561,10 +535,6 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache unset($uri); - if (! $this->returnResponse) { - $this->sendResponse(); - } - // Is there a post-system event? Events::trigger('post_system'); @@ -978,13 +948,8 @@ protected function display404errors(PageNotFoundException $e) $cacheConfig = config(Cache::class); $this->gatherOutput($cacheConfig, $returned); - if ($this->returnResponse) { - return $this->response; - } - - $this->sendResponse(); - return; + return $this->response; } // Display 404 Errors diff --git a/tests/system/CodeIgniterTest.php b/tests/system/CodeIgniterTest.php index cef482bb4e34..ca084c0ed274 100644 --- a/tests/system/CodeIgniterTest.php +++ b/tests/system/CodeIgniterTest.php @@ -142,11 +142,10 @@ public function testRun404OverrideControllerReturnsResponse() $router = Services::router($routes, Services::incomingrequest()); Services::injectMock('router', $router); - ob_start(); - $this->codeigniter->run($routes); - $output = ob_get_clean(); + $response = $this->codeigniter->run($routes, true); - $this->assertStringContainsString('Oops', $output); + $this->assertStringContainsString('Oops', $response->getBody()); + $this->assertSame(567, $response->getStatusCode()); } public function testRun404OverrideReturnResponse() diff --git a/user_guide_src/source/changelogs/v4.4.0.rst b/user_guide_src/source/changelogs/v4.4.0.rst index ebfa65ede545..eb98dcfe5e28 100644 --- a/user_guide_src/source/changelogs/v4.4.0.rst +++ b/user_guide_src/source/changelogs/v4.4.0.rst @@ -138,6 +138,7 @@ Deprecations are deprecated. Because these methods have been moved to ``BaseExceptionHandler`` or ``ExceptionHandler``. - **Autoloader:** ``Autoloader::sanitizeFilename()`` is deprecated. +- **CodeIgniter:** ``CodeIgniter::$returnResponse`` property is deprecated. no longer used. Bugs Fixed ********** From d14ee1c6fc7546e0e3d5ec8a72d2faba2a4f04d3 Mon Sep 17 00:00:00 2001 From: Andrey Pyzhikov <5071@mail.ru> Date: Sun, 4 Jun 2023 11:11:26 +0800 Subject: [PATCH 2/2] Update user_guide_src/source/changelogs/v4.4.0.rst Co-authored-by: kenjis --- user_guide_src/source/changelogs/v4.4.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/changelogs/v4.4.0.rst b/user_guide_src/source/changelogs/v4.4.0.rst index eb98dcfe5e28..f65565a7792f 100644 --- a/user_guide_src/source/changelogs/v4.4.0.rst +++ b/user_guide_src/source/changelogs/v4.4.0.rst @@ -138,7 +138,7 @@ Deprecations are deprecated. Because these methods have been moved to ``BaseExceptionHandler`` or ``ExceptionHandler``. - **Autoloader:** ``Autoloader::sanitizeFilename()`` is deprecated. -- **CodeIgniter:** ``CodeIgniter::$returnResponse`` property is deprecated. no longer used. +- **CodeIgniter:** ``CodeIgniter::$returnResponse`` property is deprecated. No longer used. Bugs Fixed **********