This commit is contained in:
2021-08-05 11:45:14 +08:00
parent b0a585ac3e
commit deae5457aa
2 changed files with 19 additions and 30 deletions
+1 -4
View File
@@ -281,12 +281,9 @@ class Node extends HttpService
if (empty($this->childes)) { if (empty($this->childes)) {
return null; return null;
} }
if (isset($this->childes[$search])) {
return $this->childes[$search];
}
foreach ($this->childes as $val) { foreach ($this->childes as $val) {
if ($search == $val->path) { if ($search == $val->path) {
return $this->childes[$val->path]; return $val;
} }
} }
return null; return null;
+18 -26
View File
@@ -30,7 +30,7 @@ defined('ROUTER_HASH') or define('ROUTER_HASH', 2);
class Router extends HttpService implements RouterInterface class Router extends HttpService implements RouterInterface
{ {
/** @var Node[] $nodes */ /** @var Node[] $nodes */
public static array $nodes = []; public array $nodes = [];
public array $groupTacks = []; public array $groupTacks = [];
public ?string $dir = 'App\\Http\\Controllers'; public ?string $dir = 'App\\Http\\Controllers';
@@ -44,10 +44,6 @@ class Router extends HttpService implements RouterInterface
public int $useTree = ROUTER_TREE; public int $useTree = ROUTER_TREE;
private static array $controllers = [];
public ?Response $response = null; public ?Response $response = null;
@@ -91,8 +87,8 @@ class Router extends HttpService implements RouterInterface
*/ */
public function addRoute($path, $handler, string $method = 'any'): ?Node public function addRoute($path, $handler, string $method = 'any'): ?Node
{ {
if (!isset(static::$nodes[$method])) { if (!isset($this->nodes[$method])) {
static::$nodes[$method] = []; $this->nodes[$method] = [];
} }
if ($handler instanceof Closure) { if ($handler instanceof Closure) {
$handler = Closure::bind($handler, di(Controller::class)); $handler = Closure::bind($handler, di(Controller::class));
@@ -110,9 +106,9 @@ class Router extends HttpService implements RouterInterface
private function hash($path, $handler, string $method = 'any'): ?Node private function hash($path, $handler, string $method = 'any'): ?Node
{ {
$path = $this->resolve($path); $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);
} }
@@ -144,11 +140,10 @@ class Router extends HttpService implements RouterInterface
private function tree($path, $handler, string $method = 'any'): Node private function tree($path, $handler, string $method = 'any'): Node
{ {
[$first, $explode] = $this->split($path); [$first, $explode] = $this->split($path);
if (!isset(static::$nodes[$method]['/'])) { if (!isset($this->nodes[$method]['/'])) {
static::$nodes[$method]['/'] = $this->NodeInstance('/', 0, $method); $this->nodes[$method]['/'] = $this->NodeInstance('/', 0, $method);
} }
$parent = $this->nodes[$method]['/'];
$parent = static::$nodes[$method]['/'];
if (!empty($first) && !empty($explode)) { if (!empty($first) && !empty($explode)) {
$parent = $this->bindNode($parent, $explode, $method); $parent = $this->bindNode($parent, $explode, $method);
} }
@@ -168,9 +163,6 @@ class Router extends HttpService implements RouterInterface
{ {
$a = 0; $a = 0;
foreach ($explode as $value) { foreach ($explode as $value) {
if (empty($value)) {
continue;
}
++$a; ++$a;
$search = $parent->findNode($value); $search = $parent->findNode($value);
if ($search === null) { if ($search === null) {
@@ -416,10 +408,10 @@ class Router extends HttpService implements RouterInterface
*/ */
public function tree_search(?array $explode, $method): ?Node public function tree_search(?array $explode, $method): ?Node
{ {
if (!isset(static::$nodes[$method])) { if (!isset($this->nodes[$method])) {
return null; return null;
} }
$parent = static::$nodes[$method]['/']; $parent = $this->nodes[$method]['/'];
while ($value = array_shift($explode)) { while ($value = array_shift($explode)) {
$node = $parent->findNode($value); $node = $parent->findNode($value);
var_dump($node); var_dump($node);
@@ -454,7 +446,7 @@ class Router extends HttpService implements RouterInterface
public function each(): array public function each(): array
{ {
$paths = []; $paths = [];
foreach (static::$nodes as $node) { foreach ($this->nodes as $node) {
/** @var Node[] $node */ /** @var Node[] $node */
foreach ($node as $path => $_node) { foreach ($node as $path => $_node) {
$paths[] = strtoupper($_node->method) . ' : ' . $path; $paths[] = strtoupper($_node->method) . ' : ' . $path;
@@ -485,12 +477,12 @@ class Router extends HttpService implements RouterInterface
{ {
$method = $request->getMethod(); $method = $request->getMethod();
$methods = static::$nodes[$method][\request()->getUri()] ?? null; $methods = $this->nodes[$method][\request()->getUri()] ?? null;
if (!is_null($methods)) { if (!is_null($methods)) {
return $methods; return $methods;
} }
if ($request->isOption) { if ($request->isOption) {
return static::$nodes[$method]['*'] ?? null; return $this->nodes[$method]['*'] ?? null;
} }
return null; return null;
} }
@@ -503,10 +495,10 @@ class Router extends HttpService implements RouterInterface
*/ */
public function search($uri, $method): Node|null public function search($uri, $method): Node|null
{ {
if (!isset(static::$nodes[$method])) { if (!isset($this->nodes[$method])) {
return null; return null;
} }
$methods = static::$nodes[$method]; $methods = $this->nodes[$method];
if (isset($methods[$uri])) { if (isset($methods[$uri])) {
return $methods[$uri]; return $methods[$uri];
} }
@@ -521,13 +513,13 @@ class Router extends HttpService implements RouterInterface
private function search_options($request): ?Node private function search_options($request): ?Node
{ {
$method = $request->getMethod(); $method = $request->getMethod();
if (!isset(static::$nodes[$method])) { if (!isset($this->nodes[$method])) {
return null; return null;
} }
if (!isset(static::$nodes[$method]['*'])) { if (!isset($this->nodes[$method]['*'])) {
return null; return null;
} }
return static::$nodes[$method]['*']; return $this->nodes[$method]['*'];
} }