改名
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Http\Handler\Abstracts;
|
||||
|
||||
|
||||
|
||||
use Swoole\Coroutine;
|
||||
|
||||
abstract class BaseContext
|
||||
{
|
||||
protected static array $pool = [];
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Handler\Abstracts;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Exception;
|
||||
use Http\Handler\Handler as CHl;
|
||||
use Http\Message\ServerRequest;
|
||||
use Kiri\Core\Help;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
|
||||
abstract class Handler implements RequestHandlerInterface
|
||||
{
|
||||
|
||||
protected int $offset = 0;
|
||||
|
||||
|
||||
/**
|
||||
* @param CHl $handler
|
||||
* @param array|null $middlewares
|
||||
*/
|
||||
public function __construct(public CHl $handler, public ?array $middlewares)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @return ResponseInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function execute(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
if (empty($this->middlewares) || !isset($this->middlewares[$this->offset])) {
|
||||
return $this->dispatcher($request);
|
||||
}
|
||||
|
||||
$middleware = $this->middlewares[$this->offset];
|
||||
if (!($middleware instanceof MiddlewareInterface)) {
|
||||
throw new Exception('get_implements_class($middleware) not found method process.');
|
||||
}
|
||||
|
||||
$this->offset++;
|
||||
|
||||
return $middleware->process($request, $this);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function dispatcher(ServerRequestInterface $request): mixed
|
||||
{
|
||||
$response = call_user_func($this->handler->callback, ...$this->handler->params);
|
||||
if (!($response instanceof ResponseInterface)) {
|
||||
$response = $this->transferToResponse($response);
|
||||
}
|
||||
$response->withHeader('Run-Time', $this->_runTime($request));
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ServerRequest $request
|
||||
* @return float
|
||||
*/
|
||||
private function _runTime(ServerRequestInterface $request): float
|
||||
{
|
||||
$float = microtime(true) - time();
|
||||
|
||||
$serverParams = $request->getServerParams();
|
||||
|
||||
$rTime = $serverParams['request_time_float'] - $serverParams['request_time'];
|
||||
|
||||
return round($float - $rTime, 6);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $responseData
|
||||
* @return \Server\Constrict\ResponseInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
private function transferToResponse(mixed $responseData): ResponseInterface
|
||||
{
|
||||
$interface = response()->withStatus(200);
|
||||
if (!$interface->hasContentType()) {
|
||||
$interface->withContentType('application/json;charset=utf-8');
|
||||
}
|
||||
if (str_contains($interface->getContentType(), 'xml')) {
|
||||
if (is_object($responseData)) {
|
||||
$responseData = get_object_vars($responseData);
|
||||
}
|
||||
$interface->getBody()->write(Help::toXml($responseData));
|
||||
} else if (is_array($responseData)) {
|
||||
$interface->getBody()->write(json_encode($responseData));
|
||||
} else {
|
||||
$interface->getBody()->write((string)$responseData);
|
||||
}
|
||||
return $interface;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Handler\Abstracts;
|
||||
|
||||
use Closure;
|
||||
|
||||
class HandlerManager
|
||||
{
|
||||
|
||||
|
||||
private static array $handlers = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param string $method
|
||||
* @param \Http\Handler\Handler|Closure $handler
|
||||
*/
|
||||
public static function add(string $path, string $method, \Http\Handler\Handler|Closure $handler)
|
||||
{
|
||||
if (!isset(static::$handlers[$path])) {
|
||||
static::$handlers[$path] = [];
|
||||
}
|
||||
static::$handlers[$path][$method] = $handler;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
* @param $method
|
||||
* @return null|int|array|Closure
|
||||
*/
|
||||
public static function get($path, $method): null|int|\Http\Handler\Handler|Closure
|
||||
{
|
||||
if (!isset(static::$handlers[$path])) {
|
||||
return null;
|
||||
}
|
||||
$array = static::$handlers[$path][$method] ?? null;
|
||||
if (is_null($array)) {
|
||||
return 405;
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public static function getHandlers(): array
|
||||
{
|
||||
return static::$handlers;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public static function dump(): array
|
||||
{
|
||||
$array = [];
|
||||
foreach (static::$handlers as $path => $handlers) {
|
||||
$array[] = [
|
||||
'path' => $path,
|
||||
'method' => implode(',', array_keys($handlers))
|
||||
];
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Http\Handler\Abstracts;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Kiri;
|
||||
|
||||
|
||||
/**
|
||||
* Class HttpService
|
||||
* @package Http\Abstracts
|
||||
*/
|
||||
abstract class HttpService extends Component
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param string $category
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function write($message, string $category = 'app')
|
||||
{
|
||||
$logger = Kiri::app()->getLogger();
|
||||
$logger->write($message, $category);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __get($name): mixed
|
||||
{
|
||||
if (method_exists($this, $name)) {
|
||||
return $this->{$name}();
|
||||
}
|
||||
$handler = 'get' . ucfirst($name);
|
||||
if (method_exists($this, $handler)) {
|
||||
return $this->{$handler}();
|
||||
}
|
||||
if (property_exists($this, $name)) {
|
||||
return $this->$name;
|
||||
}
|
||||
$message = sprintf('method %s::%s not exists.', static::class, $name);
|
||||
throw new Exception($message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Handler\Abstracts;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Http\Constrict\ResponseInterface;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
abstract class Middleware implements MiddlewareInterface
|
||||
{
|
||||
|
||||
|
||||
#[Inject(ResponseInterface::class)]
|
||||
public ResponseInterface $response;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Http\Handler\Abstracts;
|
||||
|
||||
|
||||
use Closure;
|
||||
use Co\Iterator;
|
||||
use Kiri\Abstracts\BaseObject;
|
||||
|
||||
|
||||
/**
|
||||
* Class MiddlewareManager
|
||||
* @package Http\Route
|
||||
*/
|
||||
class MiddlewareManager extends BaseObject
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var array<string, Iterator>
|
||||
*/
|
||||
private static array $_middlewares = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @param $method
|
||||
* @param array|string|null $middlewares
|
||||
* @return bool
|
||||
*/
|
||||
public static function add($class, $method, array|string|null $middlewares): bool
|
||||
{
|
||||
[$class, $method] = static::setDefault($class, $method);
|
||||
if (empty($middlewares)) {
|
||||
return false;
|
||||
}
|
||||
if (is_string($middlewares)) {
|
||||
$middlewares = [$middlewares];
|
||||
}
|
||||
$source = &static::$_middlewares[$class][$method];
|
||||
foreach ($middlewares as $middleware) {
|
||||
$middleware = di($middleware);
|
||||
if (in_array($middleware, $source)) {
|
||||
continue;
|
||||
}
|
||||
$source[] = $middleware;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @param $method
|
||||
* @return array
|
||||
*/
|
||||
private static function setDefault($class, $method): array
|
||||
{
|
||||
if (is_object($class)) {
|
||||
$class = $class::class;
|
||||
}
|
||||
if (!isset(static::$_middlewares[$class])) {
|
||||
static::$_middlewares[$class] = [];
|
||||
}
|
||||
if (!isset(static::$_middlewares[$class][$method])) {
|
||||
static::$_middlewares[$class][$method] = [];
|
||||
}
|
||||
return [$class, $method];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $handler
|
||||
* @return Iterator|null
|
||||
*/
|
||||
public static function get($handler): ?array
|
||||
{
|
||||
if (!($handler instanceof Closure)) {
|
||||
return static::$_middlewares[$handler[0]][$handler[1]] ?? null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user