From 9bed1836d70e4c9832844d59aebf91bae62103e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Sat, 8 May 2021 10:53:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HttpServer/Route/Node.php | 18 +++++++++--------- HttpServer/Route/Router.php | 35 ++++++++++++++++++----------------- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/HttpServer/Route/Node.php b/HttpServer/Route/Node.php index 346dbc3e..23911d3a 100644 --- a/HttpServer/Route/Node.php +++ b/HttpServer/Route/Node.php @@ -35,7 +35,7 @@ class Node extends HttpService public string $method = ''; /** @var Node[] $childes */ - private static array $childes = []; + public array $childes = []; public array $group = []; @@ -429,12 +429,12 @@ class Node extends HttpService { $field = (string)$field; /** @var Node $oLod */ - $oLod = static::$childes[$field] ?? null; + $oLod = $this->childes[$field] ?? null; if (!empty($oLod)) { $node = $oLod; } - static::$childes[$field] = $node; - return static::$childes[$field]; + $this->childes[$field] = $node; + return $this->childes[$field]; } @@ -445,19 +445,19 @@ class Node extends HttpService */ public function findNode(string $search): ?Node { - if (empty(static::$childes)) { + if (empty($this->childes)) { return null; } - if (isset(static::$childes[$search])) { - return static::$childes[$search]; + if (isset($this->childes[$search])) { + return $this->childes[$search]; } $_searchMatch = '/<(\w+)?:(.+)?>/'; - foreach (static::$childes as $key => $val) { + foreach ($this->childes as $key => $val) { if (preg_match($_searchMatch, (string)$key, $match)) { Input()->addGetParam($match[1] ?? '--', $search); - return static::$childes[$key]; + return $this->childes[$key]; } } return null; diff --git a/HttpServer/Route/Router.php b/HttpServer/Route/Router.php index d0731435..276fff19 100644 --- a/HttpServer/Route/Router.php +++ b/HttpServer/Route/Router.php @@ -27,7 +27,7 @@ defined('ROUTER_HASH') or define('ROUTER_HASH', 2); class Router extends HttpService implements RouterInterface { /** @var Node[] $nodes */ - public static array $nodes = []; + public array $nodes = []; public array $groupTacks = []; public ?string $dir = 'App\\Http\\Controllers'; @@ -104,8 +104,8 @@ class Router extends HttpService implements RouterInterface public function addRoute($path, $handler, $method = 'any'): ?Node { $method = strtolower($method); - if (!isset(static::$nodes[$method])) { - static::$nodes[$method] = []; + if (!isset($this->nodes[$method])) { + $this->nodes[$method] = []; } if ($handler instanceof Closure) { @@ -130,9 +130,9 @@ class Router extends HttpService implements RouterInterface { $path = $this->resolve($path); - static::$nodes[$method][$path] = $this->NodeInstance($path, 0, $method); + $this->nodes[$method][$path] = $this->NodeInstance($path, 0, $method); - return static::$nodes[$method][$path]->bindHandler($handler); + return $this->nodes[$method][$path]->bindHandler($handler); } @@ -164,9 +164,9 @@ class Router extends HttpService implements RouterInterface { list($first, $explode) = $this->split($path); - $parent = static::$nodes[$method][$first] ?? null; + $parent = $this->nodes[$method][$first] ?? null; if (empty($parent)) { - static::$nodes[$method][$first] = $parent = $this->NodeInstance('/', 0, $method); + $this->nodes[$method][$first] = $parent = $this->NodeInstance('/', 0, $method); } if ($first !== '/') { @@ -298,6 +298,7 @@ class Router extends HttpService implements RouterInterface public function NodeInstance($value, $index = 0, $method = 'get'): Node { $node = new Node(); + $node->childes = []; $node->path = $value; $node->index = $index; $node->method = $method; @@ -420,10 +421,10 @@ class Router extends HttpService implements RouterInterface public function tree_search(?array $explode, $method): ?Node { if (empty($explode)) { - return static::$nodes[$method]['/'] ?? null; + return $this->nodes[$method]['/'] ?? null; } $first = array_shift($explode); - if (!($parent = static::$nodes[$method][$first] ?? null)) { + if (!($parent = $this->nodes[$method][$first] ?? null)) { return null; } if (empty($explode)) { @@ -470,7 +471,7 @@ class Router extends HttpService implements RouterInterface public function each(): array { $paths = []; - foreach (static::$nodes as $node) { + foreach ($this->nodes as $node) { /** @var Node[] $node */ foreach ($node as $path => $_node) { $paths[] = strtoupper($_node->method) . ' : ' . $path; @@ -520,10 +521,10 @@ class Router extends HttpService implements RouterInterface $method = $request->getMethod(); $uri = $request->headers->get('request_uri', '/'); - if (!isset(static::$nodes[$method])) { + if (!isset($this->nodes[$method])) { return null; } - $methods = static::$nodes[$method]; + $methods = $this->nodes[$method]; if (isset($methods[$uri])) { return $methods[$uri]; } @@ -541,10 +542,10 @@ class Router extends HttpService implements RouterInterface */ public function search($uri, $method): Node|null { - if (!isset(static::$nodes[$method])) { + if (!isset($this->nodes[$method])) { return null; } - $methods = static::$nodes[$method]; + $methods = $this->nodes[$method]; if (isset($methods[$uri])) { return $methods[$uri]; } @@ -559,13 +560,13 @@ class Router extends HttpService implements RouterInterface private function search_options($request): ?Node { $method = $request->getMethod(); - if (!isset(static::$nodes[$method])) { + if (!isset($this->nodes[$method])) { return null; } - if (!isset(static::$nodes[$method]['*'])) { + if (!isset($this->nodes[$method]['*'])) { return null; } - return static::$nodes[$method]['*']; + return $this->nodes[$method]['*']; }