added plain stream() method

pull/575/head
n0nag0n 10 months ago
parent a74ee3955c
commit deb0d68875

@ -487,13 +487,16 @@ class Engine
// If this route is to be streamed, we need to output the headers now
if ($route->is_streamed === true) {
$response->status($route->streamed_headers['status'] ?? 200);
unset($route->streamed_headers['status']);
if (count($route->streamed_headers) > 0) {
$response->status($route->streamed_headers['status'] ?? 200);
unset($route->streamed_headers['status']);
foreach ($route->streamed_headers as $header => $value) {
$response->header($header, $value);
}
}
$response->header('X-Accel-Buffering', 'no');
$response->header('Connection', 'close');
foreach ($route->streamed_headers as $header => $value) {
$response->header($header, $value);
}
// We obviously don't know the content length right now. This must be false.
$response->content_length = false;

@ -238,6 +238,17 @@ class Route
return $this;
}
/**
* If the response should be streamed
*
* @return self
*/
public function stream(): self
{
$this->is_streamed = true;
return $this;
}
/**
* This will allow the response for this route to be streamed.
*

@ -280,6 +280,30 @@ class FlightTest extends TestCase
}
public function testStreamRoute()
{
$response_mock = new class extends Response {
public function setRealHeader(string $header_string, bool $replace = true, int $response_code = 0): Response
{
return $this;
}
};
$mock_response_class_name = get_class($response_mock);
Flight::register('response', $mock_response_class_name);
Flight::route('/stream', function () {
echo 'stream';
})->stream();
Flight::request()->url = '/stream';
$this->expectOutputString('stream');
Flight::start();
$this->assertEquals('', Flight::response()->getBody());
$this->assertEquals([
'X-Accel-Buffering' => 'no',
'Connection' => 'close'
], Flight::response()->getHeaders());
$this->assertEquals(200, Flight::response()->status());
}
public function testStreamRouteWithHeaders()
{
$response_mock = new class extends Response {
public function setRealHeader(string $header_string, bool $replace = true, int $response_code = 0): Response

@ -76,7 +76,8 @@ class LayoutMiddleware
<li><a href="/jsonp?jsonp=myjson">JSONP</a></li>
<li><a href="/halt">Halt</a></li>
<li><a href="/redirect">Redirect</a></li>
<li><a href="/streamResponse">Stream</a></li>
<li><a href="/streamResponse">Stream Plain</a></li>
<li><a href="/streamWithHeaders">Stream Headers</a></li>
<li><a href="/overwrite">Overwrite Body</a></li>
<li><a href="/redirect/before%2Fafter">Slash in Param</a></li>
<li><a href="/わたしはひとです">UTF8 URL</a></li>

@ -122,7 +122,19 @@ Flight::group('', function () {
ob_flush();
}
echo "is successful!!";
})->stream();
// Test 12: Redirect with status code
Flight::route('/streamWithHeaders', function () {
echo "Streaming a response";
for ($i = 1; $i <= 50; $i++) {
echo ".";
usleep(50000);
ob_flush();
}
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>';

Loading…
Cancel
Save