Make next call argumentless and add traits

master
eater 6 years ago
parent 9d8ab04348
commit 554bb6b7e3
Signed by: eater
GPG Key ID: AD2560A0F84F0759

@ -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": {

@ -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();
}
}

@ -0,0 +1,37 @@
<?php
namespace BitCommunism\Http\Context;
use Dflydev\FigCookies\Cookie;
use Dflydev\FigCookies\Cookies;
trait RequestTraits
{
/**
* @var Cookies
*/
protected $cookies;
public function getCookies(): Cookies {
if ($this->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;
}
}

@ -0,0 +1,54 @@
<?php
namespace BitCommunism\Http\Context;
use Dflydev\FigCookies\SetCookie;
use Dflydev\FigCookies\SetCookies;
use Psr\Http\Message\ResponseInterface;
use function GuzzleHttp\Psr7\stream_for;
trait ResponseTraits
{
/**
* @var SetCookies
*/
private $setCookies;
public function json($obj, $options = 0, $depth = 512): void
{
$this->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());
}
}

@ -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)

Loading…
Cancel
Save