From 96d6727c68df3cf9a0057b2b967605a00e7c1ce7 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 13 Aug 2025 12:03:41 -0400 Subject: [PATCH 1/2] refactor(application): Improve response handling and reset request state in `StatelessApplication`. --- src/http/StatelessApplication.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/http/StatelessApplication.php b/src/http/StatelessApplication.php index 0656e05a..f6b92981 100644 --- a/src/http/StatelessApplication.php +++ b/src/http/StatelessApplication.php @@ -275,10 +275,12 @@ public function handle(ServerRequestInterface $request): ResponseInterface $this->state = self::STATE_END; - return $this->terminate($response); + $response = $this->terminate($response); } catch (Throwable $e) { - return $this->terminate($this->handleError($e)); + $response = $this->terminate($this->handleError($e)); } + + return $response; } /** @@ -406,6 +408,7 @@ protected function reset(ServerRequestInterface $request): void $this->requestedParams = []; $this->errorHandler->setResponse($this->response); + $this->request->reset(); $this->request->setPsr7Request($request); $this->session->close(); @@ -443,8 +446,6 @@ protected function terminate(Response $response): ResponseInterface Yii::getLogger()->flush(true); - $this->request->reset(); - return $response->getPsr7Response(); } From 163075434fe7edadb576e0cef091345202c746e4 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 13 Aug 2025 12:14:07 -0400 Subject: [PATCH 2/2] Apply fixed review coderabbitai nitpick comments. --- src/http/StatelessApplication.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/http/StatelessApplication.php b/src/http/StatelessApplication.php index f6b92981..9f4ad344 100644 --- a/src/http/StatelessApplication.php +++ b/src/http/StatelessApplication.php @@ -275,12 +275,12 @@ public function handle(ServerRequestInterface $request): ResponseInterface $this->state = self::STATE_END; - $response = $this->terminate($response); + $psrResponse = $this->terminate($response); } catch (Throwable $e) { - $response = $this->terminate($this->handleError($e)); + $psrResponse = $this->terminate($this->handleError($e)); } - return $response; + return $psrResponse; } /** @@ -375,7 +375,7 @@ protected static function parseMemoryLimit(string $limit): int * Resets the StatelessApplication state and prepares the Yii2 environment for handling a PSR-7 request. * * Performs a full reinitialization of the application state, including event tracking, error handler cleanup, - * session management, and PSR-7 request injection. + * request adapter reset, session management, and PSR-7 request injection. * * This method ensures that the application is ready to process a new stateless request in worker or SAPI * environments, maintaining strict type safety and compatibility with Yii2 core components. @@ -426,7 +426,7 @@ protected function reset(ServerRequestInterface $request): void /** * Finalizes the application lifecycle and converts the Yii2 Response to a PSR-7 ResponseInterface. * - * Cleans up registered events, resets uploaded files, flushes the logger, and resets the request state. + * Cleans up registered events, resets uploaded files, flushes the logger. * * This method ensures that all application resources are released and the response is converted to a PSR-7 * ResponseInterface for interoperability with PSR-7 compatible HTTP stacks.