Use context object with settings
parent
231ff5161f
commit
34c2939d14
@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
namespace BitCommunism\Http;
|
||||
|
||||
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
class Context
|
||||
{
|
||||
/**
|
||||
* @var ServerRequestInterface
|
||||
*/
|
||||
private $request;
|
||||
|
||||
/**
|
||||
* @var ResponseInterface
|
||||
*/
|
||||
private $response;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $context = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $vars;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $settings;
|
||||
|
||||
/**
|
||||
* Context constructor.
|
||||
* @param ServerRequestInterface $request
|
||||
* @param ResponseInterface $response
|
||||
* @param array $vars
|
||||
* @param array $settings
|
||||
*/
|
||||
public function __construct(ServerRequestInterface $request, ResponseInterface $response, array $vars = [], array $settings = [])
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->response = $response;
|
||||
$this->vars = $vars;
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ServerRequestInterface
|
||||
*/
|
||||
public function getRequest(): ServerRequestInterface
|
||||
{
|
||||
return $this->request;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
*/
|
||||
public function setRequest(ServerRequestInterface $request): void
|
||||
{
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function getResponse(): ResponseInterface
|
||||
{
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ResponseInterface $response
|
||||
*/
|
||||
public function setResponse(ResponseInterface $response): void
|
||||
{
|
||||
$this->response = $response;
|
||||
}
|
||||
|
||||
public function withResponse($fn)
|
||||
{
|
||||
$this->setResponse($fn($this->getResponse()));
|
||||
}
|
||||
|
||||
public function getSetting($name, $default = null) {
|
||||
return $this->settings[$name] ?? $default;
|
||||
}
|
||||
|
||||
public function getVar($name, $default = null) {
|
||||
return $this->settings[$name] ?? $default;
|
||||
}
|
||||
|
||||
public function __get($name)
|
||||
{
|
||||
return $this->context[$name];
|
||||
}
|
||||
|
||||
public function __set($name, $value)
|
||||
{
|
||||
$this->context[$name] = $value;
|
||||
}
|
||||
|
||||
public function __isset($name)
|
||||
{
|
||||
return isset($this->context[$name]);
|
||||
}
|
||||
|
||||
public function __unset($name)
|
||||
{
|
||||
unset($this->context[$name]);
|
||||
}
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace BitCommunism\Http;
|
||||
|
||||
class Handle
|
||||
{
|
||||
/**
|
||||
* @var callable
|
||||
*/
|
||||
private $call;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $settings = [];
|
||||
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
private $middleware = [];
|
||||
|
||||
public function __construct($call, $settings = [], $middleware = [])
|
||||
{
|
||||
if (is_string($call)) {
|
||||
$call = [$call, 'handle'];
|
||||
}
|
||||
|
||||
$this->call = $call;
|
||||
$this->settings = $settings;
|
||||
$this->middleware = $middleware;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return callable
|
||||
*/
|
||||
public function getCall(): callable
|
||||
{
|
||||
return $this->call;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getSettings(): array
|
||||
{
|
||||
return $this->settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getMiddleware()
|
||||
{
|
||||
return $this->middleware;
|
||||
}
|
||||
|
||||
public function withMiddleware($middleware): Handle
|
||||
{
|
||||
return new static($this->call, $this->settings, array_merge($this->middleware, [$middleware]));
|
||||
}
|
||||
|
||||
public function mergeWithSettings($settings): Handle
|
||||
{
|
||||
return new static($this->call, array_merge($this->settings, $settings), $this->middleware);
|
||||
}
|
||||
|
||||
public function withSettings($settings): Handle
|
||||
{
|
||||
return new static($this->call, $settings, $this->middleware);
|
||||
}
|
||||
|
||||
public function withPrependedMiddleware($middlewares): Handle
|
||||
{
|
||||
return new static($this->call, $this->settings, array_merge($middlewares, $this->middleware));
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace BitCommunism\Http;
|
||||
|
||||
if (function_exists(__NAMESPACE__ . '\\handle')) {
|
||||
function handle($call, $settings = [])
|
||||
{
|
||||
return new Handle($call, $settings);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue