From 47cd98e78ec498f8e6fc6ada835f9229f980dd75 Mon Sep 17 00:00:00 2001
From: n0nag0n <n0nag0n@sky-9.com>
Date: Sat, 24 Feb 2024 08:38:25 -0700
Subject: [PATCH 1/2] added ability to overwrite the body

---
 flight/Engine.php                        |  8 ++++----
 flight/net/Response.php                  | 20 ++++++++++++++++--
 tests/FlightTest.php                     | 26 ++++++++++++++++++++++++
 tests/ResponseTest.php                   | 17 ++++++++++++++++
 tests/server/LayoutMiddleware.php        |  1 +
 tests/server/OverwriteBodyMiddleware.php | 12 +++++++++++
 tests/server/index.php                   |  5 +++++
 7 files changed, 83 insertions(+), 6 deletions(-)
 create mode 100644 tests/server/OverwriteBodyMiddleware.php

diff --git a/flight/Engine.php b/flight/Engine.php
index fb4d9e84..aaaf7785 100644
--- a/flight/Engine.php
+++ b/flight/Engine.php
@@ -401,8 +401,8 @@ protected function processMiddleware(Route $route, string $event_name): bool
                 continue;
             }
 
-            $use_v3_output_buffering = 
-                $this->response()->v2_output_buffering === false && 
+            $use_v3_output_buffering =
+                $this->response()->v2_output_buffering === false &&
                 $route->is_streamed === false;
 
             if ($use_v3_output_buffering === true) {
@@ -493,8 +493,8 @@ public function _start(): void
                 }
             }
 
-            $use_v3_output_buffering = 
-                $this->response()->v2_output_buffering === false && 
+            $use_v3_output_buffering =
+                $this->response()->v2_output_buffering === false &&
                 $route->is_streamed === false;
 
             if ($use_v3_output_buffering === true) {
diff --git a/flight/net/Response.php b/flight/net/Response.php
index cfc8ffdb..e1abaac2 100644
--- a/flight/net/Response.php
+++ b/flight/net/Response.php
@@ -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.
      *
@@ -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) {
diff --git a/tests/FlightTest.php b/tests/FlightTest.php
index a6ffa163..042b6bb4 100644
--- a/tests/FlightTest.php
+++ b/tests/FlightTest.php
@@ -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');
+    }
 }
diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php
index d7155fca..42701d4c 100644
--- a/tests/ResponseTest.php
+++ b/tests/ResponseTest.php
@@ -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());
+    }
 }
diff --git a/tests/server/LayoutMiddleware.php b/tests/server/LayoutMiddleware.php
index d5dfcdea..24538f9c 100644
--- a/tests/server/LayoutMiddleware.php
+++ b/tests/server/LayoutMiddleware.php
@@ -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">';
diff --git a/tests/server/OverwriteBodyMiddleware.php b/tests/server/OverwriteBodyMiddleware.php
new file mode 100644
index 00000000..79e1194a
--- /dev/null
+++ b/tests/server/OverwriteBodyMiddleware.php
@@ -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);
+    }
+}
diff --git a/tests/server/index.php b/tests/server/index.php
index e3ea703f..040cbfc1 100644
--- a/tests/server/index.php
+++ b/tests/server/index.php
@@ -17,6 +17,7 @@
 //Flight::set('flight.v2.output_buffering', true);
 
 require_once 'LayoutMiddleware.php';
+require_once 'OverwriteBodyMiddleware.php';
 
 Flight::group('', function () {
 
@@ -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)

From c1ba04d96e40781698dcf7ddf060a9e869d37f36 Mon Sep 17 00:00:00 2001
From: n0nag0n <n0nag0n@sky-9.com>
Date: Sun, 25 Feb 2024 14:54:08 -0700
Subject: [PATCH 2/2] added deprecated tag to stop. Halt is better

---
 flight/Engine.php | 1 +
 1 file changed, 1 insertion(+)

diff --git a/flight/Engine.php b/flight/Engine.php
index aaaf7785..b0ae09da 100644
--- a/flight/Engine.php
+++ b/flight/Engine.php
@@ -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
     {