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

629 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;
use Closure;
use Exception;
2021-02-20 17:33:28 +08:00
use HttpServer\Abstracts\HttpService;
2020-09-07 10:51:23 +08:00
use HttpServer\Http\Request;
2020-08-31 01:27:08 +08:00
use HttpServer\IInterface\RouterInterface;
2020-12-17 14:12:44 +08:00
2020-09-04 01:05:33 +08:00
use Snowflake\Abstracts\Config;
2020-09-10 16:00:26 +08:00
use Snowflake\Exception\ComponentException;
2020-08-31 01:27:08 +08:00
use Snowflake\Exception\ConfigException;
2020-08-31 12:38:32 +08:00
use Snowflake\Snowflake;
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
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 Router extends HttpService implements RouterInterface
2020-08-31 01:27:08 +08:00
{
/** @var Node[] $nodes */
2020-10-29 18:17:25 +08:00
public array $nodes = [];
public array $groupTacks = [];
public ?string $dir = 'App\\Http\\Controllers';
2020-08-31 01:27:08 +08:00
2020-09-09 11:05:28 +08:00
const NOT_FOUND = 'Page not found or method not allowed.';
2020-08-31 01:27:08 +08:00
/** @var string[] */
2020-10-29 18:17:25 +08:00
public array $methods = ['get', 'post', 'options', 'put', 'delete', 'receive'];
2020-08-31 01:27:08 +08:00
2020-10-13 11:55:15 +08:00
2020-10-30 01:04:54 +08:00
public ?Closure $middleware = null;
2020-10-15 10:53:35 +08:00
2020-10-29 18:17:25 +08:00
public bool $useTree = false;
2020-10-13 11:55:15 +08:00
2020-10-15 10:53:35 +08:00
/**
* @param Closure $middleware
*/
public function setMiddleware(\Closure $middleware): void
{
$this->middleware = $middleware;
}
2020-08-31 01:27:08 +08:00
/**
* @throws ConfigException
* 初始化函数路径
*/
public function init()
{
2020-08-31 17:21:45 +08:00
$this->dir = Config::get('http.namespace', false, $this->dir);
2020-08-31 01:27:08 +08:00
}
2020-10-13 11:55:15 +08:00
/**
* @param bool $useTree
*/
public function setUseTree(bool $useTree): void
{
$this->useTree = $useTree;
}
2020-08-31 01:27:08 +08:00
/**
* @param $path
* @param $handler
* @param string $method
2020-12-15 14:04:02 +08:00
* @return ?Node
* @throws ConfigException
2020-08-31 01:27:08 +08:00
*/
2020-12-15 14:04:02 +08:00
public function addRoute($path, $handler, $method = 'any'): ?Node
2020-08-31 01:27:08 +08:00
{
2020-09-21 17:30:13 +08:00
$method = strtolower($method);
2020-09-04 18:47:22 +08:00
if (!isset($this->nodes[$method])) {
$this->nodes[$method] = [];
2020-08-31 01:27:08 +08:00
}
2020-10-13 13:36:45 +08:00
2021-02-20 16:23:23 +08:00
$useTree = Config::get('router', false, ROUTER_TREE);
if ($useTree == ROUTER_TREE) {
return $this->tree($path, $handler, $method);
} else {
return $this->hash($path, $handler, $method);
}
2020-10-13 11:55:15 +08:00
}
2020-09-07 10:42:10 +08:00
2020-10-13 11:55:15 +08:00
/**
* @param $path
* @param $handler
* @param string $method
2020-12-15 14:04:02 +08:00
* @return ?Node
2020-10-13 11:55:15 +08:00
*/
2020-12-15 14:04:02 +08:00
private function hash($path, $handler, $method = 'any'): ?Node
2020-10-13 11:55:15 +08:00
{
$path = $this->resolve($path);
2020-09-07 10:42:10 +08:00
2020-10-13 11:55:15 +08:00
$this->nodes[$method][$path] = $this->NodeInstance($path, 0, $method);
2020-09-07 10:42:10 +08:00
return $this->nodes[$method][$path]->bindHandler($handler);
2020-08-31 01:27:08 +08:00
}
2020-10-13 11:55:15 +08:00
/**
* @param $path
* @return string
*/
2020-12-17 14:12:44 +08:00
private function resolve($path): string
2020-10-13 11:55:15 +08:00
{
$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
*/
2020-12-16 15:40:42 +08:00
private function tree($path, $handler, $method = 'any'): Node
2020-10-13 11:55:15 +08:00
{
list($first, $explode) = $this->split($path);
$parent = $this->nodes[$method][$first] ?? null;
if (empty($parent)) {
$parent = $this->NodeInstance($first, 0, $method);
$this->nodes[$method][$first] = $parent;
}
if ($first !== '/') {
$parent = $this->bindNode($parent, $explode, $method);
}
return $parent->bindHandler($handler);
}
2020-08-31 01:27:08 +08:00
/**
* @param Node $parent
* @param array $explode
* @param $method
* @return Node
*/
2020-12-16 15:40:42 +08:00
private function bindNode(Node $parent, array $explode, $method): Node
2020-08-31 01:27:08 +08:00
{
$a = 0;
if (empty($explode)) {
return $parent->addChild($this->NodeInstance('/', $a, $method), '/');
}
foreach ($explode as $value) {
if (empty($value)) {
continue;
}
++$a;
$search = $parent->findNode($value);
if ($search === null) {
$parent = $parent->addChild($this->NodeInstance($value, $a, $method), $value);
} else {
$parent = $search;
}
}
return $parent;
}
/**
* @param $route
* @param $handler
2020-12-17 14:09:14 +08:00
* @return Node|null
2020-12-17 17:27:48 +08:00
* @throws
2020-08-31 01:27:08 +08:00
*/
2020-12-17 14:09:14 +08:00
public function socket($route, $handler): ?Node
2020-08-31 01:27:08 +08:00
{
return $this->addRoute($route, $handler, 'socket');
}
/**
* @param $route
* @param $handler
2020-12-16 15:40:42 +08:00
* @return Node|null
2020-12-17 17:27:48 +08:00
* @throws
2020-08-31 01:27:08 +08:00
*/
2020-12-16 15:40:42 +08:00
public function post($route, $handler): ?Node
2020-08-31 01:27:08 +08:00
{
return $this->addRoute($route, $handler, 'post');
}
/**
* @param $route
* @param $handler
2020-12-17 14:09:14 +08:00
* @return Node|null
2020-12-17 17:27:48 +08:00
* @throws
2020-08-31 01:27:08 +08:00
*/
2020-12-17 14:09:14 +08:00
public function get($route, $handler): ?Node
2020-08-31 01:27:08 +08:00
{
return $this->addRoute($route, $handler, 'get');
}
/**
* @param $route
* @param $handler
2020-12-17 17:27:48 +08:00
* @return Node|null
2020-08-31 01:27:08 +08:00
* @throws
*/
2020-12-17 14:09:14 +08:00
public function options($route, $handler): ?Node
2020-08-31 01:27:08 +08:00
{
return $this->addRoute($route, $handler, 'options');
}
/**
* @param $port
* @param Closure $closure
* @throws
*/
public function listen(int $port, Closure $closure)
{
2020-08-31 12:38:32 +08:00
$stdClass = Snowflake::createObject(Handler::class);
2020-08-31 01:27:08 +08:00
$this->group(['prefix' => $port], $closure, $stdClass);
}
/**
* @param $route
* @param $handler
* @return Any
2020-12-17 17:27:48 +08:00
* @throws
2020-08-31 01:27:08 +08:00
*/
2020-12-17 14:09:14 +08:00
public function any($route, $handler): Any
2020-08-31 01:27:08 +08:00
{
$nodes = [];
foreach (['get', 'post', 'options', 'put', 'delete'] as $method) {
$nodes[] = $this->addRoute($route, $handler, $method);
}
return new Any($nodes);
}
/**
* @param $route
* @param $handler
2020-12-17 14:09:14 +08:00
* @return Node|null
2020-12-17 17:27:48 +08:00
* @throws
2020-08-31 01:27:08 +08:00
*/
2020-12-17 14:09:14 +08:00
public function delete($route, $handler): ?Node
2020-08-31 01:27:08 +08:00
{
return $this->addRoute($route, $handler, 'delete');
}
/**
* @param $route
* @param $handler
2020-12-17 14:09:14 +08:00
* @return Node|null
2020-12-17 17:27:48 +08:00
* @throws
2020-08-31 01:27:08 +08:00
*/
2020-12-17 14:09:14 +08:00
public function put($route, $handler): ?Node
2020-08-31 01:27:08 +08:00
{
return $this->addRoute($route, $handler, 'put');
}
/**
* @param $value
* @param $index
* @param $method
* @return Node
* @throws
*/
2020-12-15 14:04:02 +08:00
public function NodeInstance($value, $index = 0, $method = 'get'): Node
2020-08-31 01:27:08 +08:00
{
$node = new Node();
$node->childes = [];
$node->path = $value;
$node->index = $index;
2020-09-04 18:47:22 +08:00
$node->method = $method;
2020-10-15 10:59:28 +08:00
$node->namespace = $this->loadNamespace($method);
2020-08-31 01:27:08 +08:00
$name = array_column($this->groupTacks, 'middleware');
2020-10-15 10:53:47 +08:00
if ($this->middleware instanceof \Closure) {
2020-10-15 11:58:17 +08:00
$node->addMiddleware($this->middleware);
2020-08-31 01:27:08 +08:00
}
2020-10-15 10:53:35 +08:00
$node->bindMiddleware($name);
2020-08-31 01:27:08 +08:00
return $node;
}
2020-10-15 10:59:28 +08:00
/**
* @param $method
* @return array
*/
2020-12-15 14:04:02 +08:00
private function loadNamespace($method): array
2020-10-15 10:59:28 +08:00
{
$name = array_column($this->groupTacks, 'namespace');
if ($method == 'package' || $method == 'receive') {
$dir = 'App\\Listener';
} else {
$dir = $this->dir;
}
array_unshift($name, $dir);
return array_filter($name);
}
2020-08-31 01:27:08 +08:00
/**
* @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
*/
2020-12-15 14:04:02 +08:00
public function addPrefix(): string
2020-08-31 01:27:08 +08:00
{
$prefix = array_column($this->groupTacks, 'prefix');
$prefix = array_filter($prefix);
if (empty($prefix)) {
return '';
}
return '/' . implode('/', $prefix);
}
/**
2020-10-13 11:55:15 +08:00
* @param array|null $explode
2020-09-04 18:47:22 +08:00
* @param $method
2020-08-31 01:27:08 +08:00
* @return Node|null
* 查找指定路由
*/
2020-12-15 14:04:02 +08:00
public function tree_search(?array $explode, $method): ?Node
2020-08-31 01:27:08 +08:00
{
if (empty($explode)) {
2020-09-04 18:47:22 +08:00
return $this->nodes[$method]['/'] ?? null;
2020-08-31 01:27:08 +08:00
}
$first = array_shift($explode);
2020-09-04 18:47:22 +08:00
if (!($parent = $this->nodes[$method][$first] ?? null)) {
2020-08-31 01:27:08 +08:00
return null;
}
if (empty($explode)) {
return $parent->findNode('/');
}
while ($value = array_shift($explode)) {
$node = $parent->findNode($value);
if (!$node) {
break;
}
$parent = $node;
}
return $parent;
}
/**
* @param $path
* @return array
* '*'
*/
2020-12-17 14:09:14 +08:00
public function split($path): array
2020-08-31 01:27:08 +08:00
{
$prefix = $this->addPrefix();
$path = ltrim($path, '/');
if (!empty($prefix)) {
$path = $prefix . '/' . $path;
}
$explode = array_filter(explode('/', $path));
if (empty($explode)) {
return ['/', []];
}
$first = array_shift($explode);
if (empty($explode)) {
$explode = [];
}
return [$first, $explode];
}
/**
* @return array
*/
2020-12-17 14:09:14 +08:00
public function each(): array
2020-08-31 01:27:08 +08:00
{
$paths = [];
foreach ($this->nodes as $node) {
/** @var Node[] $node */
foreach ($node as $_node) {
if ($_node->path == '/') {
continue;
}
2020-09-04 18:47:22 +08:00
$path = strtoupper($_node->method) . ' : ' . $_node->path;
2020-08-31 01:27:08 +08:00
if (!empty($_node->childes)) {
$path = $this->readByChild($_node->childes, $path);
}
$paths[] = $path;
}
}
return $this->readByArray($paths);
}
/**
* @param $array
* @param array $returns
* @return array
*/
2020-12-17 14:09:14 +08:00
private function readByArray($array, $returns = []): array
2020-08-31 01:27:08 +08:00
{
foreach ($array as $value) {
if (empty($value)) {
continue;
}
if (is_array($value)) {
$returns = $this->readByArray($value, $returns);
} else {
[$method, $route] = explode(' : ', $value);
$returns[] = ['method' => $method, 'route' => $route];
}
}
return $returns;
}
/**
* @param $child
* @param string $paths
* @return array
*/
2020-12-17 14:09:14 +08:00
private function readByChild($child, $paths = ''): array
2020-08-31 01:27:08 +08:00
{
$newPath = [];
/** @var Node $item */
foreach ($child as $item) {
if ($item->path == '/') {
continue;
}
if (!empty($item->childes)) {
$newPath[] = $this->readByChild($item->childes, $paths . '/' . $item->path);
} else {
[$first, $route] = explode(' : ', $paths);
2020-09-04 18:47:22 +08:00
$newPath[] = strtoupper($item->method) . ' : ' . $route . '/' . $item->path;
2020-08-31 01:27:08 +08:00
}
}
return $newPath;
}
2020-09-09 11:05:28 +08:00
2020-08-31 01:27:08 +08:00
/**
2020-12-16 17:48:01 +08:00
* @return mixed
2020-08-31 01:27:08 +08:00
* @throws
*/
2020-12-16 17:48:01 +08:00
public function dispatch(): mixed
2020-08-31 01:27:08 +08:00
{
2020-12-16 17:48:01 +08:00
if (!($node = $this->find_path(\request()))) {
2021-02-25 14:42:26 +08:00
return send(self::NOT_FOUND);
}
send($response = $node->dispatch(), 200);
if (!$node->hasAfter()) {
return null;
2020-09-09 11:44:14 +08:00
}
2021-02-25 14:42:26 +08:00
return $node->afterDispatch($response);
2020-08-31 01:27:08 +08:00
}
2020-09-07 10:58:39 +08:00
2020-09-10 16:00:26 +08:00
/**
* @param $exception
2020-12-17 14:09:14 +08:00
* @return mixed
2020-09-10 16:00:26 +08:00
* @throws ComponentException
2021-02-20 15:59:42 +08:00
* @throws Exception
2020-09-10 16:00:26 +08:00
*/
2020-12-17 14:09:14 +08:00
private function exception($exception): mixed
2020-09-10 16:00:26 +08:00
{
return Snowflake::app()->getLogger()->exception($exception);
}
2020-08-31 01:27:08 +08:00
/**
2020-09-07 10:51:23 +08:00
* @param Request $request
2020-12-17 14:09:14 +08:00
* @return Node|null 树干搜索
2020-09-07 10:58:39 +08:00
* 树干搜索
2021-02-20 16:23:23 +08:00
* @throws ConfigException
2020-08-31 01:27:08 +08:00
*/
2021-03-03 13:44:37 +08:00
public function find_path(Request $request): ?Node
2020-08-31 01:27:08 +08:00
{
2021-02-20 16:23:23 +08:00
$useTree = Config::get('router', false, ROUTER_TREE);
if ($useTree === ROUTER_TREE) {
return $this->Branch_search($request);
}
2021-02-20 16:03:36 +08:00
2020-09-07 10:42:10 +08:00
$method = $request->getMethod();
2021-02-20 15:59:42 +08:00
$uri = $request->headers->get('request_uri', '/');
2020-09-07 10:42:10 +08:00
if (!isset($this->nodes[$method])) {
return null;
}
$methods = $this->nodes[$method];
2020-09-16 21:09:45 +08:00
if (isset($methods[$uri])) {
return $methods[$uri];
}
if (!$request->isOption || !isset($methods['/'])) {
return null;
2020-09-07 10:42:10 +08:00
}
2020-09-16 21:09:45 +08:00
return $methods['/'];
2020-12-15 14:04:02 +08:00
}
2020-09-07 10:58:39 +08:00
2020-12-15 14:04:02 +08:00
/**
* @param $uri
* @param $method
* @return Node|null
*/
public function search($uri, $method): Node|null
{
if (!isset($this->nodes[$method])) {
return null;
}
$methods = $this->nodes[$method];
if (isset($methods[$uri])) {
return $methods[$uri];
}
return $methods['/'] ?? null;
2020-09-07 10:58:39 +08:00
}
2020-09-07 11:16:41 +08:00
/**
* @param $request
* @return Node|null
*/
2020-12-17 14:09:14 +08:00
private function search_options($request): ?Node
2020-09-07 11:16:41 +08:00
{
$method = $request->getMethod();
if (!isset($this->nodes[$method])) {
return null;
}
if (!isset($this->nodes[$method]['*'])) {
return null;
}
return $this->nodes[$method]['*'];
}
2020-09-07 10:58:39 +08:00
/**
2021-02-20 16:05:40 +08:00
* @param Request $request
2020-09-07 10:58:39 +08:00
* @return Node|null
* 树杈搜索
*/
2021-02-20 16:05:40 +08:00
private function Branch_search(Request $request): ?Node
2020-09-07 10:58:39 +08:00
{
2020-09-04 18:47:22 +08:00
$node = $this->tree_search($request->getExplode(), $request->getMethod());
2020-08-31 01:27:08 +08:00
if ($node instanceof Node) {
return $node;
}
if (!$request->isOption) {
return null;
}
2020-09-04 18:47:22 +08:00
$node = $this->tree_search(['*'], $request->getMethod());
2020-08-31 01:27:08 +08:00
if (!($node instanceof Node)) {
return null;
}
return $node;
}
2020-09-07 10:58:39 +08:00
2020-08-31 01:27:08 +08:00
/**
* @throws
*/
2020-09-01 03:16:30 +08:00
public function loadRouterSetting()
2020-08-31 01:27:08 +08:00
{
2020-09-01 03:16:30 +08:00
$this->loadRouteDir(APP_PATH . '/routes');
2020-08-31 01:27:08 +08:00
}
/**
* @param $path
* @throws Exception
* 加载目录下的路由文件
*/
2020-09-01 03:16:30 +08:00
private function loadRouteDir($path)
2020-08-31 01:27:08 +08:00
{
2020-09-01 03:16:30 +08:00
$files = glob($path . '/*');
for ($i = 0; $i < count($files); $i++) {
2020-09-01 03:38:27 +08:00
if (is_dir($files[$i])) {
$this->loadRouteDir($files[$i]);
} else {
$this->loadRouterFile($files[$i]);
2020-08-31 01:27:08 +08:00
}
}
}
2020-09-01 03:38:27 +08:00
/**
* @param $files
* @throws Exception
*/
private function loadRouterFile($files)
{
try {
$router = $this;
include_once "{$files}";
2020-11-06 16:47:17 +08:00
} catch (\Throwable $exception) {
2021-02-20 15:45:48 +08:00
$this->error($exception);
2020-09-01 03:38:27 +08:00
} finally {
if (isset($exception)) {
unset($exception);
}
}
}
2020-08-31 01:27:08 +08:00
}