diff --git a/composer.json b/composer.json index 57f5260..f863dbd 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "require": { "nikic/fast-route": "^1.2", "guzzlehttp/psr7": "^1.4", - "php-di/php-di": "^5.4" + "php-di/php-di": "^6" }, "autoload": { "psr-4": { diff --git a/src/Context.php b/src/Context.php index 265c98b..17b523c 100644 --- a/src/Context.php +++ b/src/Context.php @@ -86,14 +86,38 @@ class Context $this->setResponse($fn($this->getResponse())); } - public function getSetting($name, $default = null) { + public function getSetting($name, $default = null) + { return $this->settings[$name] ?? $default; } - public function getVar($name, $default = null) { + public function getVar($name, $default = null) + { return $this->settings[$name] ?? $default; } + /** + * @return array + */ + public function getVars(): array + { + return $this->vars; + } + + /** + * @return array + */ + public function getSettings(): array + { + return $this->settings; + } + + public function redirect($to, $status = 302): void { + $this->withResponse(function (ResponseInterface $response) use ($to, $status) { + return $response->withStatus($status)->withHeader('Location', $to); + }); + } + public function __get($name) { return $this->context[$name]; diff --git a/src/Server.php b/src/Server.php index b684944..26d18c9 100644 --- a/src/Server.php +++ b/src/Server.php @@ -155,6 +155,8 @@ class Server 'request' => $context->getRequest(), 'response' => $context->getResponse(), 'context' => $context, + 'vars' => $context->getVars(), + 'settings' => $context->getSettings(), ]); $this->normalizeResponse($output, $context); diff --git a/src/functions.php b/src/functions.php index 6eb9e69..debc2ef 100644 --- a/src/functions.php +++ b/src/functions.php @@ -2,9 +2,20 @@ namespace BitCommunism\Http; +use Psr\Http\Message\ResponseInterface; + if (function_exists(__NAMESPACE__ . '\\handle')) { function handle($call, $settings = []) { return new Handle($call, $settings); } +} + +if (function_exists(__NAMESPACE__ . '\\redirect')) { + function redirect($to, $status = 302) + { + return function (Context $c) use ($to, $status) { + $c->redirect($to, $status); + }; + } } \ No newline at end of file