Files
kiri-core/HttpServer/Route/Node.php
T

593 lines
12 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
namespace HttpServer\Route;
2021-03-05 17:35:00 +08:00
use Annotation\Route\Middleware;
2020-09-04 17:51:10 +08:00
use Closure;
2021-02-20 17:33:28 +08:00
use HttpServer\Abstracts\HttpService;
2021-03-05 18:03:18 +08:00
use HttpServer\Controller;
2020-08-31 01:27:08 +08:00
use HttpServer\Http\Request;
use Exception;
2020-12-17 14:12:44 +08:00
2021-03-03 18:35:04 +08:00
use HttpServer\IInterface\After;
use HttpServer\IInterface\Interceptor;
use HttpServer\IInterface\Limits;
2021-03-05 16:17:22 +08:00
use HttpServer\Route\Dispatch\Dispatch;
2021-02-08 17:20:43 +08:00
use JetBrains\PhpStorm\Pure;
2020-11-27 14:19:07 +08:00
use ReflectionException;
2020-12-24 11:12:23 +08:00
use Snowflake\Core\Json;
2021-02-14 21:54:04 +08:00
use Snowflake\Event;
2021-02-22 17:44:24 +08:00
use Snowflake\Exception\ComponentException;
2020-11-27 14:19:07 +08:00
use Snowflake\Exception\NotFindClassException;
2020-08-31 12:38:32 +08:00
use Snowflake\Snowflake;
2020-12-15 19:17:42 +08:00
use Throwable;
use function Input;
2020-08-31 01:27:08 +08:00
/**
* Class Node
2020-08-31 12:38:32 +08:00
* @package Snowflake\Snowflake\Route
2020-08-31 01:27:08 +08:00
*/
2021-02-20 17:33:28 +08:00
class Node extends HttpService
2020-08-31 01:27:08 +08:00
{
2020-10-29 18:17:25 +08:00
public string $path = '';
public int $index = 0;
public string $method = '';
2020-08-31 01:27:08 +08:00
/** @var Node[] $childes */
2020-10-29 18:17:25 +08:00
public array $childes = [];
2020-08-31 01:27:08 +08:00
2020-10-29 18:17:25 +08:00
public array $group = [];
2020-08-31 01:27:08 +08:00
2020-10-29 18:17:25 +08:00
private string $_error = '';
2020-08-31 01:27:08 +08:00
2020-10-29 18:17:25 +08:00
public array $rules = [];
2020-10-30 01:22:50 +08:00
/** @var ?Closure|?array */
2020-11-27 14:19:07 +08:00
public Closure|array|null $handler;
2020-10-29 18:17:25 +08:00
public string $htmlSuffix = '.html';
public bool $enableHtmlSuffix = false;
public array $namespace = [];
public array $middleware = [];
2020-10-30 01:28:45 +08:00
2020-11-27 14:19:07 +08:00
/** @var array|Closure */
public Closure|array $callback = [];
2020-08-31 01:27:08 +08:00
2020-10-29 18:17:25 +08:00
private string $_alias = '';
2020-10-13 12:01:48 +08:00
2020-10-29 18:17:25 +08:00
private array $_interceptors = [];
private array $_after = [];
private array $_limits = [];
2020-08-31 22:33:50 +08:00
2020-08-31 01:27:08 +08:00
/**
* @param $handler
* @return Node
2020-09-04 18:47:22 +08:00
* @throws
2020-08-31 01:27:08 +08:00
*/
2020-12-15 17:12:21 +08:00
public function bindHandler($handler): static
2020-08-31 01:27:08 +08:00
{
2021-03-05 18:05:45 +08:00
if (is_string($handler) && str_contains($handler, '@')) {
2020-10-21 11:11:42 +08:00
list($controller, $action) = explode('@', $handler);
if (!empty($this->namespace)) {
$controller = implode('\\', $this->namespace) . '\\' . $controller;
}
$this->handler = $this->getReflect($controller, $action);
2021-03-05 18:42:14 +08:00
$this->callback = Reduce::reduce($this->createDispatch(), $this->annotation());
2020-10-21 11:11:42 +08:00
} else if ($handler != null && !is_callable($handler, true)) {
$this->_error = 'Controller is con\'t exec.';
2021-03-05 18:05:45 +08:00
} else if ($handler instanceof Closure) {
$this->handler = $handler;
2020-10-21 11:11:42 +08:00
} else {
2021-03-05 17:41:07 +08:00
[$controller, $action] = $this->handler = $handler;
2021-03-05 18:05:45 +08:00
if (!($controller instanceof Controller)) {
return $this;
2021-03-05 18:03:18 +08:00
}
2021-03-05 18:05:45 +08:00
$this->annotationInject(get_class($controller), $action);
$this->callback = Reduce::reduce($this->createDispatch(), $this->annotation());
2021-03-05 16:17:22 +08:00
}
return $this;
}
/**
2021-03-05 16:47:25 +08:00
* @return Closure
2021-03-05 16:17:22 +08:00
*/
2021-03-05 16:47:25 +08:00
public function createDispatch(): Closure
2021-03-05 16:17:22 +08:00
{
2021-03-05 16:47:25 +08:00
return function () {
return Dispatch::create($this->handler, func_get_args())->dispatch();
};
2021-03-05 16:17:22 +08:00
}
/**
* @return array
*/
2021-03-05 17:41:07 +08:00
protected function annotation(): array
2021-03-05 16:17:22 +08:00
{
2021-03-05 17:41:07 +08:00
$middleWares = $this->getMiddleWares();
$middleWares = $this->annotation_limit($this, $middleWares);
$middleWares = $this->annotation_interceptor($this, $middleWares);
2021-03-05 16:17:22 +08:00
return $middleWares;
}
/**
* @param Node $node
* @param $middleWares
* @return array
*/
protected function annotation_interceptor(Node $node, $middleWares = []): array
{
if (!$node->hasInterceptor()) {
return $middleWares;
}
foreach ($node->getInterceptor() as $item) {
$middleWares[] = $item;
}
return $middleWares;
}
/**
* @param Node $node
* @param $middleWares
* @return array
*/
protected function annotation_limit(Node $node, $middleWares = []): array
{
if (!$node->hasLimits()) {
return $middleWares;
}
foreach ($node->getLimits() as $item) {
$middleWares[] = $item;
}
return $middleWares;
2020-08-31 01:27:08 +08:00
}
2020-09-01 00:32:48 +08:00
/**
* @return bool
*/
2021-02-08 17:20:43 +08:00
#[Pure] public function hasInterceptor(): bool
2020-09-01 00:32:48 +08:00
{
return count($this->_interceptors) > 0;
}
2020-09-07 02:15:51 +08:00
/**
* @return bool
*/
2021-02-08 17:20:43 +08:00
#[Pure] public function hasLimits(): bool
2020-09-07 02:15:51 +08:00
{
return count($this->_limits) > 0;
}
2020-09-09 11:44:14 +08:00
/**
2020-12-15 19:17:42 +08:00
* @param null $response
* @return mixed
2020-09-09 11:44:14 +08:00
*/
2020-12-15 19:14:11 +08:00
public function afterDispatch($response = null): mixed
2020-09-09 11:44:14 +08:00
{
2021-02-20 13:25:09 +08:00
return (Reduce::after($this->_after))(\request(), $response);
2020-09-09 11:44:14 +08:00
}
2020-09-01 00:32:48 +08:00
/**
* @return array
*/
2020-12-15 19:14:11 +08:00
public function getInterceptor(): array
2020-09-01 00:32:48 +08:00
{
return $this->_interceptors;
}
2020-09-07 02:15:51 +08:00
2020-09-09 11:44:14 +08:00
/**
* @return array
*/
2020-12-15 19:14:11 +08:00
public function getAfters(): array
2020-09-09 11:44:14 +08:00
{
return $this->_after;
}
/**
* @return bool
*/
2021-02-08 17:20:43 +08:00
#[Pure] public function hasAfter(): bool
2020-09-09 11:44:14 +08:00
{
return count($this->_after) > 0;
}
2020-09-07 02:15:51 +08:00
/**
* @return array
*/
2020-12-15 19:14:11 +08:00
public function getLimits(): array
2020-09-07 02:15:51 +08:00
{
return $this->_limits;
}
2020-08-31 01:27:08 +08:00
/**
* @param $request
* @return bool
*/
2020-12-15 19:14:11 +08:00
public function methodAllow(Request $request): bool
2020-08-31 01:27:08 +08:00
{
if ($this->method == $request->getMethod()) {
return true;
}
return $this->method == 'any';
}
/**
* @return bool
* @throws Exception
*/
2020-12-15 19:14:11 +08:00
public function checkSuffix(): bool
2020-08-31 01:27:08 +08:00
{
if ($this->enableHtmlSuffix) {
$url = request()->getUri();
$nowLength = strlen($this->htmlSuffix);
if (strpos($url, $this->htmlSuffix) !== strlen($url) - $nowLength) {
return false;
}
}
return $this->checkRule();
}
/**
* @return bool
* @throws Exception
*/
2020-12-15 19:14:11 +08:00
private function checkRule(): bool
2020-08-31 01:27:08 +08:00
{
if (empty($this->rules)) {
return true;
}
foreach ($this->rules as $rule) {
if (!isset($rule['class'])) {
$rule['class'] = Filter::class;
}
/** @var Filter $object */
2020-08-31 12:38:32 +08:00
$object = Snowflake::createObject($rule);
2020-08-31 01:27:08 +08:00
if (!$object->handler()) {
return false;
2020-12-15 19:17:42 +08:00
}
2020-08-31 01:27:08 +08:00
}
return true;
}
/**
* @param string $controller
* @param string $action
* @return null|array
* @throws Exception
*/
2020-12-15 18:25:03 +08:00
private function getReflect(string $controller, string $action): ?array
2020-08-31 01:27:08 +08:00
{
try {
2020-08-31 22:33:50 +08:00
$reflect = Snowflake::getDi()->getReflect($controller);
2021-02-22 18:11:23 +08:00
if (empty($reflect)) {
2020-08-31 01:27:08 +08:00
throw new Exception($controller . ' Class is con\'t Instantiable.');
}
if (!empty($action) && !$reflect->hasMethod($action)) {
throw new Exception('method ' . $action . ' not exists at ' . $controller . '.');
}
2021-03-05 16:10:36 +08:00
2021-03-05 18:43:03 +08:00
$this->annotationInject($controller, $action);
2021-03-05 16:10:36 +08:00
2020-08-31 01:27:08 +08:00
return [$reflect->newInstance(), $action];
2020-12-15 19:17:42 +08:00
} catch (Throwable $exception) {
2020-08-31 01:27:08 +08:00
$this->_error = $exception->getMessage();
2021-02-20 15:45:48 +08:00
$this->error($exception, 'router');
2020-08-31 01:27:08 +08:00
return null;
}
}
2020-09-01 00:32:48 +08:00
2021-03-05 16:10:36 +08:00
/**
2021-03-05 17:11:47 +08:00
* @param Closure|array|string $handler
* @throws Exception
*/
public function addInterceptor(Closure|string|array $handler)
{
if (!is_array($handler) || is_object($handler[0])) {
$handler = [$handler];
}
foreach ($handler as $closure) {
if (in_array($closure, $this->_interceptors)) {
continue;
}
$this->_interceptors[] = $closure;
}
}
/**
2021-03-05 16:10:36 +08:00
* @param string $className
* @param string $action
2021-03-05 17:11:47 +08:00
* @return Node
2021-03-05 16:10:36 +08:00
* @throws ComponentException
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
2021-03-05 17:41:07 +08:00
public function annotationInject(string $className, string $action): Node
2021-03-05 16:10:36 +08:00
{
2021-03-05 17:03:15 +08:00
$annotation = annotation()->getMethods($className, $action);
2021-03-05 16:10:36 +08:00
if (empty($annotation)) {
2021-03-05 17:41:07 +08:00
return $this;
2021-03-05 16:10:36 +08:00
}
foreach ($annotation as $name => $attribute) {
if ($attribute instanceof \Annotation\Route\Interceptor) {
2021-03-05 17:41:07 +08:00
$this->addInterceptor($attribute->interceptor);
2021-03-05 16:10:36 +08:00
}
if ($attribute instanceof \Annotation\Route\After) {
2021-03-05 17:41:07 +08:00
$this->addAfter($attribute->after);
2021-03-05 16:10:36 +08:00
}
2021-03-05 17:35:00 +08:00
if ($attribute instanceof Middleware) {
2021-03-05 17:41:07 +08:00
$this->addMiddleware($attribute->middleware);
2021-03-05 16:10:36 +08:00
}
if ($attribute instanceof \Annotation\Route\Limits) {
2021-03-05 17:41:07 +08:00
$this->addLimits($attribute->limits);
2021-03-05 16:10:36 +08:00
}
}
2021-03-05 17:41:07 +08:00
return $this;
2020-09-04 17:51:10 +08:00
}
2020-09-08 11:46:22 +08:00
/**
* @param Closure|array|string $handler
* @throws Exception
*/
2020-11-27 14:19:07 +08:00
public function addAfter(Closure|string|array $handler)
2020-09-08 11:46:22 +08:00
{
2020-12-15 18:50:28 +08:00
if (!is_array($handler) || is_object($handler[0])) {
$handler = [$handler];
}
foreach ($handler as $closure) {
2021-01-12 16:27:49 +08:00
if (in_array($closure, $this->_after)) {
continue;
}
2020-12-15 18:50:28 +08:00
$this->_after[] = $closure;
}
2020-09-08 11:46:22 +08:00
}
2020-09-07 02:15:51 +08:00
/**
* @param Closure|array|string $handler
* @throws Exception
*/
2020-11-27 14:19:07 +08:00
public function addLimits(Closure|string|array $handler)
2020-09-07 02:15:51 +08:00
{
2020-12-15 18:50:28 +08:00
if (!is_array($handler) || is_object($handler[0])) {
$handler = [$handler];
}
foreach ($handler as $closure) {
2021-01-12 16:27:49 +08:00
if (in_array($closure, $this->_limits)) {
continue;
}
2020-12-15 18:50:28 +08:00
$this->_limits[] = $closure;
}
2020-09-07 02:15:51 +08:00
}
2020-08-31 01:27:08 +08:00
/**
* @return string
* 错误信息
*/
2020-12-15 18:50:28 +08:00
public function getError(): string
2020-08-31 01:27:08 +08:00
{
return $this->_error;
}
/**
* @param Node $node
* @param string $field
* @return Node
*/
2020-12-15 18:50:28 +08:00
public function addChild(Node $node, string $field): Node
2020-08-31 01:27:08 +08:00
{
/** @var Node $oLod */
$oLod = $this->childes[$field] ?? null;
if (!empty($oLod)) {
$node = $oLod;
}
$this->childes[$field] = $node;
return $this->childes[$field];
}
/**
* @param $rule
* @return $this
*/
2020-12-15 18:50:28 +08:00
public function filter($rule): static
2020-08-31 01:27:08 +08:00
{
if (empty($rule)) {
return $this;
}
if (!isset($rule[0])) {
$rule = [$rule];
}
foreach ($rule as $value) {
if (empty($value)) {
continue;
}
$this->rules[] = $value;
}
return $this;
}
/**
* @param string $search
2020-12-15 18:50:28 +08:00
* @return Node|null
2020-08-31 01:27:08 +08:00
*/
2020-12-15 18:50:28 +08:00
public function findNode(string $search): ?Node
2020-08-31 01:27:08 +08:00
{
if (empty($this->childes)) {
return null;
}
if (isset($this->childes[$search])) {
return $this->childes[$search];
}
$_searchMatch = '/<(\w+)?:(.+)?>/';
foreach ($this->childes as $key => $val) {
if (preg_match($_searchMatch, $key, $match)) {
2020-12-15 19:17:42 +08:00
Input()->addGetParam($match[1] ?? '--', $search);
2020-08-31 01:27:08 +08:00
return $this->childes[$key];
}
}
return null;
}
/**
* @param string $alias
* @return $this
* 别称
*/
2020-12-15 18:50:28 +08:00
public function alias(string $alias): static
2020-08-31 01:27:08 +08:00
{
2020-10-13 12:01:48 +08:00
$this->_alias = $alias;
2020-08-31 01:27:08 +08:00
return $this;
}
2020-10-13 12:01:48 +08:00
/**
* @return string
*/
2020-12-15 18:50:28 +08:00
public function getAlias(): string
2020-10-13 12:01:48 +08:00
{
return $this->_alias;
}
2020-08-31 01:27:08 +08:00
/**
* @param int $limit
* @param int $duration
* @param bool $isBindConsumer
* @return $this
* @throws Exception
*/
2020-12-15 18:50:28 +08:00
public function limits(int $limit, int $duration = 60, bool $isBindConsumer = false): static
2020-08-31 01:27:08 +08:00
{
2020-09-03 11:39:20 +08:00
$limits = Snowflake::app()->getLimits();
2020-08-31 01:27:08 +08:00
$limits->addLimits($this->path, $limit, $duration, $isBindConsumer);
2020-12-17 10:16:21 +08:00
return $this;
2020-08-31 01:27:08 +08:00
}
2020-10-20 15:38:45 +08:00
2020-09-04 17:51:10 +08:00
/**
2020-11-27 14:19:07 +08:00
* @param array|Closure|string $class
2021-03-03 18:35:04 +08:00
* @return Node
2020-11-27 14:19:07 +08:00
* @throws ReflectionException
* @throws NotFindClassException
2020-09-04 17:51:10 +08:00
* @throws Exception
*/
2021-03-03 18:35:04 +08:00
public function addMiddleware(Closure|string|array $class): static
2020-09-04 17:51:10 +08:00
{
2021-03-04 00:27:14 +08:00
if (empty($class)) return $this;
2020-09-04 18:13:06 +08:00
if (is_string($class)) {
2021-03-03 18:35:04 +08:00
$class = $this->resolve_aop($class);
if ($class === null) {
return $this;
2020-09-04 18:13:06 +08:00
}
}
2021-03-03 19:13:29 +08:00
if (is_array($class)) {
if (isset($class[0]) && is_object($class[0])) {
$class = [$class];
}
} else {
2020-12-15 18:56:23 +08:00
$class = [$class];
}
foreach ($class as $closure) {
2021-03-04 00:35:18 +08:00
if (is_string($closure)) {
$closure = [Snowflake::createObject($closure), 'onHandler'];
}
2021-03-04 00:24:13 +08:00
if (in_array($closure, $this->middleware)) {
continue;
}
2020-12-15 18:56:23 +08:00
$this->middleware[] = $closure;
}
2021-03-03 18:35:04 +08:00
return $this;
}
/**
* @param string $class
* @return array|null
* @throws NotFindClassException
* @throws ReflectionException
*/
private function resolve_aop(string $class): array|null
{
$class = Snowflake::createObject($class);
if ($class instanceof \HttpServer\IInterface\Middleware) {
return [$class, 'onHandler'];
} else if ($class instanceof Interceptor) {
return [$class, 'Interceptor'];
} else if ($class instanceof After) {
return [$class, 'onHandler'];
} else if ($class instanceof Limits) {
return [$class, 'next'];
} else {
return null;
}
2020-09-04 17:51:10 +08:00
}
2020-12-16 16:49:02 +08:00
/**
* @return array
*/
public function getMiddleWares(): array
{
return $this->middleware;
}
2020-09-08 11:53:09 +08:00
/**
* @return mixed
2020-10-20 15:37:21 +08:00
* @throws Exception
2020-09-08 11:53:09 +08:00
*/
2020-12-14 17:42:48 +08:00
public function dispatch(): mixed
2020-09-08 11:53:09 +08:00
{
2021-03-05 16:22:30 +08:00
if (empty($this->callback)) {
2021-03-03 18:35:04 +08:00
return Json::to(404, $this->errorMsg());
2020-09-08 13:58:22 +08:00
}
2021-03-03 18:35:04 +08:00
return $this->runWith(...func_get_args());
}
2021-02-23 14:26:10 +08:00
2021-03-03 18:35:04 +08:00
/**
* @return mixed
* @throws Exception
*/
private function runWith(): mixed
{
2020-12-15 17:26:07 +08:00
$requestParams = func_get_args();
2020-12-16 15:33:08 +08:00
if (func_num_args() > 0) {
return call_user_func($this->callback, ...$requestParams);
} else {
return call_user_func($this->callback, \request());
2020-12-15 17:26:07 +08:00
}
2020-09-08 11:53:09 +08:00
}
2020-08-31 01:27:08 +08:00
/**
2021-03-03 18:35:04 +08:00
* @return string
2020-08-31 01:27:08 +08:00
*/
2021-03-03 18:35:04 +08:00
private function errorMsg(): string
2020-08-31 01:27:08 +08:00
{
2021-03-03 18:35:04 +08:00
return $this->_error ?? 'Page not found.';
2020-08-31 01:27:08 +08:00
}
2021-03-03 18:35:04 +08:00
2020-08-31 01:27:08 +08:00
}