Files
kiri-core/http-helper/Route/Router.php
T

555 lines
10 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
2021-08-17 16:43:50 +08:00
namespace Http\Route;
2020-08-31 01:27:08 +08:00
2021-08-27 17:06:17 +08:00
use Annotation\Inject;
2020-08-31 01:27:08 +08:00
use Closure;
use Exception;
2021-08-17 16:43:50 +08:00
use Http\Abstracts\HttpService;
2021-08-17 16:52:50 +08:00
use Http\Context\Response;
2021-08-25 11:07:46 +08:00
use Http\Controller;
2021-08-19 10:57:45 +08:00
use Http\IInterface\MiddlewareInterface;
2021-08-17 16:43:50 +08:00
use Http\IInterface\RouterInterface;
2021-03-03 18:35:04 +08:00
use JetBrains\PhpStorm\Pure;
2021-08-11 01:04:57 +08:00
use Kiri\Abstracts\Config;
use Kiri\Exception\ConfigException;
use Kiri\Exception\NotFindClassException;
use Kiri\Kiri;
2021-08-25 11:07:46 +08:00
use ReflectionException;
use Server\RequestInterface;
2021-08-30 19:02:28 +08:00
use Throwable;
2020-08-31 01:27:08 +08:00
2020-10-13 12:11:17 +08:00
defined('ROUTER_TREE') or define('ROUTER_TREE', 1);
defined('ROUTER_HASH') or define('ROUTER_HASH', 2);
2020-08-31 01:27:08 +08:00
/**
* Class Router
2021-08-11 01:04:57 +08:00
* @package Kiri\Kiri\Route
2020-08-31 01:27:08 +08:00
*/
2021-02-20 17:33:28 +08:00
class Router extends HttpService implements RouterInterface
2020-08-31 01:27:08 +08:00
{
2021-08-02 16:38:50 +08:00
/** @var Node[] $nodes */
2021-08-05 11:45:14 +08:00
public array $nodes = [];
2021-08-02 16:38:50 +08:00
public array $groupTacks = [];
public ?string $dir = 'App\\Http\\Controllers';
const NOT_FOUND = 'Page not found or method not allowed.';
/** @var string[] */
public array $methods = ['GET', 'POST', 'OPTIONS', 'PUT', 'DELETE', 'RECEIVE', 'HEAD'];
public ?Closure $middleware = null;
public int $useTree = ROUTER_TREE;
public ?Response $response = null;
2021-08-27 17:06:17 +08:00
/**
* @var RequestInterface
*/
#[Inject(RequestInterface::class)]
public RequestInterface $request;
2021-08-02 16:38:50 +08:00
/**
* @param Closure $middleware
*/
public function setMiddleware(\Closure $middleware): void
{
$this->middleware = $middleware;
}
/**
* @throws ConfigException
* @throws Exception
* 初始化函数路径
*/
public function init()
{
$this->dir = Config::get('http.namespace', $this->dir);
2021-08-11 01:04:57 +08:00
$this->response = Kiri::app()->get('response');
2021-08-02 16:38:50 +08:00
}
/**
* @param bool $useTree
*/
public function setUseTree(bool $useTree): void
{
$this->useTree = $useTree ? ROUTER_TREE : ROUTER_HASH;
}
/**
* @param $path
* @param $handler
* @param string $method
* @return ?Node
* @throws Exception
*/
public function addRoute($path, $handler, string $method = 'any'): ?Node
{
if ($handler instanceof Closure) {
$handler = Closure::bind($handler, di(Controller::class));
}
2021-08-05 11:19:12 +08:00
return $this->tree($path, $handler, $method);
2021-08-02 16:38:50 +08:00
}
/**
* @param $path
* @return string
*/
#[Pure] private function resolve($path): string
{
$paths = array_column($this->groupTacks, 'prefix');
if (empty($paths)) {
return '/' . ltrim($path, '/');
}
$paths = '/' . implode('/', $paths);
if ($path != '/') {
return $paths . '/' . ltrim($path, '/');
}
return $paths . '/';
}
/**
* @param $path
* @param $handler
* @param string $method
* @return Node
* @throws Exception
*/
private function tree($path, $handler, string $method = 'any'): Node
{
2021-08-05 13:54:33 +08:00
$explode = $this->split($path);
2021-08-30 17:20:22 +08:00
$start = array_shift($explode);
$parent = $this->nodes[$start] ?? null;
if (is_null($parent)) {
$parent = $this->nodes[$start] = $this->NodeInstance($start, 0, $method);
2021-08-02 16:38:50 +08:00
}
2021-08-30 17:27:39 +08:00
if (!empty($explode)) {
$parent = $this->bindNode($parent, $explode, $method);
2021-08-02 16:38:50 +08:00
}
2021-08-30 17:27:39 +08:00
if (!in_array($method, $parent->method)) {
$parent->method[] = $method;
}
return $parent->setHandler($handler, $method, $path);
2021-08-02 16:38:50 +08:00
}
/**
* @param Node $parent
* @param array $explode
* @param $method
* @return Node
* @throws Exception
*/
private function bindNode(Node $parent, array $explode, $method): Node
{
$a = 0;
foreach ($explode as $value) {
++$a;
$search = $parent->findNode($value);
if ($search === null) {
2021-08-05 13:47:47 +08:00
$parent = $parent->addChild($this->NodeInstance($value, $a, $method));
2021-08-02 16:38:50 +08:00
} else {
$parent = $search;
}
}
return $parent;
}
/**
* @param $route
* @param $handler
2021-08-30 19:02:28 +08:00
* @return void
2021-08-02 16:38:50 +08:00
* @throws
*/
2021-08-30 19:02:28 +08:00
public function socket($route, $handler): void
2021-08-02 16:38:50 +08:00
{
2021-08-30 19:02:28 +08:00
$this->addRoute($route, $handler, 'SOCKET');
2021-08-02 16:38:50 +08:00
}
/**
* @param $route
* @param $handler
2021-08-30 19:02:28 +08:00
* @return void
2021-08-02 16:38:50 +08:00
* @throws
*/
2021-08-30 19:02:28 +08:00
public function post($route, $handler): void
2021-08-02 16:38:50 +08:00
{
2021-08-30 19:02:28 +08:00
$this->addRoute($route, $handler, 'POST');
2021-08-02 16:38:50 +08:00
}
/**
* @param $route
* @param $handler
2021-08-30 19:02:28 +08:00
* @return void
2021-08-02 16:38:50 +08:00
* @throws
*/
2021-08-30 19:02:28 +08:00
public function get($route, $handler): void
2021-08-02 16:38:50 +08:00
{
2021-08-30 19:02:28 +08:00
$this->addRoute($route, $handler, 'GET');
2021-08-02 16:38:50 +08:00
}
/**
* @param $route
* @param $handler
2021-08-30 19:02:28 +08:00
* @return void
2021-08-02 16:38:50 +08:00
* @throws
*/
2021-08-30 19:02:28 +08:00
public function options($route, $handler): void
2021-08-02 16:38:50 +08:00
{
2021-08-30 19:02:28 +08:00
$this->addRoute($route, $handler, 'OPTIONS');
2021-08-02 16:38:50 +08:00
}
/**
* @param $route
* @param $handler
* @throws
*/
2021-08-30 19:02:28 +08:00
public function any($route, $handler): void
2021-08-02 16:38:50 +08:00
{
2021-08-03 11:55:12 +08:00
foreach ($this->methods as $method) {
2021-08-30 19:02:28 +08:00
$this->addRoute($route, $handler, $method);
2021-08-02 16:38:50 +08:00
}
}
/**
* @param $route
* @param $handler
2021-08-30 19:02:28 +08:00
* @return void
2021-08-02 16:38:50 +08:00
* @throws
*/
2021-08-30 19:02:28 +08:00
public function delete($route, $handler): void
2021-08-02 16:38:50 +08:00
{
2021-08-30 19:02:28 +08:00
$this->addRoute($route, $handler, 'DELETE');
2021-08-02 16:38:50 +08:00
}
/**
* @param $route
* @param $handler
2021-08-30 19:02:28 +08:00
* @return void
2021-08-02 16:38:50 +08:00
* @throws Exception
*/
2021-08-30 19:02:28 +08:00
public function head($route, $handler): void
2021-08-02 16:38:50 +08:00
{
2021-08-30 19:02:28 +08:00
$this->addRoute($route, $handler, 'HEAD');
2021-08-02 16:38:50 +08:00
}
/**
* @param $route
* @param $handler
2021-08-30 19:02:28 +08:00
* @return void
2021-08-02 16:38:50 +08:00
* @throws
*/
2021-08-30 19:02:28 +08:00
public function put($route, $handler): void
2021-08-02 16:38:50 +08:00
{
2021-08-30 19:02:28 +08:00
$this->addRoute($route, $handler, 'PUT');
2021-08-02 16:38:50 +08:00
}
/**
* @param $value
* @param int $index
* @param string $method
* @return Node
* @throws
*/
public function NodeInstance($value, int $index = 0, string $method = 'GET'): Node
{
2021-08-30 19:02:28 +08:00
$node = new Node($this);
2021-08-02 16:38:50 +08:00
$node->childes = [];
$node->path = $value;
$node->index = $index;
2021-08-30 17:09:32 +08:00
$node->method[] = $method;
2021-08-30 19:02:28 +08:00
$node->namespace = $this->loadNamespace();
2021-08-02 16:38:50 +08:00
$name = array_column($this->groupTacks, 'middleware');
if (is_array($name)) {
2021-08-30 18:17:37 +08:00
$node->addMiddleware($method, $this->resolve_middleware($name));
2021-08-02 16:38:50 +08:00
}
return $node;
}
2021-08-30 18:44:24 +08:00
/**
* @return Closure
*/
public function getMiddleware(): Closure
{
return $this->middleware;
}
2021-08-02 16:38:50 +08:00
/**
* @param string|array $middleware
* @return array
* @throws NotFindClassException
* @throws ReflectionException
*/
private function resolve_middleware(string|array $middleware): array
{
if (is_string($middleware)) {
$middleware = [$middleware];
}
$array = [];
foreach ($middleware as $value) {
if (is_array($value)) {
foreach ($value as $item) {
$array[] = $this->getMiddlewareInstance($item);
}
} else {
$array[] = $this->getMiddlewareInstance($value);
}
}
return array_filter($array);
}
/**
* @param $value
* @return Closure|array|null
* @throws NotFindClassException
* @throws ReflectionException
*/
private function getMiddlewareInstance($value): null|Closure|array
{
if (!is_string($value)) {
return $value;
}
2021-08-11 01:04:57 +08:00
$value = Kiri::createObject($value);
2021-08-19 10:57:45 +08:00
if (!($value instanceof MiddlewareInterface)) {
2021-08-02 16:38:50 +08:00
return null;
}
return [$value, 'onHandler'];
}
/**
* @return array
*/
2021-08-30 19:02:28 +08:00
private function loadNamespace(): array
2021-08-02 16:38:50 +08:00
{
$name = array_column($this->groupTacks, 'namespace');
2021-08-03 12:39:48 +08:00
array_unshift($name, $this->dir);
2021-08-02 16:38:50 +08:00
return array_filter($name);
}
/**
* @param array $config
* @param callable $callback
* 路由分组
* @param null $stdClass
*/
public function group(array $config, callable $callback, $stdClass = null)
{
$this->groupTacks[] = $config;
if ($stdClass) {
$callback($stdClass);
} else {
$callback($this);
}
array_pop($this->groupTacks);
}
/**
* @return string
*/
public function addPrefix(): string
{
$prefix = array_column($this->groupTacks, 'prefix');
$prefix = array_filter($prefix);
if (empty($prefix)) {
return '';
}
return '/' . implode('/', $prefix);
}
/**
* @param array|null $explode
* @return Node|null
* 查找指定路由
2021-08-30 17:10:58 +08:00
* @throws Exception
2021-08-02 16:38:50 +08:00
*/
2021-08-30 17:10:58 +08:00
public function tree_search(?array $explode): ?Node
2021-08-02 16:38:50 +08:00
{
2021-08-30 17:20:22 +08:00
if (empty($this->nodes)) {
return null;
}
2021-08-31 01:14:17 +08:00
$parent = $this->nodes[array_shift($explode)] ?? null;
2021-08-30 17:20:22 +08:00
if (!($parent instanceof Node)) {
2021-08-02 16:38:50 +08:00
return null;
}
while ($value = array_shift($explode)) {
$node = $parent->findNode($value);
if (!$node) {
2021-08-13 15:32:35 +08:00
return null;
2021-08-02 16:38:50 +08:00
}
$parent = $node;
}
return $parent;
}
/**
* @param $path
2021-08-05 11:39:04 +08:00
* @return array|null
2021-08-02 16:38:50 +08:00
*/
2021-08-05 11:39:04 +08:00
public function split($path): ?array
2021-08-02 16:38:50 +08:00
{
2021-08-05 11:19:12 +08:00
$path = $this->addPrefix() . '/' . ltrim($path, '/');
if ($path === '/') {
2021-08-30 17:20:22 +08:00
return ['/'];
2021-08-02 16:38:50 +08:00
}
2021-08-05 11:19:12 +08:00
$filter = array_filter(explode('/', $path));
2021-08-05 11:39:04 +08:00
if (!empty($filter)) {
2021-08-05 13:54:33 +08:00
return $filter;
2021-08-02 16:38:50 +08:00
}
2021-08-30 17:20:22 +08:00
return ['/'];
2021-08-02 16:38:50 +08:00
}
/**
* @return array
*/
public function each(): array
{
$paths = [];
2021-08-31 01:48:34 +08:00
foreach ($this->nodes as $_node) {
2021-08-02 16:38:50 +08:00
/** @var Node[] $node */
2021-08-31 01:58:05 +08:00
$paths[] = ['method' => $_node->method, 'path' => $_node->sourcePath];
2021-08-31 01:48:34 +08:00
$paths = $this->getChildes($_node, $paths);
2021-08-02 16:38:50 +08:00
}
return $paths;
}
2021-08-25 11:07:46 +08:00
/**
* @param Node $node
* @param array $path
* @return array
*/
private function getChildes(Node $node, array $path): array
{
foreach ($node->childes as $item) {
2021-08-31 01:58:05 +08:00
$path[] = ['method' => $item->method, 'path' => $item->sourcePath];
2021-08-25 11:07:46 +08:00
if (!empty($item->childes)) {
$path = $this->getChildes($item, $path);
}
}
return $path;
}
2021-08-02 16:38:50 +08:00
/**
* @param $exception
* @return mixed
* @throws Exception
*/
public function exception($exception): mixed
{
2021-08-11 01:04:57 +08:00
return Kiri::app()->getLogger()->exception($exception);
2021-08-02 16:38:50 +08:00
}
/**
* @param $uri
* @param $method
* @return Node|null
*/
public function search($uri, $method): Node|null
{
2021-08-05 11:45:14 +08:00
if (!isset($this->nodes[$method])) {
2021-08-02 16:38:50 +08:00
return null;
}
2021-08-05 11:45:14 +08:00
$methods = $this->nodes[$method];
2021-08-02 16:38:50 +08:00
if (isset($methods[$uri])) {
return $methods[$uri];
}
return $methods['/'] ?? null;
}
/**
2021-08-05 19:14:08 +08:00
* @param RequestInterface $request
2021-08-02 16:38:50 +08:00
* @return Node|null
* 树杈搜索
2021-08-30 17:11:37 +08:00
* @throws Exception
2021-08-02 16:38:50 +08:00
*/
2021-08-05 19:14:08 +08:00
public function Branch_search(RequestInterface $request): ?Node
2021-08-02 16:38:50 +08:00
{
2021-08-27 17:06:17 +08:00
$uri = $request->getUri();
2021-08-30 17:11:37 +08:00
$node = $this->tree_search($uri->getExplode());
2021-08-02 16:38:50 +08:00
if ($node instanceof Node) {
return $node;
}
2021-08-27 17:06:17 +08:00
if (!$request->isMethod('OPTIONS')) {
2021-08-02 16:38:50 +08:00
return null;
}
2021-08-30 17:11:37 +08:00
$node = $this->tree_search(['*']);
2021-08-02 16:38:50 +08:00
if (!($node instanceof Node)) {
return null;
}
return $node;
}
/**
* @throws
*/
public function _loader()
{
$this->loadRouteDir(APP_PATH . 'routes');
}
/**
* @param $path
* @throws Exception
* 加载目录下的路由文件
*/
private function loadRouteDir($path)
{
$files = glob($path . '/*');
for ($i = 0; $i < count($files); $i++) {
if (is_dir($files[$i])) {
$this->loadRouteDir($files[$i]);
} else {
$this->loadRouterFile($files[$i]);
}
}
}
2021-07-26 12:48:31 +08:00
2021-07-27 18:32:24 +08:00
/**
2021-08-02 16:38:50 +08:00
* @param $files
2021-07-27 18:32:24 +08:00
* @throws Exception
*/
2021-08-02 16:38:50 +08:00
private function loadRouterFile($files)
{
try {
$router = $this;
2021-08-30 19:02:28 +08:00
include_once "$files";
} catch (Throwable $exception) {
2021-08-02 16:38:50 +08:00
$this->addError($exception, 'throwable');
} finally {
if (isset($exception)) {
unset($exception);
}
}
}
2020-09-01 03:38:27 +08:00
2020-08-31 01:27:08 +08:00
}