From 3fe2ca1d061076b4ab864e668f35cb4c9fbbab15 Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Wed, 1 Apr 2026 11:12:14 -0400 Subject: [PATCH] remove v2_output_buffering --- src/Engine.php | 41 ++------------------ src/net/Response.php | 23 +----------- tests/DocExamplesTest.php | 19 ---------- tests/EngineTest.php | 78 --------------------------------------- tests/FlightTest.php | 19 ---------- 5 files changed, 6 insertions(+), 174 deletions(-) diff --git a/src/Engine.php b/src/Engine.php index a8639e7..eefbab5 100644 --- a/src/Engine.php +++ b/src/Engine.php @@ -170,7 +170,6 @@ class Engine $this->set('flight.views.path', './views'); $this->set('flight.views.extension', '.php'); $this->set('flight.content_length', true); - $this->set('flight.v2.output_buffering', false); // Startup configuration $this->before('start', function (): void { @@ -185,11 +184,6 @@ class Engine // Set 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; @@ -431,9 +425,7 @@ class Engine } // This is the way that v3 handles output buffering (which captures output correctly) - $useV3OutputBuffering = - $this->response()->v2_output_buffering === false && - $route->is_streamed === false; + $useV3OutputBuffering = !$route->is_streamed; if ($useV3OutputBuffering === true) { ob_start(); @@ -505,17 +497,6 @@ class Engine $response = $this->response(); $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 $failedMiddlewareCheck = false; while ($route = $router->route($request)) { @@ -565,9 +546,7 @@ class Engine $this->triggerEvent('flight.middleware.before', $route); } - $useV3OutputBuffering = - $this->response()->v2_output_buffering === false && - $route->is_streamed === false; + $useV3OutputBuffering = !$route->is_streamed; if ($useV3OutputBuffering === true) { ob_start(); @@ -675,10 +654,6 @@ class Engine $response->status($code); } - if ($response->v2_output_buffering === true && ob_get_length() > 0) { - $response->write(ob_get_clean()); - } - $response->send(); } } @@ -903,9 +878,6 @@ class Engine ->status($code) ->header('Content-Type', 'application/json') ->write($json); - if ($this->response()->v2_output_buffering === true) { - $this->response()->send(); - } } /** @@ -928,10 +900,8 @@ class Engine ): void { $this->json($data, $code, $encode, $charset, $option); $jsonBody = $this->response()->getBody(); - if ($this->response()->v2_output_buffering === false) { - $this->response()->clearBody(); - $this->response()->send(); - } + $this->response()->clearBody(); + $this->response()->send(); $this->halt($code, $jsonBody, empty(getenv('PHPUNIT_TEST'))); } @@ -962,9 +932,6 @@ class Engine ->status($code) ->header('Content-Type', 'application/javascript; charset=' . $charset) ->write($callback . '(' . $json . ');'); - if ($this->response()->v2_output_buffering === true) { - $this->response()->send(); - } } /** diff --git a/src/net/Response.php b/src/net/Response.php index 1a738cf..e848640 100644 --- a/src/net/Response.php +++ b/src/net/Response.php @@ -22,15 +22,6 @@ class Response */ 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 * @@ -271,7 +262,7 @@ class Response $this->clearBody(); // 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(); } @@ -421,18 +412,8 @@ class Response */ 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); - // Only for the v3 output buffering. - if ($this->v2_output_buffering === false) { - $this->processResponseCallbacks(); - } + $this->processResponseCallbacks(); if ($this->headersSent() === false) { $this->sendHeaders(); diff --git a/tests/DocExamplesTest.php b/tests/DocExamplesTest.php index 96121f3..33f92bc 100644 --- a/tests/DocExamplesTest.php +++ b/tests/DocExamplesTest.php @@ -45,25 +45,6 @@ class DocExamplesTest extends TestCase $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 { Flight::map('error', function (Throwable $error) { diff --git a/tests/EngineTest.php b/tests/EngineTest.php index d89e453..b94bb60 100644 --- a/tests/EngineTest.php +++ b/tests/EngineTest.php @@ -49,25 +49,6 @@ class EngineTest extends TestCase $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 { $engine = new Engine(); @@ -322,34 +303,6 @@ class EngineTest extends TestCase $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 { $engine = new Engine(); @@ -519,16 +472,6 @@ class EngineTest extends TestCase $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 { $engine = new Engine(); @@ -549,17 +492,6 @@ class EngineTest extends TestCase $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 { $engine = new Engine(); @@ -569,16 +501,6 @@ class EngineTest extends TestCase $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 { $engine = new Engine(); diff --git a/tests/FlightTest.php b/tests/FlightTest.php index c74feaa..7c9d60f 100644 --- a/tests/FlightTest.php +++ b/tests/FlightTest.php @@ -275,25 +275,6 @@ class FlightTest extends TestCase $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 { $response_mock = new class extends Response {