diff --git a/composer.json b/composer.json index 8b00f03..bc674f4 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,8 @@ "require": { "nikic/fast-route": "^1.2", "guzzlehttp/psr7": "^1.4", - "php-di/php-di": "^6" + "php-di/php-di": "^6", + "dflydev/fig-cookies": "^2.0" }, "autoload": { "psr-4": { @@ -12,4 +13,4 @@ }, "files": ["src/functions.php"] } -} \ No newline at end of file +} diff --git a/src/Context.php b/src/Context.php index db20f17..7144b1c 100644 --- a/src/Context.php +++ b/src/Context.php @@ -3,12 +3,17 @@ namespace BitCommunism\Http; +use BitCommunism\Http\Context\RequestTraits; +use BitCommunism\Http\Context\ResponseTraits; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; class Context implements \ArrayAccess { + use ResponseTraits; + use RequestTraits; + /** * @var ServerRequestInterface */ @@ -34,6 +39,16 @@ class Context implements \ArrayAccess */ private $settings; + /** + * @var array + */ + public $body; + + /** + * @var array + */ + public $query; + /** * Context constructor. * @param ServerRequestInterface $request @@ -45,6 +60,8 @@ class Context implements \ArrayAccess { $this->request = $request; $this->response = $response; + $this->body = (array)$request->getParsedBody(); + $this->query = $request->getQueryParams(); $this->vars = $vars; $this->settings = $settings; } @@ -120,12 +137,6 @@ class Context implements \ArrayAccess $this->settings = $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 offsetExists($offset) { return isset($this->context[$offset]); @@ -145,4 +156,8 @@ class Context implements \ArrayAccess { unset($this->context[$offset]); } + + public function renderResponse() { + return $this->getResponse(); + } } \ No newline at end of file diff --git a/src/Context/RequestTraits.php b/src/Context/RequestTraits.php new file mode 100644 index 0000000..a162282 --- /dev/null +++ b/src/Context/RequestTraits.php @@ -0,0 +1,37 @@ +cookies === null) { + $this->cookies = Cookies::fromRequest($this->getRequest()); + } + + return $this->cookies; + } + + public function getCookie($name): ?Cookie { + return $this->getCookies()->get($name); + } + + public function getCookieValue(string $name, string $default = ""): string { + $cookie = $this->getCookie($name); + + if ($cookie !== null) { + return $cookie->getValue(); + } + + return $default; + } +} \ No newline at end of file diff --git a/src/Context/ResponseTraits.php b/src/Context/ResponseTraits.php new file mode 100644 index 0000000..a2184b1 --- /dev/null +++ b/src/Context/ResponseTraits.php @@ -0,0 +1,54 @@ +withResponse(function (ResponseInterface $response) use ($obj, $options, $depth) { + return $response->withHeader('Content-Type', 'application/json') + ->withBody(stream_for(json_encode($obj, $options, $depth))); + }); + } + + public function redirect($to, $status = 302): void + { + $this->withResponse(function (ResponseInterface $response) use ($to, $status) { + return $response->withStatus($status)->withHeader('Location', $to); + }); + } + + public function getSetCookies(): SetCookies { + if ($this->setCookies === null) { + $this->setCookies = SetCookies::fromResponse($this->getResponse()); + } + + return $this->setCookies; + } + + public function setCookie($name, $value) { + $this->addCookie(SetCookie::create($name, $value)); + } + + public function addCookie(SetCookie $cookie) { + $this->setCookies = $this->setCookies->with($cookie); + } + + public function renderResponse() { + return $this->setCookies->renderIntoSetCookieHeader(parent::renderResponse()); + } +} \ No newline at end of file diff --git a/src/Server.php b/src/Server.php index 1e1ef26..6aff5c9 100644 --- a/src/Server.php +++ b/src/Server.php @@ -164,7 +164,7 @@ class Server $context = new Context($request, $response, $vars); - $next = function (Context $context) use ($handler, $middlewareArray, &$middlewarePointer, &$next) { + $next = function () use ($context, $handler, $middlewareArray, &$middlewarePointer, &$next) { $output = null; if (count($middlewareArray) > $middlewarePointer) { @@ -194,9 +194,9 @@ class Server $this->normalizeResponse($output, $context); }; - $next($context); + $next(); - return $context->getResponse(); + return $context->renderResponse(); } private function normalizeResponse($response, Context $context)