added ability to overwrite the body

pull/550/head
n0nag0n 11 months ago
parent 4f37a4854e
commit 47cd98e78e

@ -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) {

@ -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) {

@ -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 '<p>This is a route with html</p>';
})->addMiddleware($middleware);
Flight::request()->url = '/route-with-html';
Flight::start();
$this->expectOutputString('Thisisaroutewithhtml');
}
}

@ -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());
}
}

@ -77,6 +77,7 @@ class LayoutMiddleware
<li><a href="/halt">Halt</a></li>
<li><a href="/redirect">Redirect</a></li>
<li><a href="/streamResponse">Stream</a></li>
<li><a href="/overwrite">Overwrite Body</a></li>
</ul>
HTML;
echo '<div id="container">';

@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
class OverwriteBodyMiddleware
{
public function after()
{
$response = Flight::response();
$response->write(str_replace('<span style="color:red; font-weight: bold;">failed</span>', '<span style="color:green; font-weight: bold;">successfully works!</span>', $response->getBody()), true);
}
}

@ -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 '<span id="infotext">Route text:</span> This route status is that it <span style="color:red; font-weight: bold;">failed</span>';
})->addMiddleware([new OverwriteBodyMiddleware()]);
}, [ new LayoutMiddleware() ]);
// Test 9: JSON output (should not output any other html)

Loading…
Cancel
Save