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