This commit is contained in:
as2252258@163.com
2021-09-05 02:51:53 +08:00
parent f19f0c0e81
commit 8735203573
+347 -338
View File
@@ -26,399 +26,408 @@ use Server\RequestInterface;
class Node class Node
{ {
public string $path = ''; public string $path = '';
public int $index = 0; public int $index = 0;
/** @var string[] */ /** @var string[] */
public array $method = []; public array $method = [];
/** @var Node[] $childes */ /** @var Node[] $childes */
public array $childes = []; public array $childes = [];
public array $group = []; public array $group = [];
private string $_error = ''; private string $_error = '';
private string $_dataType = ''; private string $_dataType = '';
/** @var array<string, array|Closure|null> */ /** @var array<string, array|Closure|null> */
private array $_handler = []; private array $_handler = [];
public string $htmlSuffix = '.html'; public string $htmlSuffix = '.html';
public bool $enableHtmlSuffix = false; public bool $enableHtmlSuffix = false;
/** @var array<string,mixed> */ /** @var array<string,mixed> */
public array $namespace = []; public array $namespace = [];
/** @var array<string,mixed> */ /** @var array<string,mixed> */
public array $middleware = []; public array $middleware = [];
public string $sourcePath = ''; public string $sourcePath = '';
/** @var array|Closure */ /** @var array|Closure */
public Closure|array $callback = []; public Closure|array $callback = [];
private string $_alias = ''; private string $_alias = '';
/** /**
* @param string $dataType * @param string $dataType
*/ */
public function setDataType(string $dataType) public function setDataType(string $dataType)
{ {
$this->_dataType = $dataType; $this->_dataType = $dataType;
} }
/** /**
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
*/ */
public function __construct(public Router $router) public function __construct(public Router $router)
{ {
$eventDispatcher = di(EventProvider::class); $eventDispatcher = di(EventProvider::class);
$eventDispatcher->on(OnAfterWorkerStart::class, [$this, 'setParameters']); $eventDispatcher->on(OnAfterWorkerStart::class, [$this, 'setParameters']);
} }
/** /**
* @param string $data * @param string $data
* @return mixed * @return mixed
*/ */
public function unpack(string $data): mixed public function unpack(string $data): mixed
{ {
if ($this->_dataType == 'json') { if ($this->_dataType == 'json') {
return json_decode($data, true); return json_decode($data, true);
} }
if ($this->_dataType == 'serializes') { if ($this->_dataType == 'serializes') {
return unserialize($data); return unserialize($data);
} }
return $data; return $data;
} }
/** /**
* @param string|array|Closure $handler * @param string|array|Closure $handler
* @param string $method * @param string $method
* @param string $path * @param string $path
* @return Node * @return Node
* @throws ReflectionException * @throws ReflectionException
*/ */
public function setHandler(string|array|Closure $handler, string $method, string $path): static public function setHandler(string|array|Closure $handler, string $method, string $path): static
{ {
$this->sourcePath = '/' . ltrim($path, '/'); $this->sourcePath = '/' . ltrim($path, '/');
if (is_string($handler) && str_contains($handler, '@')) { if (is_string($handler) && str_contains($handler, '@')) {
$handler = $this->splitHandler($handler); $handler = $this->splitHandler($handler);
} else if ($handler != null && !is_callable($handler, true)) { } else if ($handler != null && !is_callable($handler, true)) {
$this->_error = 'Controller is con\'t exec.'; $this->_error = 'Controller is con\'t exec.';
} return $this;
$this->_handler[$method] = $handler; }
return $this; $this->_handler[$method] = [$handler, $this->resolveMethodParams($handler)];
} return $this;
}
/** /**
* @param string $handler * @param $dispatcher
* @return array * @return array
* @throws ReflectionException * @throws \ReflectionException
*/ */
private function splitHandler(string $handler): array private function resolveMethodParams($dispatcher): array
{ {
list($controller, $action) = explode('@', $handler); $container = Kiri::getDi();
if (!class_exists($controller) && !empty($this->namespace)) { if ($dispatcher instanceof Closure) {
$controller = implode('\\', $this->namespace) . '\\' . $controller; $_injectParameters = $container->getFunctionParameters($dispatcher);
} } else {
return [Kiri::getDi()->get($controller), $action]; $_injectParameters = $container->getMethodParameters(
} $dispatcher[0]::class, $dispatcher[1]);
}
return $_injectParameters;
}
/** /**
* @param string $method * @param string $handler
* @param $handler * @return array
* @param $_injectParameters * @throws ReflectionException
* @throws NotFindClassException */
* @throws ReflectionException private function splitHandler(string $handler): array
* @throws Exception {
*/ list($controller, $action) = explode('@', $handler);
private function injectMiddleware(string $method, $handler, $_injectParameters): void if (!class_exists($controller) && !empty($this->namespace)) {
{ $controller = implode('\\', $this->namespace) . '\\' . $controller;
if (!($handler instanceof Closure)) { }
$callback = $this->injectControllerMiddleware($method, $handler, $_injectParameters); return [Kiri::getDi()->get($controller), $action];
} else { }
$callback = $this->injectClosureMiddleware($method, $handler, $_injectParameters);
}
HandlerProviders::add($method, $this->sourcePath, $callback);
}
/** /**
* @param $method * @param string $method
* @param $handler * @param $handler
* @param $_injectParameters * @param $_injectParameters
* @return mixed * @throws NotFindClassException
* @throws NotFindClassException * @throws ReflectionException
* @throws ReflectionException * @throws Exception
* @throws Exception */
*/ private function injectMiddleware(string $method, $handler, $_injectParameters): void
private function injectControllerMiddleware($method, $handler, $_injectParameters): mixed {
{ if (!($handler instanceof Closure)) {
$middleware = $this->middleware[$method] ?? []; $callback = $this->injectControllerMiddleware($method, $handler, $_injectParameters);
} else {
$allowMiddleware = $this->router->getMiddleware(); $callback = $this->injectClosureMiddleware($method, $handler, $_injectParameters);
if (!empty($allowMiddleware)){ }
array_unshift($middleware, $allowMiddleware); HandlerProviders::add($method, $this->sourcePath, $callback);
} }
MiddlewareManager::addMiddlewares($handler[0], $handler[1], $middleware);
return MiddlewareManager::callerMiddlewares(
$handler[0], $handler[1], $this->aopHandler($this->getAop($handler), $handler, $_injectParameters)
);
}
/** /**
* @param $method * @param $method
* @param $handler * @param $handler
* @param $_injectParameters * @param $_injectParameters
* @return Closure * @return mixed
* @throws Exception * @throws NotFindClassException
*/ * @throws ReflectionException
private function injectClosureMiddleware($method, $handler, $_injectParameters): Closure * @throws Exception
{ */
$middleware = $this->middleware[$method] ?? []; private function injectControllerMiddleware($method, $handler, $_injectParameters): mixed
{
$middleware = $this->middleware[$method] ?? [];
$allowMiddleware = $this->router->getMiddleware(); $allowMiddleware = $this->router->getMiddleware();
if (!empty($allowMiddleware)){ if (!empty($allowMiddleware)) {
array_unshift($middleware, $allowMiddleware); array_unshift($middleware, $allowMiddleware);
} }
if (!empty($middleware)) { MiddlewareManager::addMiddlewares($handler[0], $handler[1], $middleware);
return MiddlewareManager::closureMiddlewares($middleware, return MiddlewareManager::callerMiddlewares(
$this->normalHandler($handler, $_injectParameters) $handler[0], $handler[1], $this->aopHandler($this->getAop($handler), $handler, $_injectParameters)
); );
} else { }
return $this->normalHandler($handler, $_injectParameters);
}
}
/** /**
* @throws ReflectionException * @param $method
* @throws NotFindClassException * @param $handler
*/ * @param $_injectParameters
public function setParameters(): static * @return Closure
{ * @throws Exception
$container = Kiri::getDi(); */
if (empty($this->_handler)) { private function injectClosureMiddleware($method, $handler, $_injectParameters): Closure
return $this; {
} $middleware = $this->middleware[$method] ?? [];
foreach ($this->_handler as $method => $dispatcher) {
if ($dispatcher instanceof Closure) { $allowMiddleware = $this->router->getMiddleware();
$_injectParameters = $container->getFunctionParameters($dispatcher); if (!empty($allowMiddleware)) {
} else { array_unshift($middleware, $allowMiddleware);
[$controller, $action] = $dispatcher; }
if (is_object($controller)) { if (!empty($middleware)) {
$controller = get_class($controller); return MiddlewareManager::closureMiddlewares($middleware,
} $this->normalHandler($handler, $_injectParameters)
$_injectParameters = $container->getMethodParameters($controller, $action); );
} } else {
$this->injectMiddleware($method, $dispatcher, $_injectParameters); return $this->normalHandler($handler, $_injectParameters);
} }
$this->_handler = []; }
return $this;
}
/** /**
* @param IAspect|null $reflect * @throws ReflectionException
* @param $handler * @throws NotFindClassException
* @param $_injectParameters */
* @return Closure public function setParameters(): static
*/ {
#[Pure] private function aopHandler(?IAspect $reflect, $handler, $_injectParameters): Closure if (empty($this->_handler)) {
{ return $this;
if (is_null($reflect)) { }
return $this->normalHandler($handler, $_injectParameters); foreach ($this->_handler as $method => $dispatcher) {
} $this->injectMiddleware($method, ...$dispatcher);
return static function () use ($reflect, $handler, $_injectParameters) { }
return $reflect->invoke($handler, $_injectParameters); $this->_handler = [];
}; return $this;
} }
/** /**
* @throws ReflectionException|NotFindClassException * @param IAspect|null $reflect
*/ * @param $handler
private function getAop($handler): ?IAspect * @param $_injectParameters
{ * @return Closure
[$controller, $action] = $handler; */
#[Pure] private function aopHandler(?IAspect $reflect, $handler, $_injectParameters): Closure
if (is_object($controller)) { {
$controller = get_class($controller); if (is_null($reflect)) {
} return $this->normalHandler($handler, $_injectParameters);
}
/** @var Aspect $aspect */ return static function () use ($reflect, $handler, $_injectParameters) {
$aspect = NoteManager::getSpecify_annotation(Aspect::class, $controller, $action); return $reflect->invoke($handler, $_injectParameters);
if (empty($aspect)) { };
return null; }
}
return di($aspect->aspect);
}
/** /**
* @param $handler * @throws ReflectionException|NotFindClassException
* @param $_injectParameters */
* @return Closure private function getAop($handler): ?IAspect
*/ {
private function normalHandler($handler, $_injectParameters): Closure [$controller, $action] = $handler;
{
return static function () use ($handler, $_injectParameters) { if (is_object($controller)) {
return call_user_func($handler, ...$_injectParameters); $controller = get_class($controller);
}; }
}
/** @var Aspect $aspect */
$aspect = NoteManager::getSpecify_annotation(Aspect::class, $controller, $action);
if (empty($aspect)) {
return null;
}
return di($aspect->aspect);
}
/** /**
* @return array * @param $handler
*/ * @param $_injectParameters
#[Pure] protected function annotation(): array * @return Closure
{ */
return $this->getMiddleWares(); private function normalHandler($handler, $_injectParameters): Closure
} {
return static function () use ($handler, $_injectParameters) {
return call_user_func($handler, ...$_injectParameters);
};
}
/** /**
* @param RequestInterface $request * @return array
* @return bool */
*/ #[Pure] protected function annotation(): array
#[Pure] public function methodAllow(RequestInterface $request): bool {
{ return $this->getMiddleWares();
if (!in_array($request->getMethod(), $this->method)) { }
return true;
}
return $this->method == 'any';
}
/**
* @return bool
* @throws Exception
*/
public function checkSuffix(): bool
{
if ($this->enableHtmlSuffix) {
$url = request()->getUri()->getPath();
$nowLength = strlen($this->htmlSuffix);
if (strpos($url, $this->htmlSuffix) !== strlen($url) - $nowLength) {
return false;
}
}
return true;
}
/** /**
* @return string * @param RequestInterface $request
* 错误信息 * @return bool
*/ */
public function getError(): string #[Pure] public function methodAllow(RequestInterface $request): bool
{ {
return $this->_error; if (!in_array($request->getMethod(), $this->method)) {
} return true;
}
return $this->method == 'any';
}
/** /**
* @param Node $node * @return bool
* @return Node * @throws Exception
*/ */
public function addChild(Node $node): Node public function checkSuffix(): bool
{ {
$this->childes[] = $node; if ($this->enableHtmlSuffix) {
return $node; $url = request()->getUri()->getPath();
} $nowLength = strlen($this->htmlSuffix);
if (strpos($url, $this->htmlSuffix) !== strlen($url) - $nowLength) {
return false;
}
}
return true;
}
/** /**
* @param string $search * @return string
* @return Node|null * 错误信息
* @throws Exception */
*/ public function getError(): string
public function findNode(string $search): ?Node {
{ return $this->_error;
if (empty($this->childes)) { }
return null;
} /**
foreach ($this->childes as $val) { * @param Node $node
if ($search == $val->path) { * @return Node
return $val; */
} public function addChild(Node $node): Node
} {
return null; $this->childes[] = $node;
} return $node;
}
/** /**
* @param string $alias * @param string $search
* @return $this * @return Node|null
* 别称 * @throws Exception
*/ */
public function alias(string $alias): static public function findNode(string $search): ?Node
{ {
$this->_alias = $alias; if (empty($this->childes)) {
return $this; return null;
} }
foreach ($this->childes as $val) {
if ($search == $val->path) {
return $val;
}
}
return null;
}
/** /**
* @return string * @param string $alias
*/ * @return $this
public function getAlias(): string * 别称
{ */
return $this->_alias; public function alias(string $alias): static
} {
$this->_alias = $alias;
return $this;
}
/** /**
* @param $method * @return string
* @param Closure|array $class */
* @return $this public function getAlias(): string
*/ {
public function addMiddleware($method, Closure|array $class): static return $this->_alias;
{ }
if (empty($class)) return $this;
if (!isset($this->middleware[$method])) {
$this->middleware[$method] = [];
}
foreach ($class as $closure) {
if (in_array($closure, $this->middleware[$method])) {
continue;
}
$this->middleware[$method][] = $closure;
}
return $this;
}
/** /**
* @return array * @param $method
*/ * @param Closure|array $class
public function getMiddleWares(): array * @return $this
{ */
return $this->middleware; public function addMiddleware($method, Closure|array $class): static
} {
if (empty($class)) return $this;
if (!isset($this->middleware[$method])) {
$this->middleware[$method] = [];
}
foreach ($class as $closure) {
if (in_array($closure, $this->middleware[$method])) {
continue;
}
$this->middleware[$method][] = $closure;
}
return $this;
}
/** /**
* @return mixed * @return array
* @throws Exception */
*/ public function getMiddleWares(): array
public function dispatch(): mixed {
{ return $this->middleware;
if (!in_array(request()->getMethod(), $this->method)) { }
throw new RequestException('<h2>HTTP 405 Method allow</h2><hr><i>Powered by Swoole</i>', 405);
}
$handlerProviders = HandlerProviders::get($this->sourcePath, request()->getMethod()); /**
if (empty($handlerProviders)) { * @return mixed
throw new RequestException('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404); * @throws Exception
} */
return call_user_func($handlerProviders, request()); public function dispatch(): mixed
} {
if (!in_array(request()->getMethod(), $this->method)) {
throw new RequestException('<h2>HTTP 405 Method allow</h2><hr><i>Powered by Swoole</i>', 405);
}
$handlerProviders = HandlerProviders::get($this->sourcePath, request()->getMethod());
if (empty($handlerProviders)) {
throw new RequestException('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404);
}
return call_user_func($handlerProviders, request());
}
} }