From ed6cb4cf28ffb325aed98f73ab70dae041c8e7a4 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Wed, 7 Apr 2021 01:53:32 +0800 Subject: [PATCH] modify --- HttpServer/Route/Node.php | 76 +-- HttpServer/Route/Router.php | 1121 ++++++++++++++++---------------- Validator/LengthValidator.php | 6 +- Validator/TypesOfValidator.php | 4 +- 4 files changed, 580 insertions(+), 627 deletions(-) diff --git a/HttpServer/Route/Node.php b/HttpServer/Route/Node.php index c03199d7..0c25f381 100644 --- a/HttpServer/Route/Node.php +++ b/HttpServer/Route/Node.php @@ -409,26 +409,6 @@ class Node extends HttpService return $this->childes[$field]; } - /** - * @param $rule - * @return $this - */ - public function filter($rule): static - { - 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 @@ -476,21 +456,6 @@ class Node extends HttpService } - /** - * @param int $limit - * @param int $duration - * @param bool $isBindConsumer - * @return $this - * @throws Exception - */ - public function limits(int $limit, int $duration = 60, bool $isBindConsumer = false): static - { - $limits = Snowflake::app()->getLimits(); - $limits->addLimits($this->path, $limit, $duration, $isBindConsumer); - return $this; - } - - /** * @param array|Closure|string $class * @return Node @@ -498,26 +463,10 @@ class Node extends HttpService * @throws NotFindClassException * @throws Exception */ - public function addMiddleware(Closure|string|array $class): static + public function addMiddleware(Closure|array $class): static { if (empty($class)) return $this; - if (is_string($class)) { - $class = $this->resolve_aop($class); - if ($class === null) { - return $this; - } - } - if (is_array($class)) { - if (isset($class[0]) && is_object($class[0])) { - $class = [$class]; - } - } else { - $class = [$class]; - } foreach ($class as $closure) { - if (is_string($closure)) { - $closure = [Snowflake::createObject($closure), 'onHandler']; - } if (in_array($closure, $this->middleware)) { continue; } @@ -527,29 +476,6 @@ class Node extends HttpService } - /** - * @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; - } - } - - /** * @return array */ diff --git a/HttpServer/Route/Router.php b/HttpServer/Route/Router.php index a9461304..27d7c5c8 100644 --- a/HttpServer/Route/Router.php +++ b/HttpServer/Route/Router.php @@ -9,6 +9,7 @@ use Database\InjectProperty; use Exception; use HttpServer\Abstracts\HttpService; use HttpServer\Http\Request; +use HttpServer\IInterface\Middleware; use HttpServer\IInterface\RouterInterface; use JetBrains\PhpStorm\Pure; @@ -28,640 +29,666 @@ defined('ROUTER_HASH') or define('ROUTER_HASH', 2); */ class Router extends HttpService implements RouterInterface { - /** @var Node[] $nodes */ - public array $nodes = []; - public array $groupTacks = []; - public ?string $dir = 'App\\Http\\Controllers'; + /** @var Node[] $nodes */ + public array $nodes = []; + public array $groupTacks = []; + public ?string $dir = 'App\\Http\\Controllers'; - public array $hashMap = []; + public array $hashMap = []; - const NOT_FOUND = 'Page not found or method not allowed.'; + const NOT_FOUND = 'Page not found or method not allowed.'; - /** @var string[] */ - public array $methods = ['get', 'post', 'options', 'put', 'delete', 'receive']; + /** @var string[] */ + public array $methods = ['get', 'post', 'options', 'put', 'delete', 'receive']; - public ?Closure $middleware = null; + public ?Closure $middleware = null; - public int $useTree = ROUTER_TREE; + public int $useTree = ROUTER_TREE; - /** - * @param Closure $middleware - */ - public function setMiddleware(\Closure $middleware): void - { - $this->middleware = $middleware; - } + /** + * @param Closure $middleware + */ + public function setMiddleware(\Closure $middleware): void + { + $this->middleware = $middleware; + } - /** - * @throws ConfigException - * 初始化函数路径 - */ - public function init() - { - $this->dir = Config::get('http.namespace', $this->dir); - } + /** + * @throws ConfigException + * 初始化函数路径 + */ + public function init() + { + $this->dir = Config::get('http.namespace', $this->dir); + } - /** - * @param bool $useTree - */ - public function setUseTree(bool $useTree): void - { - $this->useTree = $useTree ? ROUTER_TREE : ROUTER_HASH; - } + /** + * @param bool $useTree + */ + public function setUseTree(bool $useTree): void + { + $this->useTree = $useTree ? ROUTER_TREE : ROUTER_HASH; + } - /** - * @param $port - * @param Closure|array|string $closure - * @param null $method - * @return Node|bool|null - * @throws Exception - */ - public function addPortListen($port, Closure|array|string $closure, $method = null): Node|null|bool - { - if (!is_string($closure)) { - return $this->addRoute('add-port-listen/port_' . $port, $closure, 'listen'); - } - if (empty($method)) { - return $this->addError($closure . '::' . $method); - } - $_closure = Snowflake::createObject($closure); - if (!method_exists($_closure, $method)) { - return $this->addError($closure . '::' . $method); - } - return $this->addRoute('add-port-listen/port_' . $port, [$_closure, $method], 'listen'); - } + /** + * @param $port + * @param Closure|array|string $closure + * @param null $method + * @return Node|bool|null + * @throws Exception + */ + public function addPortListen($port, Closure|array|string $closure, $method = null): Node|null|bool + { + if (!is_string($closure)) { + return $this->addRoute('add-port-listen/port_' . $port, $closure, 'listen'); + } + if (empty($method)) { + return $this->addError($closure . '::' . $method); + } + $_closure = Snowflake::createObject($closure); + if (!method_exists($_closure, $method)) { + return $this->addError($closure . '::' . $method); + } + return $this->addRoute('add-port-listen/port_' . $port, [$_closure, $method], 'listen'); + } - /** - * @param $path - * @param $handler - * @param string $method - * @return ?Node - */ - public function addRoute($path, $handler, $method = 'any'): ?Node - { - $method = strtolower($method); - if (!isset($this->nodes[$method])) { - $this->nodes[$method] = []; - } + /** + * @param $path + * @param $handler + * @param string $method + * @return ?Node + */ + public function addRoute($path, $handler, $method = 'any'): ?Node + { + $method = strtolower($method); + if (!isset($this->nodes[$method])) { + $this->nodes[$method] = []; + } - if ($this->useTree == ROUTER_TREE) { - return $this->tree($path, $handler, $method); - } else { - return $this->hash($path, $handler, $method); - } - } + if ($this->useTree == ROUTER_TREE) { + return $this->tree($path, $handler, $method); + } else { + return $this->hash($path, $handler, $method); + } + } - /** - * @param $path - * @param $handler - * @param string $method - * @return ?Node - */ - private function hash($path, $handler, $method = 'any'): ?Node - { - $path = $this->resolve($path); + /** + * @param $path + * @param $handler + * @param string $method + * @return ?Node + */ + private function hash($path, $handler, $method = 'any'): ?Node + { + $path = $this->resolve($path); - $this->nodes[$method][$path] = $this->NodeInstance($path, 0, $method); + $this->nodes[$method][$path] = $this->NodeInstance($path, 0, $method); - return $this->nodes[$method][$path]->bindHandler($handler); - } + return $this->nodes[$method][$path]->bindHandler($handler); + } - /** - * @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 + * @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 - */ - private function tree($path, $handler, $method = 'any'): Node - { - list($first, $explode) = $this->split($path); + /** + * @param $path + * @param $handler + * @param string $method + * @return Node + */ + private function tree($path, $handler, $method = 'any'): Node + { + list($first, $explode) = $this->split($path); - $parent = $this->nodes[$method][$first] ?? null; - if (empty($parent)) { - $this->nodes[$method][$first] = $parent = $this->NodeInstance('/', 0, $method); - } + $parent = $this->nodes[$method][$first] ?? null; + if (empty($parent)) { + $this->nodes[$method][$first] = $parent = $this->NodeInstance('/', 0, $method); + } - if ($first !== '/') { - $parent = $this->bindNode($parent, $explode, $method); - } - $parent->path = $path; - return $parent->bindHandler($handler); - } + if ($first !== '/') { + $parent = $this->bindNode($parent, $explode, $method); + } + $parent->path = $path; + return $parent->bindHandler($handler); + } - /** - * @param Node $parent - * @param array $explode - * @param $method - * @return Node - */ - private function bindNode(Node $parent, array $explode, $method): Node - { - $a = 0; - if (empty($explode)) { - return $parent->addChild($this->NodeInstance('/', $a, $method), '/'); - } - foreach ($explode as $value) { - if (empty($value)) { - continue; - } - ++$a; + /** + * @param Node $parent + * @param array $explode + * @param $method + * @return Node + */ + private function bindNode(Node $parent, array $explode, $method): Node + { + $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; - } + $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 - * @return Node|null - * @throws - */ - public function socket($route, $handler): ?Node - { - return $this->addRoute($route, $handler, 'socket'); - } + /** + * @param $route + * @param $handler + * @return Node|null + * @throws + */ + public function socket($route, $handler): ?Node + { + return $this->addRoute($route, $handler, 'socket'); + } - /** - * @param $route - * @param $handler - * @return Node|null - * @throws - */ - public function post($route, $handler): ?Node - { - return $this->addRoute($route, $handler, 'post'); - } + /** + * @param $route + * @param $handler + * @return Node|null + * @throws + */ + public function post($route, $handler): ?Node + { + return $this->addRoute($route, $handler, 'post'); + } - /** - * @param $route - * @param $handler - * @return Node|null - * @throws - */ - public function get($route, $handler): ?Node - { - return $this->addRoute($route, $handler, 'get'); - } + /** + * @param $route + * @param $handler + * @return Node|null + * @throws + */ + public function get($route, $handler): ?Node + { + return $this->addRoute($route, $handler, 'get'); + } - /** - * @param $route - * @param $handler - * @return Node|null - * @throws - */ - public function options($route, $handler): ?Node - { - return $this->addRoute($route, $handler, 'options'); - } + /** + * @param $route + * @param $handler + * @return Node|null + * @throws + */ + public function options($route, $handler): ?Node + { + return $this->addRoute($route, $handler, 'options'); + } - /** - * @param $port - * @param Closure $closure - * @throws - */ - public function listen(int $port, Closure $closure) - { - $stdClass = Snowflake::createObject(Handler::class); - $this->group(['prefix' => $port], $closure, $stdClass); - } + /** + * @param $port + * @param Closure $closure + * @throws + */ + public function listen(int $port, Closure $closure) + { + $stdClass = Snowflake::createObject(Handler::class); + $this->group(['prefix' => $port], $closure, $stdClass); + } - /** - * @param $route - * @param $handler - * @return Any - * @throws - */ - public function any($route, $handler): Any - { - $nodes = []; - foreach (['get', 'post', 'options', 'put', 'delete'] as $method) { - $nodes[] = $this->addRoute($route, $handler, $method); - } - return new Any($nodes); - } + /** + * @param $route + * @param $handler + * @return Any + * @throws + */ + public function any($route, $handler): Any + { + $nodes = []; + foreach (['get', 'post', 'options', 'put', 'delete'] as $method) { + $nodes[] = $this->addRoute($route, $handler, $method); + } + return new Any($nodes); + } - /** - * @param $route - * @param $handler - * @return Node|null - * @throws - */ - public function delete($route, $handler): ?Node - { - return $this->addRoute($route, $handler, 'delete'); - } + /** + * @param $route + * @param $handler + * @return Node|null + * @throws + */ + public function delete($route, $handler): ?Node + { + return $this->addRoute($route, $handler, 'delete'); + } - /** - * @param $route - * @param $handler - * @return Node|null - * @throws - */ - public function put($route, $handler): ?Node - { - return $this->addRoute($route, $handler, 'put'); - } + /** + * @param $route + * @param $handler + * @return Node|null + * @throws + */ + public function put($route, $handler): ?Node + { + return $this->addRoute($route, $handler, 'put'); + } - /** - * @param $value - * @param $index - * @param $method - * @return Node - * @throws - */ - public function NodeInstance($value, $index = 0, $method = 'get'): Node - { - $node = new Node(); - $node->childes = []; - $node->path = $value; - $node->index = $index; - $node->method = $method; - $node->namespace = $this->loadNamespace($method); + /** + * @param $value + * @param $index + * @param $method + * @return Node + * @throws + */ + public function NodeInstance($value, $index = 0, $method = 'get'): Node + { + $node = new Node(); + $node->childes = []; + $node->path = $value; + $node->index = $index; + $node->method = $method; + $node->namespace = $this->loadNamespace($method); - $name = array_column($this->groupTacks, 'middleware'); - if ($this->middleware instanceof \Closure) { - $node->addMiddleware($this->middleware); - } + $name = array_column($this->groupTacks, 'middleware'); + if ($this->middleware instanceof \Closure) { + $node->addMiddleware([$this->middleware]); + } - if (is_array($name)) foreach ($name as $item) { - $node->addMiddleware($item); - } + if (is_array($name)) { + $node->addMiddleware($this->resolve_middleware($name)); + } - return $node; - } + return $node; + } - /** - * @param $method - * @return array - */ - private function loadNamespace($method): array - { - $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); - } + /** + * @param string|array $middleware + * @return array + * @throws NotFindClassException + * @throws \ReflectionException + */ + private function resolve_middleware(string|array $middleware) + { + if (is_string($middleware)) { + $middleware = [$middleware]; + } - /** - * @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); - } + $array = []; + foreach ($middleware as $value) { + if (is_string($value)) { + $value = Snowflake::createObject($value); + if (!($value instanceof Middleware)) { + continue; + } + } + $array[] = $value; + } + return $array; + } - /** - * @return string - */ - public function addPrefix(): string - { - $prefix = array_column($this->groupTacks, 'prefix'); - $prefix = array_filter($prefix); + /** + * @param $method + * @return array + */ + private function loadNamespace($method): array + { + $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); + } - if (empty($prefix)) { - return ''; - } + /** + * @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 '/' . implode('/', $prefix); - } + /** + * @return string + */ + public function addPrefix(): string + { + $prefix = array_column($this->groupTacks, 'prefix'); - /** - * @param array|null $explode - * @param $method - * @return Node|null - * 查找指定路由 - */ - public function tree_search(?array $explode, $method): ?Node - { - if (empty($explode)) { - return $this->nodes[$method]['/'] ?? null; - } - $first = array_shift($explode); - if (!($parent = $this->nodes[$method][$first] ?? null)) { - return null; - } - if (empty($explode)) { - return $parent->findNode('/'); - } - while ($value = array_shift($explode)) { - $node = $parent->findNode($value); - if (!$node) { - break; - } - $parent = $node; - } - return $parent; - } + $prefix = array_filter($prefix); - /** - * @param $path - * @return array - * '*' - */ - public function split($path): array - { - $prefix = $this->addPrefix(); - $path = ltrim($path, '/'); - if (!empty($prefix)) { - $path = $prefix . '/' . $path; - } + if (empty($prefix)) { + return ''; + } - $explode = array_filter(explode('/', $path)); - if (empty($explode)) { - return ['/', []]; - } + return '/' . implode('/', $prefix); + } - $first = array_shift($explode); - if (empty($explode)) { - $explode = []; - } - return [$first, $explode]; - } + /** + * @param array|null $explode + * @param $method + * @return Node|null + * 查找指定路由 + */ + public function tree_search(?array $explode, $method): ?Node + { + if (empty($explode)) { + return $this->nodes[$method]['/'] ?? null; + } + $first = array_shift($explode); + if (!($parent = $this->nodes[$method][$first] ?? null)) { + return null; + } + if (empty($explode)) { + return $parent->findNode('/'); + } + while ($value = array_shift($explode)) { + $node = $parent->findNode($value); + if (!$node) { + break; + } + $parent = $node; + } + return $parent; + } - /** - * @return array - */ - public function each(): array - { - $paths = []; - foreach ($this->nodes as $node) { - /** @var Node[] $node */ - foreach ($node as $_node) { + /** + * @param $path + * @return array + * '*' + */ + public function split($path): array + { + $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 + */ + public function each(): array + { + $paths = []; + foreach ($this->nodes as $node) { + /** @var Node[] $node */ + foreach ($node as $_node) { // if ($_node->path == '/') { // continue; // } - $path = strtoupper($_node->method) . ' : ' . $_node->path; - if (!empty($_node->childes)) { - $path = $this->readByChild($_node->childes, $path); - } - $paths[] = $path; - } - } - return $this->readByArray($paths); - } + $path = strtoupper($_node->method) . ' : ' . $_node->path; + if (!empty($_node->childes)) { + $path = $this->readByChild($_node->childes, $path); + } + $paths[] = $path; + } + } + return $this->readByArray($paths); + } - /** - * @param $array - * @param array $returns - * @return array - */ - private function readByArray($array, $returns = []): array - { - foreach ($array as $value) { - if (empty($value)) { - continue; - } - if (is_array($value)) { - $returns = $this->readByArray($value, $returns); - } else { - [$method, $route] = explode(' : ', $value); + /** + * @param $array + * @param array $returns + * @return array + */ + private function readByArray($array, $returns = []): array + { + 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; - } + $returns[] = ['method' => $method, 'route' => $route]; + } + } + return $returns; + } - /** - * @param $child - * @param string $paths - * @return array - */ - private function readByChild($child, $paths = ''): array - { - $newPath = []; - /** @var Node $item */ - foreach ($child as $item) { + /** + * @param $child + * @param string $paths + * @return array + */ + private function readByChild($child, $paths = ''): array + { + $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); + if (!empty($item->childes)) { + $newPath[] = $this->readByChild($item->childes, $paths . '/' . $item->path); + } else { + [$first, $route] = explode(' : ', $paths); - $newPath[] = strtoupper($item->method) . ' : ' . $route . '/' . $item->path; - } - } - return $newPath; - } + $newPath[] = strtoupper($item->method) . ' : ' . $route . '/' . $item->path; + } + } + return $newPath; + } - /** - * @return mixed - * @throws - */ - public function dispatch(): mixed - { - try { - if (!($node = $this->find_path(\request()))) { - return send(self::NOT_FOUND); - } - send($response = $node->dispatch(), 200); - if (!$node->hasAfter()) { - return null; - } - return $node->afterDispatch($response); - } catch (\Throwable $exception) { - $this->addError($exception, 'throwable'); + /** + * @return mixed + * @throws + */ + public function dispatch(): mixed + { + try { + if (!($node = $this->find_path(\request()))) { + return send(self::NOT_FOUND); + } + send($response = $node->dispatch(), 200); + if (!$node->hasAfter()) { + return null; + } + return $node->afterDispatch($response); + } catch (\Throwable $exception) { + $this->addError($exception, 'throwable'); - $Code = $exception->getCode() == 0 ? 500 : $exception->getCode(); + $Code = $exception->getCode() == 0 ? 500 : $exception->getCode(); - return send(Json::to($Code, $exception->getMessage()), 200); - } - } + return send(Json::to($Code, $exception->getMessage()), 200); + } + } - /** - * @param $exception - * @return mixed - * @throws Exception - */ - private function exception($exception): mixed - { - return Snowflake::app()->getLogger()->exception($exception); - } + /** + * @param $exception + * @return mixed + * @throws Exception + */ + private function exception($exception): mixed + { + return Snowflake::app()->getLogger()->exception($exception); + } - /** - * @param Request $request - * @return Node|null 树干搜索 - * 树干搜索 - * @throws ConfigException - */ - public function find_path(Request $request): ?Node - { - if ($this->useTree === ROUTER_TREE) { - return $this->Branch_search($request); - } + /** + * @param Request $request + * @return Node|null 树干搜索 + * 树干搜索 + * @throws ConfigException + */ + public function find_path(Request $request): ?Node + { + if ($this->useTree === ROUTER_TREE) { + return $this->Branch_search($request); + } - $method = $request->getMethod(); - $uri = $request->headers->get('request_uri', '/'); + $method = $request->getMethod(); + $uri = $request->headers->get('request_uri', '/'); - if (!isset($this->nodes[$method])) { - return null; - } - $methods = $this->nodes[$method]; - if (isset($methods[$uri])) { - return $methods[$uri]; - } - if (!$request->isOption || !isset($methods['/'])) { - return null; - } - return $methods['/']; - } + if (!isset($this->nodes[$method])) { + return null; + } + $methods = $this->nodes[$method]; + if (isset($methods[$uri])) { + return $methods[$uri]; + } + if (!$request->isOption || !isset($methods['/'])) { + return null; + } + return $methods['/']; + } - /** - * @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; - } + /** + * @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; + } - /** - * @param $request - * @return Node|null - */ - private function search_options($request): ?Node - { - $method = $request->getMethod(); - if (!isset($this->nodes[$method])) { - return null; - } - if (!isset($this->nodes[$method]['*'])) { - return null; - } - return $this->nodes[$method]['*']; - } + /** + * @param $request + * @return Node|null + */ + private function search_options($request): ?Node + { + $method = $request->getMethod(); + if (!isset($this->nodes[$method])) { + return null; + } + if (!isset($this->nodes[$method]['*'])) { + return null; + } + return $this->nodes[$method]['*']; + } - /** - * @param Request $request - * @return Node|null - * 树杈搜索 - */ - private function Branch_search(Request $request): ?Node - { - $node = $this->tree_search($request->getExplode(), $request->getMethod()); - if ($node instanceof Node) { - return $node; - } - if (!$request->isOption) { - return null; - } - $node = $this->tree_search(['*'], $request->getMethod()); - if (!($node instanceof Node)) { - return null; - } - return $node; - } + /** + * @param Request $request + * @return Node|null + * 树杈搜索 + */ + private function Branch_search(Request $request): ?Node + { + $node = $this->tree_search($request->getExplode(), $request->getMethod()); + if ($node instanceof Node) { + return $node; + } + if (!$request->isOption) { + return null; + } + $node = $this->tree_search(['*'], $request->getMethod()); + if (!($node instanceof Node)) { + return null; + } + return $node; + } - /** - * @throws - */ - public function loadRouterSetting() - { - $this->loadRouteDir(APP_PATH . 'routes'); - } + /** + * @throws + */ + public function loadRouterSetting() + { + $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]); - } - } - } + /** + * @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]); + } + } + } - /** - * @param $files - * @throws Exception - */ - private function loadRouterFile($files) - { - try { - $router = $this; - include_once "{$files}"; - } catch (\Throwable $exception) { - $this->addError($exception, 'throwable'); - } finally { - if (isset($exception)) { - unset($exception); - } - } - } + /** + * @param $files + * @throws Exception + */ + private function loadRouterFile($files) + { + try { + $router = $this; + include_once "{$files}"; + } catch (\Throwable $exception) { + $this->addError($exception, 'throwable'); + } finally { + if (isset($exception)) { + unset($exception); + } + } + } } diff --git a/Validator/LengthValidator.php b/Validator/LengthValidator.php index 72ec3121..616e0a19 100644 --- a/Validator/LengthValidator.php +++ b/Validator/LengthValidator.php @@ -60,7 +60,7 @@ class LengthValidator extends BaseValidator return $this->addError('The param :attribute length overflow'); } } else { - if (is_numeric($value) && strlen(floatval($value)) > $this->value) { + if (is_numeric($value) && strlen((string)$value) > $this->value) { return $this->addError('The param :attribute length overflow'); } if (strlen($value) > $this->value) { @@ -83,7 +83,7 @@ class LengthValidator extends BaseValidator return $this->addError('The param :attribute length error'); } } else { - if (is_numeric($value) && strlen(floatval($value)) < $this->value) { + if (is_numeric($value) && strlen((string)$value) < $this->value) { return $this->addError('The param :attribute length overflow'); } if (strlen($value) < $this->value) { @@ -106,7 +106,7 @@ class LengthValidator extends BaseValidator return $this->addError('The param :attribute length error'); } } else { - if (is_numeric($value) && mb_strlen(floatval($value)) !== $this->value) { + if (is_numeric($value) && strlen((string)$value) !== $this->value) { return $this->addError('The param :attribute length overflow'); } if (mb_strlen($value) !== $this->value) { diff --git a/Validator/TypesOfValidator.php b/Validator/TypesOfValidator.php index 2d0d17fa..4c3586e4 100644 --- a/Validator/TypesOfValidator.php +++ b/Validator/TypesOfValidator.php @@ -126,7 +126,7 @@ class TypesOfValidator extends BaseValidator if (!is_numeric($value)) { return $this->addError('The ' . $this->field . ' not is number data.'); } - if (intval($value) != $value) { + if ((int)$value != $value) { return $this->addError('The ' . $this->field . ' not is number data.'); } @@ -139,7 +139,7 @@ class TypesOfValidator extends BaseValidator */ public function floatFormat($value): bool { - $trim = floatval((string)$value); + $trim = (float)$value; if ($trim != $value || !is_float($trim)) { return $this->addError('The ' . $this->field . ' not is float data.'); }