Skip to content

Commit

Permalink
Merge pull request #594 from flightphp/maintain-headers
Browse files Browse the repository at this point in the history
Maintains headers on redirect, error, and halt. Added jsonHalt
  • Loading branch information
n0nag0n authored May 30, 2024
2 parents 10165eb + 5549e91 commit c8a1c88
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 5 deletions.
41 changes: 36 additions & 5 deletions flight/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
* @method void redirect(string $url, int $code = 303) Redirects the current request to another URL.
* @method void json(mixed $data, int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0)
* Sends a JSON response.
* @method void jsonHalt(mixed $data, int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0)
* Sends a JSON response and immediately halts the request.
* @method void jsonp(mixed $data, string $param = 'jsonp', int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0)
* Sends a JSONP response.
*
Expand All @@ -73,7 +75,7 @@ class Engine
*/
private const MAPPABLE_METHODS = [
'start', 'stop', 'route', 'halt', 'error', 'notFound',
'render', 'redirect', 'etag', 'lastModified', 'json', 'jsonp',
'render', 'redirect', 'etag', 'lastModified', 'json', 'jsonHalt', 'jsonp',
'post', 'put', 'patch', 'delete', 'group', 'getUrl'
];

Expand Down Expand Up @@ -608,7 +610,7 @@ public function _error(Throwable $e): void

try {
$this->response()
->clear()
->clearBody()
->status(500)
->write($msg)
->send();
Expand Down Expand Up @@ -735,7 +737,7 @@ public function _delete(string $pattern, $callback, bool $pass_route = false, st
public function _halt(int $code = 200, string $message = '', bool $actuallyExit = true): void
{
$this->response()
->clear()
->clearBody()
->status($code)
->write($message)
->send();
Expand All @@ -750,7 +752,7 @@ public function _notFound(): void
$output = '<h1>404 Not Found</h1><h3>The page you have requested could not be found.</h3>';

$this->response()
->clear()
->clearBody()
->status(404)
->write($output)
->send();
Expand All @@ -775,7 +777,7 @@ public function _redirect(string $url, int $code = 303): void
}

$this->response()
->clear()
->clearBody()
->status($code)
->header('Location', $url)
->send();
Expand Down Expand Up @@ -829,6 +831,33 @@ public function _json(
}
}

/**
* Sends a JSON response and halts execution immediately.
*
* @param mixed $data JSON data
* @param int $code HTTP status code
* @param bool $encode Whether to perform JSON encoding
* @param string $charset Charset
* @param int $option Bitmask Json constant such as JSON_HEX_QUOT
*
* @throws Exception
*/
public function _jsonHalt(
$data,
int $code = 200,
bool $encode = true,
string $charset = 'utf-8',
int $option = 0
): 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->halt($code, $jsonBody, empty(getenv('PHPUNIT_TEST')));
}

/**
* Sends a JSONP response.
*
Expand Down Expand Up @@ -877,6 +906,7 @@ public function _etag(string $id, string $type = 'strong'): void
isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
$_SERVER['HTTP_IF_NONE_MATCH'] === $id
) {
$this->response()->clear();
$this->halt(304, '', empty(getenv('PHPUNIT_TEST')));
}
}
Expand All @@ -894,6 +924,7 @@ public function _lastModified(int $time): void
isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) === $time
) {
$this->response()->clear();
$this->halt(304, '', empty(getenv('PHPUNIT_TEST')));
}
}
Expand Down
2 changes: 2 additions & 0 deletions flight/Flight.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
* @method static void redirect(string $url, int $code = 303) Redirects to another URL.
* @method static void json(mixed $data, int $code = 200, bool $encode = true, string $charset = "utf8", int $encodeOption = 0, int $encodeDepth = 512)
* Sends a JSON response.
* @method void jsonHalt(mixed $data, int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0)
* Sends a JSON response and immediately halts the request.
* @method static void jsonp(mixed $data, string $param = 'jsonp', int $code = 200, bool $encode = true, string $charset = "utf8", int $encodeOption = 0, int $encodeDepth = 512)
* Sends a JSONP response.
* @method static void error(Throwable $exception) Sends an HTTP 500 response.
Expand Down
10 changes: 10 additions & 0 deletions tests/EngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,16 @@ public function testJsonV2OutputBuffering()
$this->assertEquals(200, $engine->response()->status());
}

public function testJsonHalt()
{
$engine = new Engine();
$this->expectOutputString('{"key1":"value1","key2":"value2"}');
$engine->jsonHalt(['key1' => 'value1', 'key2' => 'value2']);
$this->assertEquals('application/json; charset=utf-8', $engine->response()->headers()['Content-Type']);
$this->assertEquals(200, $engine->response()->status());
$this->assertEquals('{"key1":"value1","key2":"value2"}', $engine->response()->getBody());
}

public function testJsonP()
{
$engine = new Engine();
Expand Down
4 changes: 4 additions & 0 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,10 @@ public function testRewindAndValid()
$this->router->rewind();
$result = $this->router->valid();
$this->assertTrue($result);

$this->router->previous();
$result = $this->router->valid();
$this->assertFalse($result);
}

public function testGetRootUrlByAlias()
Expand Down
5 changes: 5 additions & 0 deletions tests/server-v2/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ public function before()
echo "\n\n\n\n\n";
});

Flight::route('/json-halt', function () {
Flight::jsonHalt(['message' => 'JSON rendered and halted successfully with no other body content!']);
});

// Test 10: Halt
Flight::route('/halt', function () {
Flight::halt(400, 'Halt worked successfully');
Expand Down Expand Up @@ -200,6 +204,7 @@ public function before()
<li><a href="/error">Error</a></li>
<li><a href="/json">JSON</a></li>
<li><a href="/jsonp?jsonp=myjson">JSONP</a></li>
<li><a href="/json-halt">JSON Halt</a></li>
<li><a href="/halt">Halt</a></li>
<li><a href="/redirect">Redirect</a></li>
</ul>';
Expand Down
1 change: 1 addition & 0 deletions tests/server/LayoutMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function before()
<li><a href="/error">Error</a></li>
<li><a href="/json">JSON</a></li>
<li><a href="/jsonp?jsonp=myjson">JSONP</a></li>
<li><a href="/json-halt">JSON Halt</a></li>
<li><a href="/halt">Halt</a></li>
<li><a href="/redirect">Redirect</a></li>
<li><a href="/streamResponse">Stream Plain</a></li>
Expand Down
4 changes: 4 additions & 0 deletions tests/server/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@
Flight::jsonp(['message' => 'JSONP renders successfully!'], 'jsonp');
});

Flight::route('/json-halt', function () {
Flight::jsonHalt(['message' => 'JSON rendered and halted successfully with no other body content!']);
});

Flight::map('error', function (Throwable $e) {
echo sprintf(
<<<HTML
Expand Down

0 comments on commit c8a1c88

Please sign in to comment.