From 47cd98e78ec498f8e6fc6ada835f9229f980dd75 Mon Sep 17 00:00:00 2001 From: n0nag0n Date: Sat, 24 Feb 2024 08:38:25 -0700 Subject: [PATCH 1/2] added ability to overwrite the body --- flight/Engine.php | 8 ++++---- flight/net/Response.php | 20 ++++++++++++++++-- tests/FlightTest.php | 26 ++++++++++++++++++++++++ tests/ResponseTest.php | 17 ++++++++++++++++ tests/server/LayoutMiddleware.php | 1 + tests/server/OverwriteBodyMiddleware.php | 12 +++++++++++ tests/server/index.php | 5 +++++ 7 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 tests/server/OverwriteBodyMiddleware.php diff --git a/flight/Engine.php b/flight/Engine.php index fb4d9e8..aaaf778 100644 --- a/flight/Engine.php +++ b/flight/Engine.php @@ -401,8 +401,8 @@ class Engine continue; } - $use_v3_output_buffering = - $this->response()->v2_output_buffering === false && + $use_v3_output_buffering = + $this->response()->v2_output_buffering === false && $route->is_streamed === false; if ($use_v3_output_buffering === true) { @@ -493,8 +493,8 @@ class Engine } } - $use_v3_output_buffering = - $this->response()->v2_output_buffering === false && + $use_v3_output_buffering = + $this->response()->v2_output_buffering === false && $route->is_streamed === false; if ($use_v3_output_buffering === true) { diff --git a/flight/net/Response.php b/flight/net/Response.php index cfc8ffd..e1abaac 100644 --- a/flight/net/Response.php +++ b/flight/net/Response.php @@ -225,16 +225,32 @@ class Response * Writes content to the response body. * * @param string $str Response content + * @param bool $overwrite Overwrite the response body * * @return $this Self reference */ - public function write(string $str): self + public function write(string $str, bool $overwrite = false): self { + if ($overwrite === true) { + $this->clearBody(); + } + $this->body .= $str; return $this; } + /** + * Clears the response body. + * + * @return $this Self reference + */ + public function clearBody(): self + { + $this->body = ''; + return $this; + } + /** * Clears the response. * @@ -244,7 +260,7 @@ class Response { $this->status = 200; $this->headers = []; - $this->body = ''; + $this->clearBody(); // This needs to clear the output buffer if it's on if ($this->v2_output_buffering === false && ob_get_length() > 0) { diff --git a/tests/FlightTest.php b/tests/FlightTest.php index a6ffa16..042b6bb 100644 --- a/tests/FlightTest.php +++ b/tests/FlightTest.php @@ -304,4 +304,30 @@ class FlightTest extends TestCase ], Flight::response()->getHeaders()); $this->assertEquals(200, Flight::response()->status()); } + + public function testOverwriteBodyWithMiddleware() + { + $middleware = new class { + public function after() + { + $response = Flight::response(); + $body = $response->getBody(); + $body = strip_tags($body); + // remove spaces for fun + $body = str_replace(' ', '', $body); + $response->write($body, true); + return $response; + } + }; + + Flight::route('/route-with-html', function () { + echo '

This is a route with html

'; + })->addMiddleware($middleware); + + Flight::request()->url = '/route-with-html'; + + Flight::start(); + + $this->expectOutputString('Thisisaroutewithhtml'); + } } diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php index d7155fc..42701d4 100644 --- a/tests/ResponseTest.php +++ b/tests/ResponseTest.php @@ -238,4 +238,21 @@ class ResponseTest extends TestCase $response->send(); $this->assertTrue($response->sent()); } + + public function testClearBody() + { + $response = new Response(); + $response->write('test'); + $response->clearBody(); + $this->assertEquals('', $response->getBody()); + } + + public function testOverwriteBody() + { + $response = new Response(); + $response->write('test'); + $response->write('lots more test'); + $response->write('new', true); + $this->assertEquals('new', $response->getBody()); + } } diff --git a/tests/server/LayoutMiddleware.php b/tests/server/LayoutMiddleware.php index d5dfcde..24538f9 100644 --- a/tests/server/LayoutMiddleware.php +++ b/tests/server/LayoutMiddleware.php @@ -77,6 +77,7 @@ class LayoutMiddleware
  • Halt
  • Redirect
  • Stream
  • +
  • Overwrite Body
  • HTML; echo '
    '; diff --git a/tests/server/OverwriteBodyMiddleware.php b/tests/server/OverwriteBodyMiddleware.php new file mode 100644 index 0000000..79e1194 --- /dev/null +++ b/tests/server/OverwriteBodyMiddleware.php @@ -0,0 +1,12 @@ +write(str_replace('failed', 'successfully works!', $response->getBody()), true); + } +} diff --git a/tests/server/index.php b/tests/server/index.php index e3ea703..040cbfc 100644 --- a/tests/server/index.php +++ b/tests/server/index.php @@ -17,6 +17,7 @@ Flight::set('flight.views.extension', '.phtml'); //Flight::set('flight.v2.output_buffering', true); require_once 'LayoutMiddleware.php'; +require_once 'OverwriteBodyMiddleware.php'; Flight::group('', function () { @@ -119,6 +120,10 @@ Flight::group('', function () { } echo "is successful!!"; })->streamWithHeaders(['Content-Type' => 'text/html', 'status' => 200 ]); + // Test 14: Overwrite the body with a middleware + Flight::route('/overwrite', function () { + echo 'Route text: This route status is that it failed'; + })->addMiddleware([new OverwriteBodyMiddleware()]); }, [ new LayoutMiddleware() ]); // Test 9: JSON output (should not output any other html) From c1ba04d96e40781698dcf7ddf060a9e869d37f36 Mon Sep 17 00:00:00 2001 From: n0nag0n Date: Sun, 25 Feb 2024 14:54:08 -0700 Subject: [PATCH 2/2] added deprecated tag to stop. Halt is better --- flight/Engine.php | 1 + 1 file changed, 1 insertion(+) diff --git a/flight/Engine.php b/flight/Engine.php index aaaf778..b0ae09d 100644 --- a/flight/Engine.php +++ b/flight/Engine.php @@ -575,6 +575,7 @@ class Engine * @param ?int $code HTTP status code * * @throws Exception + * @deprecated 3.5.3 This method will be removed in v4 */ public function _stop(?int $code = null): void {