改名
This commit is contained in:
@@ -295,7 +295,7 @@ class Container extends BaseObject implements ContainerInterface
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionClass|string $class
|
||||
* @param string $className
|
||||
* @param string $method
|
||||
* @return array|null
|
||||
* @throws ReflectionException
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Handler\Abstracts;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Http\Handler\Handler as CHl;
|
||||
|
||||
|
||||
abstract class Handler implements RequestHandlerInterface
|
||||
{
|
||||
|
||||
|
||||
private int $offset = 0;
|
||||
|
||||
|
||||
/**
|
||||
* @param CHl $handler
|
||||
* @param array|null $middlewares
|
||||
*/
|
||||
public function __construct(protected CHl $handler, protected ?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 call_user_func($this->handler->callback, ...$this->handler->params);
|
||||
}
|
||||
|
||||
$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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Handler\Abstracts;
|
||||
|
||||
use Closure;
|
||||
use Kiri\Kiri;
|
||||
|
||||
class HandlerManager
|
||||
{
|
||||
|
||||
|
||||
private static array $handlers = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
* @param $method
|
||||
* @param $handler
|
||||
*/
|
||||
public static function add($path, $method, $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;
|
||||
}
|
||||
if ($array instanceof Closure) {
|
||||
return $array;
|
||||
}
|
||||
$array[1] = Kiri::getDi()->get($array[1]);
|
||||
return $array;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Handler\Abstracts;
|
||||
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
|
||||
abstract class Middleware implements MiddlewareInterface
|
||||
{
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Handler\Annotation;
|
||||
|
||||
use Annotation\Attribute;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_CLASS)] class ControllerTarget extends Attribute
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Handler;
|
||||
|
||||
use Http\Handler\Abstracts\Middleware;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
class CoreMiddleware extends Middleware
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @param RequestHandlerInterface $handler
|
||||
* @return ResponseInterface
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
// TODO: Implement process() method.
|
||||
return $handler->handle($request);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Handler;
|
||||
|
||||
class DataGrip
|
||||
{
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Handler;
|
||||
|
||||
use Exception;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
class Dispatcher extends \Http\Handler\Abstracts\Handler
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @return ResponseInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
return $this->execute($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Handler;
|
||||
|
||||
use Closure;
|
||||
use Kiri\Kiri;
|
||||
|
||||
class Handler
|
||||
{
|
||||
|
||||
|
||||
public string $route = '';
|
||||
|
||||
|
||||
public array|Closure|null $callback;
|
||||
|
||||
|
||||
public ?array $params = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param string $route
|
||||
* @param array|Closure $callback
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function __construct(string $route, array|Closure $callback)
|
||||
{
|
||||
$this->route = $route;
|
||||
|
||||
$this->_injectParams($callback);
|
||||
|
||||
$this->callback = $callback;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array|Closure $callback
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
private function _injectParams(array|Closure $callback)
|
||||
{
|
||||
$container = Kiri::getDi();
|
||||
if (!($callback instanceof Closure)) {
|
||||
$this->params = $container->getMethodParameters($callback[0], $callback[1]);
|
||||
} else {
|
||||
$this->params = $container->getFunctionParameters($callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Handler;
|
||||
|
||||
use Closure;
|
||||
use Http\Handler\Abstracts\HandlerManager;
|
||||
use Http\Route\MiddlewareManager;
|
||||
|
||||
class Router
|
||||
{
|
||||
|
||||
|
||||
private array $groupTack = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string|Closure $closure
|
||||
* @param array $options
|
||||
*/
|
||||
public function get(string $route, string|Closure $closure, array $options = [])
|
||||
{
|
||||
array_push($this->groupTack, $options);
|
||||
|
||||
$this->addRoute('GET', $route, $closure);
|
||||
|
||||
array_pop($this->groupTack);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string|Closure $closure
|
||||
* @param array $options
|
||||
*/
|
||||
public function post(string $route, string|Closure $closure, array $options = [])
|
||||
{
|
||||
array_push($this->groupTack, $options);
|
||||
|
||||
$this->addRoute('POST', $route, $closure);
|
||||
|
||||
array_pop($this->groupTack);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string|array $method
|
||||
* @param string $route
|
||||
* @param string|Closure $closure
|
||||
*/
|
||||
public function addRoute(string|array $method, string $route, string|Closure $closure)
|
||||
{
|
||||
if (!is_array($method)) $method = [$method];
|
||||
$route = $this->getPath($route);
|
||||
if (is_string($closure)) {
|
||||
$closure = explode('@', $closure);
|
||||
$controller = $this->addNamespace($closure[0]);
|
||||
if (!class_exists($controller)) {
|
||||
return;
|
||||
}
|
||||
$this->addMiddlewares($controller, $closure[0]);
|
||||
}
|
||||
foreach ($method as $value) {
|
||||
HandlerManager::add($route, $value, $closure);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
* @param Closure $closure
|
||||
*/
|
||||
public function group(array $config, Closure $closure)
|
||||
{
|
||||
array_push($this->groupTack, $config);
|
||||
|
||||
call_user_func($closure, $this);
|
||||
|
||||
array_pop($this->groupTack);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $route
|
||||
* @return string
|
||||
*/
|
||||
protected function getPath(string $route): string
|
||||
{
|
||||
$route = ltrim($route, '/');
|
||||
$prefix = array_column($this->groupTack, 'prefix');
|
||||
if (empty($prefix = array_filter($prefix))) {
|
||||
return $route;
|
||||
}
|
||||
return implode('/', $prefix) . $route;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $controller
|
||||
* @param $method
|
||||
*/
|
||||
protected function addMiddlewares($controller, $method)
|
||||
{
|
||||
$middleware = array_column($this->groupTack, 'middleware');
|
||||
if (empty($middleware = array_filter($middleware))) {
|
||||
return;
|
||||
}
|
||||
MiddlewareManager::add($controller, $method, $middleware);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @return string|null
|
||||
*/
|
||||
protected function addNamespace($class): ?string
|
||||
{
|
||||
$middleware = array_column($this->groupTack, 'namespace');
|
||||
if (empty($middleware = array_filter($middleware))) {
|
||||
return $class;
|
||||
}
|
||||
$middleware[] = $class;
|
||||
return implode('\\', array_map(function ($value) {
|
||||
return trim($value, '\\');
|
||||
}, $middleware));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Handler;
|
||||
|
||||
use Exception;
|
||||
use Http\Context\Context;
|
||||
use Http\Handler\Abstracts\HandlerManager;
|
||||
use Http\Message\ServerRequest;
|
||||
use Http\Message\Stream;
|
||||
use Http\Route\MiddlewareManager;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Server\Constrict\RequestInterface;
|
||||
use Server\Constrict\ResponseEmitter;
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\Http\Response;
|
||||
|
||||
class TestRequest
|
||||
{
|
||||
|
||||
|
||||
private ?ResponseEmitter $response = null;
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onRequest(Request $request, Response $response): void
|
||||
{
|
||||
try {
|
||||
[$PsrRequest, $PsrResponse] = $this->initRequestResponse($request);
|
||||
|
||||
/** @var Handler $handler */
|
||||
$handler = HandlerManager::get($request->server['request_uri'], $request->getMethod());
|
||||
if (is_null($handler)) {
|
||||
$PsrResponse->withStatus(404)->withBody(null);
|
||||
} else if (is_integer($handler)) {
|
||||
$PsrResponse->withStatus($handler)->withBody(null);
|
||||
} else {
|
||||
$middlewares = MiddlewareManager::get($handler->callback[0]::class, $handler->callback[1]);
|
||||
|
||||
$stream = new Stream((new Dispatcher($handler, $middlewares))->handle($PsrRequest));
|
||||
|
||||
$PsrResponse->withStatus(200)->withBody($stream);
|
||||
}
|
||||
} catch (\Throwable $throwable) {
|
||||
|
||||
} finally {
|
||||
$this->response->sender($response, $PsrResponse);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return array<ServerRequestInterface, ResponseInterface>
|
||||
* @throws Exception
|
||||
*/
|
||||
private function initRequestResponse(Request $request): array
|
||||
{
|
||||
$PsrResponse = Context::setContext(\Server\Constrict\ResponseInterface::class, new \Http\Message\Response());
|
||||
|
||||
$PsrRequest = Context::setContext(RequestInterface::class, ServerRequest::createServerRequest($request));
|
||||
|
||||
return [$PsrRequest, $PsrResponse];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user