Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow rendering to be turned off, fox Lambda usage. #362

Merged
merged 3 commits into from Dec 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .scrutinizer.yml
@@ -1,7 +1,7 @@
build:
environment:
php:
version: 7.2.0
version: 7.4.0
tests:
override:
-
Expand Down
20 changes: 14 additions & 6 deletions src/Lifecycle.php
Expand Up @@ -12,8 +12,6 @@
use Gt\Http\Header\Headers;
use Gt\Http\ServerInfo;
use Gt\Http\RequestFactory;
use Gt\WebEngine\Dispatch\PageDispatcher;
use Gt\WebEngine\FileSystem\Path;
use Gt\WebEngine\Route\PageRouter;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -44,8 +42,11 @@ class Lifecycle implements MiddlewareInterface {
/**
* The start of the application's lifecycle. This function breaks the lifecycle down
* into its different functions, in order.
*
* @param bool $render Set to false and the Lifecycle will not render
* the Response object, allowing you to manipulate it elsewhere.
*/
public function start():void {
public function start(bool $render = true):ResponseInterface {
$server = new ServerInfo($_SERVER);

$cwd = dirname($server->getDocumentRoot());
Expand Down Expand Up @@ -127,7 +128,7 @@ public function start():void {
);

$response = $this->process($request, $dispatcher);
$this->finish($response);
return $this->finish($response, $render);
}

/**
Expand Down Expand Up @@ -234,11 +235,18 @@ public function process(
* after the response headers are appended from any calls to the native
* header function.
*/
public static function finish(ResponseInterface $response):void {
public static function finish(
ResponseInterface $response,
bool $render = true
):ResponseInterface {
foreach($response->getHeaders() as $key => $value) {
header("$key: $value");
}

echo $response->getBody();
if($render) {
echo $response->getBody();
}

return $response;
}
}