Make next call argumentless and add traits
This commit is contained in:
parent
9d8ab04348
commit
554bb6b7e3
5 changed files with 118 additions and 11 deletions
|
@ -4,7 +4,8 @@
|
||||||
"require": {
|
"require": {
|
||||||
"nikic/fast-route": "^1.2",
|
"nikic/fast-route": "^1.2",
|
||||||
"guzzlehttp/psr7": "^1.4",
|
"guzzlehttp/psr7": "^1.4",
|
||||||
"php-di/php-di": "^6"
|
"php-di/php-di": "^6",
|
||||||
|
"dflydev/fig-cookies": "^2.0"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
|
|
|
@ -3,12 +3,17 @@
|
||||||
namespace BitCommunism\Http;
|
namespace BitCommunism\Http;
|
||||||
|
|
||||||
|
|
||||||
|
use BitCommunism\Http\Context\RequestTraits;
|
||||||
|
use BitCommunism\Http\Context\ResponseTraits;
|
||||||
use Psr\Http\Message\RequestInterface;
|
use Psr\Http\Message\RequestInterface;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
|
||||||
class Context implements \ArrayAccess
|
class Context implements \ArrayAccess
|
||||||
{
|
{
|
||||||
|
use ResponseTraits;
|
||||||
|
use RequestTraits;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ServerRequestInterface
|
* @var ServerRequestInterface
|
||||||
*/
|
*/
|
||||||
|
@ -34,6 +39,16 @@ class Context implements \ArrayAccess
|
||||||
*/
|
*/
|
||||||
private $settings;
|
private $settings;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
public $body;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
public $query;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Context constructor.
|
* Context constructor.
|
||||||
* @param ServerRequestInterface $request
|
* @param ServerRequestInterface $request
|
||||||
|
@ -45,6 +60,8 @@ class Context implements \ArrayAccess
|
||||||
{
|
{
|
||||||
$this->request = $request;
|
$this->request = $request;
|
||||||
$this->response = $response;
|
$this->response = $response;
|
||||||
|
$this->body = (array)$request->getParsedBody();
|
||||||
|
$this->query = $request->getQueryParams();
|
||||||
$this->vars = $vars;
|
$this->vars = $vars;
|
||||||
$this->settings = $settings;
|
$this->settings = $settings;
|
||||||
}
|
}
|
||||||
|
@ -120,12 +137,6 @@ class Context implements \ArrayAccess
|
||||||
$this->settings = $settings;
|
$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)
|
public function offsetExists($offset)
|
||||||
{
|
{
|
||||||
return isset($this->context[$offset]);
|
return isset($this->context[$offset]);
|
||||||
|
@ -145,4 +156,8 @@ class Context implements \ArrayAccess
|
||||||
{
|
{
|
||||||
unset($this->context[$offset]);
|
unset($this->context[$offset]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function renderResponse() {
|
||||||
|
return $this->getResponse();
|
||||||
|
}
|
||||||
}
|
}
|
37
src/Context/RequestTraits.php
Normal file
37
src/Context/RequestTraits.php
Normal file
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
54
src/Context/ResponseTraits.php
Normal file
54
src/Context/ResponseTraits.php
Normal file
|
@ -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);
|
$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;
|
$output = null;
|
||||||
|
|
||||||
if (count($middlewareArray) > $middlewarePointer) {
|
if (count($middlewareArray) > $middlewarePointer) {
|
||||||
|
@ -194,9 +194,9 @@ class Server
|
||||||
$this->normalizeResponse($output, $context);
|
$this->normalizeResponse($output, $context);
|
||||||
};
|
};
|
||||||
|
|
||||||
$next($context);
|
$next();
|
||||||
|
|
||||||
return $context->getResponse();
|
return $context->renderResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function normalizeResponse($response, Context $context)
|
private function normalizeResponse($response, Context $context)
|
||||||
|
|
Loading…
Reference in a new issue