Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.4] refactor: a single point of sending the Response. #7519

Merged
merged 2 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 24 additions & 59 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ class CodeIgniter

/**
* Whether to return Response object or send response.
*
* @deprecated No longer used.
*/
protected bool $returnResponse = false;

Expand Down Expand Up @@ -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, '
Expand All @@ -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();
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions tests/system/CodeIgniterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
**********
Expand Down