Skip to content

Commit

Permalink
Merge pull request #550 from flightphp/overwrite-body
Browse files Browse the repository at this point in the history
added ability to overwrite the body
  • Loading branch information
n0nag0n authored Mar 2, 2024
2 parents c043655 + c1ba04d commit 735dafe
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 2 deletions.
1 change: 1 addition & 0 deletions flight/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,7 @@ public function _error(Throwable $e): void
* @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
{
Expand Down
20 changes: 18 additions & 2 deletions flight/net/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,32 @@ public function getHeaders(): array
* 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.
*
Expand All @@ -244,7 +260,7 @@ public function clear(): self
{
$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) {
Expand Down
26 changes: 26 additions & 0 deletions tests/FlightTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,30 @@ public function setRealHeader(string $header_string, bool $replace = true, int $
], 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');
}
}
17 changes: 17 additions & 0 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,21 @@ public function setRealHeader(string $header_string, bool $replace = true, int $
$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());
}
}
1 change: 1 addition & 0 deletions tests/server/LayoutMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public function before()
<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">';
Expand Down
12 changes: 12 additions & 0 deletions tests/server/OverwriteBodyMiddleware.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
5 changes: 5 additions & 0 deletions tests/server/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
//Flight::set('flight.v2.output_buffering', true);

require_once 'LayoutMiddleware.php';
require_once 'OverwriteBodyMiddleware.php';

Flight::group('', function () {

Expand Down Expand Up @@ -119,6 +120,10 @@
}
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)
Expand Down

0 comments on commit 735dafe

Please sign in to comment.