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

358 lines
6.9 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-04-07 11:42:43 +08:00
use Annotation\Route\RpcProducer;
2020-09-04 17:51:10 +08:00
use Closure;
2021-04-07 11:42:43 +08:00
use Exception;
2021-02-20 17:33:28 +08:00
use HttpServer\Abstracts\HttpService;
2020-08-31 01:27:08 +08:00
use HttpServer\Http\Request;
2021-02-08 17:20:43 +08:00
use JetBrains\PhpStorm\Pure;
2021-07-12 18:51:41 +08:00
use ReflectionException;
use Snowflake\Aop;
2020-12-24 11:12:23 +08:00
use Snowflake\Core\Json;
2021-07-12 18:51:41 +08:00
use Snowflake\Exception\NotFindClassException;
2021-08-02 18:12:32 +08:00
use Snowflake\IAspect;
2020-08-31 12:38:32 +08:00
use Snowflake\Snowflake;
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
{
2021-07-27 14:15:08 +08:00
public string $path = '';
public int $index = 0;
public string $method = '';
/** @var Node[] $childes */
public array $childes = [];
public array $group = [];
private string $_error = '';
private string $_dataType = '';
/** @var ?Closure|?array */
public Closure|array|null $handler;
public string $htmlSuffix = '.html';
public bool $enableHtmlSuffix = false;
public array $namespace = [];
public array $middleware = [];
/** @var array|Closure */
public Closure|array $callback = [];
private string $_alias = '';
/**
* @param string $dataType
*/
public function setDataType(string $dataType)
{
$this->_dataType = $dataType;
}
/**
* @param string $data
* @return mixed
*/
public function unpack(string $data): mixed
{
if ($this->_dataType == RpcProducer::PROTOCOL_JSON) {
return json_decode($data, true);
}
if ($this->_dataType == RpcProducer::PROTOCOL_SERIALIZE) {
return unserialize($data);
}
return $data;
}
/**
* @param $handler
* @return Node
* @throws
*/
public function bindHandler($handler): static
{
if (is_string($handler) && str_contains($handler, '@')) {
2021-08-02 18:29:42 +08:00
$this->handler = $this->splitHandler($handler);
2021-07-27 14:15:08 +08:00
} else if ($handler != null && !is_callable($handler, true)) {
$this->_error = 'Controller is con\'t exec.';
} else {
$this->handler = $handler;
}
2021-08-02 18:20:02 +08:00
$this->setParameters($this->handler);
2021-07-27 16:08:16 +08:00
return $this->injectMiddleware();
}
2021-08-02 18:29:42 +08:00
/**
* @param string $handler
* @return array
* @throws NotFindClassException
* @throws ReflectionException
*/
private function splitHandler(string $handler): array
{
list($controller, $action) = explode('@', $handler);
if (!class_exists($controller) && !empty($this->namespace)) {
$controller = implode('\\', $this->namespace) . '\\' . $controller;
}
return [Snowflake::getDi()->get($controller), $action];
}
2021-07-27 16:08:16 +08:00
/**
* @throws NotFindClassException
* @throws ReflectionException
*/
private function injectMiddleware(): static
{
$manager = di(MiddlewareManager::class);
if ($this->handler instanceof Closure) {
if (!empty($this->middleware)) {
$this->callback = $manager->closureMiddlewares($this->middleware, $this->createDispatch());
} else {
$this->callback = $this->createDispatch();
}
} else {
$manager->addMiddlewares($this->handler[0], $this->handler[1], $this->middleware);
$this->callback = $manager->callerMiddlewares(
2021-07-27 14:15:08 +08:00
$this->handler[0], $this->handler[1], $this->createDispatch()
);
}
return $this;
}
2021-08-02 18:12:32 +08:00
private array $_injectParameters = [];
2021-07-27 14:15:08 +08:00
/**
* @throws ReflectionException
* @throws NotFindClassException
2021-08-02 18:12:32 +08:00
*/
2021-08-02 18:20:02 +08:00
public function setParameters($handler)
2021-08-02 18:12:32 +08:00
{
$container = Snowflake::getDi();
if ($handler instanceof Closure) {
$this->_injectParameters = $container->resolveFunctionParameters($handler);
} else {
[$controller, $action] = $this->handler;
if (is_object($controller)) {
$controller = get_class($controller);
}
$this->_injectParameters = $container->getMethodParameters($controller, $action);
}
}
/**
* @throws ReflectionException
2021-07-27 14:15:08 +08:00
* @throws Exception
*/
2021-08-01 19:04:34 +08:00
private function createDispatch(): Closure
2021-07-27 14:15:08 +08:00
{
/** @var Aop $aop */
2021-08-02 18:44:51 +08:00
$aop = Snowflake::getDi()->get(Aop::class);
2021-07-27 14:15:08 +08:00
if ($this->handler instanceof Closure || !$aop->hasAop($this->handler)) {
2021-07-28 01:33:45 +08:00
return $this->normalHandler($this->handler);
2021-07-27 14:19:56 +08:00
} else {
2021-08-02 18:12:32 +08:00
return $this->aopHandler($aop->getAop($this->handler));
2021-07-27 14:15:08 +08:00
}
2021-07-27 14:19:56 +08:00
}
2021-07-27 14:15:08 +08:00
2021-07-27 14:19:56 +08:00
/**
2021-08-02 18:12:32 +08:00
* @param IAspect $reflect
2021-07-27 14:19:56 +08:00
* @return Closure
*/
2021-08-02 18:12:32 +08:00
private function aopHandler(IAspect $reflect): Closure
2021-07-27 14:19:56 +08:00
{
2021-08-02 18:12:32 +08:00
$params = $this->_injectParameters;
$handler = $this->handler;
return static function () use ($reflect, $handler, $params) {
return $reflect->invoke($handler, $params);
2021-07-27 14:19:56 +08:00
};
}
/**
2021-07-27 19:38:36 +08:00
* @param $handler
2021-07-27 14:19:56 +08:00
* @return Closure
*/
2021-07-28 01:33:45 +08:00
private function normalHandler($handler): Closure
2021-07-27 14:19:56 +08:00
{
2021-08-02 18:12:32 +08:00
$params = $this->_injectParameters;
return static function () use ($handler, $params) {
return call_user_func($handler, ...$params);
2021-07-27 14:19:56 +08:00
};
2021-07-27 14:15:08 +08:00
}
/**
* @return array
*/
#[Pure] protected function annotation(): array
{
return $this->getMiddleWares();
}
/**
* @param Request $request
* @return bool
*/
public function methodAllow(Request $request): bool
{
if ($this->method == $request->getMethod()) {
return true;
}
return $this->method == 'any';
}
/**
* @return bool
* @throws Exception
*/
public function checkSuffix(): bool
{
if ($this->enableHtmlSuffix) {
$url = request()->getUri();
$nowLength = strlen($this->htmlSuffix);
if (strpos($url, $this->htmlSuffix) !== strlen($url) - $nowLength) {
return false;
}
}
return true;
}
/**
* @return string
* 错误信息
*/
public function getError(): string
{
return $this->_error;
}
/**
* @param Node $node
* @param string $field
* @return Node
*/
public function addChild(Node $node, string $field): Node
{
$field = (string)$field;
/** @var Node $oLod */
$oLod = $this->childes[$field] ?? null;
if (!empty($oLod)) {
$node = $oLod;
}
$this->childes[$field] = $node;
return $this->childes[$field];
}
/**
* @param string $search
* @return Node|null
* @throws Exception
*/
public function findNode(string $search): ?Node
{
if (empty($this->childes)) {
return null;
}
if (isset($this->childes[$search])) {
return $this->childes[$search];
}
foreach ($this->childes as $key => $val) {
if ($search == $key) {
return $this->childes[$key];
}
}
return null;
}
/**
* @param string $alias
* @return $this
* 别称
*/
public function alias(string $alias): static
{
$this->_alias = $alias;
return $this;
}
/**
* @return string
*/
public function getAlias(): string
{
return $this->_alias;
}
/**
* @param Closure|array $class
* @return $this
*/
public function addMiddleware(Closure|array $class): static
{
if (empty($class)) return $this;
foreach ($class as $closure) {
if (in_array($closure, $this->middleware)) {
continue;
}
$this->middleware[] = $closure;
}
return $this;
}
/**
* @return array
*/
public function getMiddleWares(): array
{
return $this->middleware;
}
/**
* @return mixed
* @throws Exception
*/
public function dispatch(): mixed
{
if (empty($this->callback)) {
return Json::to(404, $this->errorMsg());
}
return call_user_func($this->callback, \request());
}
/**
* @return string
*/
private function errorMsg(): string
{
return $this->_error ?? 'Page not found.';
}
2021-03-03 18:35:04 +08:00
2020-08-31 01:27:08 +08:00
}