remove v2_output_buffering

v4
fadrian06 16 hours ago
parent 50075ab83d
commit 3fe2ca1d06

@ -170,7 +170,6 @@ class Engine
$this->set('flight.views.path', './views'); $this->set('flight.views.path', './views');
$this->set('flight.views.extension', '.php'); $this->set('flight.views.extension', '.php');
$this->set('flight.content_length', true); $this->set('flight.content_length', true);
$this->set('flight.v2.output_buffering', false);
// Startup configuration // Startup configuration
$this->before('start', function (): void { $this->before('start', function (): void {
@ -185,11 +184,6 @@ class Engine
// Set Content-Length // Set Content-Length
$this->response()->content_length = $this->get('flight.content_length'); $this->response()->content_length = $this->get('flight.content_length');
// This is to maintain legacy handling of output buffering
// which causes a lot of problems. This will be removed
// in v4
$this->response()->v2_output_buffering = $this->get('flight.v2.output_buffering');
}); });
$this->initialized = true; $this->initialized = true;
@ -431,9 +425,7 @@ class Engine
} }
// This is the way that v3 handles output buffering (which captures output correctly) // This is the way that v3 handles output buffering (which captures output correctly)
$useV3OutputBuffering = $useV3OutputBuffering = !$route->is_streamed;
$this->response()->v2_output_buffering === false &&
$route->is_streamed === false;
if ($useV3OutputBuffering === true) { if ($useV3OutputBuffering === true) {
ob_start(); ob_start();
@ -505,17 +497,6 @@ class Engine
$response = $this->response(); $response = $this->response();
$router = $this->router(); $router = $this->router();
if ($response->v2_output_buffering === true) {
// Flush any existing output
if (ob_get_length() > 0) {
$response->write(ob_get_clean()); // @codeCoverageIgnore
}
// Enable output buffering
// This is closed in the Engine->_stop() method
ob_start();
}
// Route the request // Route the request
$failedMiddlewareCheck = false; $failedMiddlewareCheck = false;
while ($route = $router->route($request)) { while ($route = $router->route($request)) {
@ -565,9 +546,7 @@ class Engine
$this->triggerEvent('flight.middleware.before', $route); $this->triggerEvent('flight.middleware.before', $route);
} }
$useV3OutputBuffering = $useV3OutputBuffering = !$route->is_streamed;
$this->response()->v2_output_buffering === false &&
$route->is_streamed === false;
if ($useV3OutputBuffering === true) { if ($useV3OutputBuffering === true) {
ob_start(); ob_start();
@ -675,10 +654,6 @@ class Engine
$response->status($code); $response->status($code);
} }
if ($response->v2_output_buffering === true && ob_get_length() > 0) {
$response->write(ob_get_clean());
}
$response->send(); $response->send();
} }
} }
@ -903,9 +878,6 @@ class Engine
->status($code) ->status($code)
->header('Content-Type', 'application/json') ->header('Content-Type', 'application/json')
->write($json); ->write($json);
if ($this->response()->v2_output_buffering === true) {
$this->response()->send();
}
} }
/** /**
@ -928,10 +900,8 @@ class Engine
): void { ): void {
$this->json($data, $code, $encode, $charset, $option); $this->json($data, $code, $encode, $charset, $option);
$jsonBody = $this->response()->getBody(); $jsonBody = $this->response()->getBody();
if ($this->response()->v2_output_buffering === false) {
$this->response()->clearBody(); $this->response()->clearBody();
$this->response()->send(); $this->response()->send();
}
$this->halt($code, $jsonBody, empty(getenv('PHPUNIT_TEST'))); $this->halt($code, $jsonBody, empty(getenv('PHPUNIT_TEST')));
} }
@ -962,9 +932,6 @@ class Engine
->status($code) ->status($code)
->header('Content-Type', 'application/javascript; charset=' . $charset) ->header('Content-Type', 'application/javascript; charset=' . $charset)
->write($callback . '(' . $json . ');'); ->write($callback . '(' . $json . ');');
if ($this->response()->v2_output_buffering === true) {
$this->response()->send();
}
} }
/** /**

@ -22,15 +22,6 @@ class Response
*/ */
public bool $content_length = true; public bool $content_length = true;
/**
* This is to maintain legacy handling of output buffering
* which causes a lot of problems. This will be removed
* in v4
*
* @var boolean
*/
public bool $v2_output_buffering = false;
/** /**
* HTTP status codes * HTTP status codes
* *
@ -271,7 +262,7 @@ class Response
$this->clearBody(); $this->clearBody();
// This needs to clear the output buffer if it's on // This needs to clear the output buffer if it's on
if ($this->v2_output_buffering === false && ob_get_length() > 0) { if (ob_get_length() > 0) {
ob_clean(); ob_clean();
} }
@ -421,18 +412,8 @@ class Response
*/ */
public function send(): void public function send(): void
{ {
// legacy way of handling this
if ($this->v2_output_buffering === true) {
if (ob_get_length() > 0) {
ob_end_clean(); // @codeCoverageIgnore
}
}
$start = microtime(true); $start = microtime(true);
// Only for the v3 output buffering.
if ($this->v2_output_buffering === false) {
$this->processResponseCallbacks(); $this->processResponseCallbacks();
}
if ($this->headersSent() === false) { if ($this->headersSent() === false) {
$this->sendHeaders(); $this->sendHeaders();

@ -45,25 +45,6 @@ class DocExamplesTest extends TestCase
$this->assertEquals('[]', Flight::response()->getBody()); $this->assertEquals('[]', Flight::response()->getBody());
} }
public function testMapNotFoundMethodV2OutputBuffering(): void
{
Flight::map('notFound', function () {
Flight::json([], 404);
});
Flight::request()->url = '/not-found';
Flight::route('/', function () {
echo 'hello world!';
});
Flight::set('flight.v2.output_buffering', true);
Flight::start();
ob_get_clean();
$this->assertEquals(404, Flight::response()->status());
$this->assertEquals('[]', Flight::response()->getBody());
}
public function testMapErrorMethod(): void public function testMapErrorMethod(): void
{ {
Flight::map('error', function (Throwable $error) { Flight::map('error', function (Throwable $error) {

@ -49,25 +49,6 @@ class EngineTest extends TestCase
$this->assertTrue($engine->response()->content_length); $this->assertTrue($engine->response()->content_length);
} }
public function testInitBeforeStartV2OutputBuffering(): void
{
$engine = new class extends Engine {
public function getInitializedVar(): bool
{
return $this->initialized;
}
};
$engine->set('flight.v2.output_buffering', true);
$this->assertTrue($engine->getInitializedVar());
$engine->start();
// This is a necessary evil because of how the v2 output buffer works.
ob_end_clean();
$this->assertFalse($engine->router()->caseSensitive);
$this->assertTrue($engine->response()->content_length);
}
public function testHandleErrorNoErrorNumber(): void public function testHandleErrorNoErrorNumber(): void
{ {
$engine = new Engine(); $engine = new Engine();
@ -322,34 +303,6 @@ class EngineTest extends TestCase
$this->assertEquals(500, $engine->response()->status()); $this->assertEquals(500, $engine->response()->status());
} }
public function testStopWithCodeV2OutputBuffering(): void
{
$engine = new class extends Engine {
public function getLoader()
{
return $this->loader;
}
};
// doing this so we can overwrite some parts of the response
$engine->getLoader()->register('response', function () {
return new class extends Response {
public function setRealHeader(string $header_string, bool $replace = true, int $response_code = 0): self
{
return $this;
}
};
});
$engine->set('flight.v2.output_buffering', true);
$engine->route('/testRoute', function () use ($engine) {
echo 'I am a teapot';
$engine->stop(500);
});
$engine->request()->url = '/testRoute';
$engine->start();
$this->expectOutputString('I am a teapot');
$this->assertEquals(500, $engine->response()->status());
}
public function testPostRoute(): void public function testPostRoute(): void
{ {
$engine = new Engine(); $engine = new Engine();
@ -519,16 +472,6 @@ class EngineTest extends TestCase
$engine->json(['key1' => 'value1', 'key2' => 'value2', 'utf8_emoji' => "\xB1\x31"]); $engine->json(['key1' => 'value1', 'key2' => 'value2', 'utf8_emoji' => "\xB1\x31"]);
} }
public function testJsonV2OutputBuffering(): void
{
$engine = new Engine();
$engine->response()->v2_output_buffering = true;
$engine->json(['key1' => 'value1', 'key2' => 'value2']);
$this->expectOutputString('{"key1":"value1","key2":"value2"}');
$this->assertEquals('application/json', $engine->response()->headers()['Content-Type']);
$this->assertEquals(200, $engine->response()->status());
}
public function testJsonHalt(): void public function testJsonHalt(): void
{ {
$engine = new Engine(); $engine = new Engine();
@ -549,17 +492,6 @@ class EngineTest extends TestCase
$this->assertEquals('whatever({"key1":"value1","key2":"value2"});', $engine->response()->getBody()); $this->assertEquals('whatever({"key1":"value1","key2":"value2"});', $engine->response()->getBody());
} }
public function testJsonPV2OutputBuffering(): void
{
$engine = new Engine();
$engine->response()->v2_output_buffering = true;
$engine->request()->query['jsonp'] = 'whatever';
$engine->jsonp(['key1' => 'value1', 'key2' => 'value2']);
$this->expectOutputString('whatever({"key1":"value1","key2":"value2"});');
$this->assertEquals('application/javascript; charset=utf-8', $engine->response()->headers()['Content-Type']);
$this->assertEquals(200, $engine->response()->status());
}
public function testJsonpBadParam(): void public function testJsonpBadParam(): void
{ {
$engine = new Engine(); $engine = new Engine();
@ -569,16 +501,6 @@ class EngineTest extends TestCase
$this->assertEquals(200, $engine->response()->status()); $this->assertEquals(200, $engine->response()->status());
} }
public function testJsonpBadParamV2OutputBuffering(): void
{
$engine = new Engine();
$engine->response()->v2_output_buffering = true;
$engine->jsonp(['key1' => 'value1', 'key2' => 'value2']);
$this->expectOutputString('({"key1":"value1","key2":"value2"});');
$this->assertEquals('application/javascript; charset=utf-8', $engine->response()->headers()['Content-Type']);
$this->assertEquals(200, $engine->response()->status());
}
public function testEtagSimple(): void public function testEtagSimple(): void
{ {
$engine = new Engine(); $engine = new Engine();

@ -275,25 +275,6 @@ class FlightTest extends TestCase
$this->assertEquals('test', Flight::response()->getBody()); $this->assertEquals('test', Flight::response()->getBody());
} }
public function testHookOutputBufferingV2OutputBuffering(): void
{
Flight::route('/test', function () {
echo 'test';
});
Flight::before('start', function ($output) {
echo 'hooked before start';
});
Flight::set('flight.v2.output_buffering', true);
Flight::request()->url = '/test';
$this->expectOutputString('hooked before starttest');
ob_start();
Flight::start();
$this->assertEquals('hooked before starttest', Flight::response()->getBody());
}
public function testStreamRoute(): void public function testStreamRoute(): void
{ {
$response_mock = new class extends Response { $response_mock = new class extends Response {

Loading…
Cancel
Save