Skip to content

Commit 735dafe

Browse files
authored
Merge pull request #550 from flightphp/overwrite-body
added ability to overwrite the body
2 parents c043655 + c1ba04d commit 735dafe

7 files changed

Lines changed: 80 additions & 2 deletions

File tree

flight/Engine.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ public function _error(Throwable $e): void
575575
* @param ?int $code HTTP status code
576576
*
577577
* @throws Exception
578+
* @deprecated 3.5.3 This method will be removed in v4
578579
*/
579580
public function _stop(?int $code = null): void
580581
{

flight/net/Response.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,16 +225,32 @@ public function getHeaders(): array
225225
* Writes content to the response body.
226226
*
227227
* @param string $str Response content
228+
* @param bool $overwrite Overwrite the response body
228229
*
229230
* @return $this Self reference
230231
*/
231-
public function write(string $str): self
232+
public function write(string $str, bool $overwrite = false): self
232233
{
234+
if ($overwrite === true) {
235+
$this->clearBody();
236+
}
237+
233238
$this->body .= $str;
234239

235240
return $this;
236241
}
237242

243+
/**
244+
* Clears the response body.
245+
*
246+
* @return $this Self reference
247+
*/
248+
public function clearBody(): self
249+
{
250+
$this->body = '';
251+
return $this;
252+
}
253+
238254
/**
239255
* Clears the response.
240256
*
@@ -244,7 +260,7 @@ public function clear(): self
244260
{
245261
$this->status = 200;
246262
$this->headers = [];
247-
$this->body = '';
263+
$this->clearBody();
248264

249265
// This needs to clear the output buffer if it's on
250266
if ($this->v2_output_buffering === false && ob_get_length() > 0) {

tests/FlightTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,30 @@ public function setRealHeader(string $header_string, bool $replace = true, int $
304304
], Flight::response()->getHeaders());
305305
$this->assertEquals(200, Flight::response()->status());
306306
}
307+
308+
public function testOverwriteBodyWithMiddleware()
309+
{
310+
$middleware = new class {
311+
public function after()
312+
{
313+
$response = Flight::response();
314+
$body = $response->getBody();
315+
$body = strip_tags($body);
316+
// remove spaces for fun
317+
$body = str_replace(' ', '', $body);
318+
$response->write($body, true);
319+
return $response;
320+
}
321+
};
322+
323+
Flight::route('/route-with-html', function () {
324+
echo '<p>This is a route with html</p>';
325+
})->addMiddleware($middleware);
326+
327+
Flight::request()->url = '/route-with-html';
328+
329+
Flight::start();
330+
331+
$this->expectOutputString('Thisisaroutewithhtml');
332+
}
307333
}

tests/ResponseTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,21 @@ public function setRealHeader(string $header_string, bool $replace = true, int $
238238
$response->send();
239239
$this->assertTrue($response->sent());
240240
}
241+
242+
public function testClearBody()
243+
{
244+
$response = new Response();
245+
$response->write('test');
246+
$response->clearBody();
247+
$this->assertEquals('', $response->getBody());
248+
}
249+
250+
public function testOverwriteBody()
251+
{
252+
$response = new Response();
253+
$response->write('test');
254+
$response->write('lots more test');
255+
$response->write('new', true);
256+
$this->assertEquals('new', $response->getBody());
257+
}
241258
}

tests/server/LayoutMiddleware.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public function before()
7777
<li><a href="/halt">Halt</a></li>
7878
<li><a href="/redirect">Redirect</a></li>
7979
<li><a href="/streamResponse">Stream</a></li>
80+
<li><a href="/overwrite">Overwrite Body</a></li>
8081
</ul>
8182
HTML;
8283
echo '<div id="container">';
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
class OverwriteBodyMiddleware
6+
{
7+
public function after()
8+
{
9+
$response = Flight::response();
10+
$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);
11+
}
12+
}

tests/server/index.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
//Flight::set('flight.v2.output_buffering', true);
1818

1919
require_once 'LayoutMiddleware.php';
20+
require_once 'OverwriteBodyMiddleware.php';
2021

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

@@ -119,6 +120,10 @@
119120
}
120121
echo "is successful!!";
121122
})->streamWithHeaders(['Content-Type' => 'text/html', 'status' => 200 ]);
123+
// Test 14: Overwrite the body with a middleware
124+
Flight::route('/overwrite', function () {
125+
echo '<span id="infotext">Route text:</span> This route status is that it <span style="color:red; font-weight: bold;">failed</span>';
126+
})->addMiddleware([new OverwriteBodyMiddleware()]);
122127
}, [ new LayoutMiddleware() ]);
123128

124129
// Test 9: JSON output (should not output any other html)

0 commit comments

Comments
 (0)