This commit is contained in:
2021-05-08 10:59:31 +08:00
parent 9bed1836d7
commit f2e5e4c781
2 changed files with 26 additions and 27 deletions
+17 -18
View File
@@ -27,7 +27,7 @@ defined('ROUTER_HASH') or define('ROUTER_HASH', 2);
class Router extends HttpService implements RouterInterface
{
/** @var Node[] $nodes */
public array $nodes = [];
public static 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($this->nodes[$method])) {
$this->nodes[$method] = [];
if (!isset(static::$nodes[$method])) {
static::$nodes[$method] = [];
}
if ($handler instanceof Closure) {
@@ -130,9 +130,9 @@ class Router extends HttpService implements RouterInterface
{
$path = $this->resolve($path);
$this->nodes[$method][$path] = $this->NodeInstance($path, 0, $method);
static::$nodes[$method][$path] = $this->NodeInstance($path, 0, $method);
return $this->nodes[$method][$path]->bindHandler($handler);
return static::$nodes[$method][$path]->bindHandler($handler);
}
@@ -164,9 +164,9 @@ class Router extends HttpService implements RouterInterface
{
list($first, $explode) = $this->split($path);
$parent = $this->nodes[$method][$first] ?? null;
$parent = static::$nodes[$method][$first] ?? null;
if (empty($parent)) {
$this->nodes[$method][$first] = $parent = $this->NodeInstance('/', 0, $method);
static::$nodes[$method][$first] = $parent = $this->NodeInstance('/', 0, $method);
}
if ($first !== '/') {
@@ -298,7 +298,6 @@ 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;
@@ -421,10 +420,10 @@ class Router extends HttpService implements RouterInterface
public function tree_search(?array $explode, $method): ?Node
{
if (empty($explode)) {
return $this->nodes[$method]['/'] ?? null;
return static::$nodes[$method]['/'] ?? null;
}
$first = array_shift($explode);
if (!($parent = $this->nodes[$method][$first] ?? null)) {
if (!($parent = static::$nodes[$method][$first] ?? null)) {
return null;
}
if (empty($explode)) {
@@ -471,7 +470,7 @@ class Router extends HttpService implements RouterInterface
public function each(): array
{
$paths = [];
foreach ($this->nodes as $node) {
foreach (static::$nodes as $node) {
/** @var Node[] $node */
foreach ($node as $path => $_node) {
$paths[] = strtoupper($_node->method) . ' : ' . $path;
@@ -521,10 +520,10 @@ class Router extends HttpService implements RouterInterface
$method = $request->getMethod();
$uri = $request->headers->get('request_uri', '/');
if (!isset($this->nodes[$method])) {
if (!isset(static::$nodes[$method])) {
return null;
}
$methods = $this->nodes[$method];
$methods = static::$nodes[$method];
if (isset($methods[$uri])) {
return $methods[$uri];
}
@@ -542,10 +541,10 @@ class Router extends HttpService implements RouterInterface
*/
public function search($uri, $method): Node|null
{
if (!isset($this->nodes[$method])) {
if (!isset(static::$nodes[$method])) {
return null;
}
$methods = $this->nodes[$method];
$methods = static::$nodes[$method];
if (isset($methods[$uri])) {
return $methods[$uri];
}
@@ -560,13 +559,13 @@ class Router extends HttpService implements RouterInterface
private function search_options($request): ?Node
{
$method = $request->getMethod();
if (!isset($this->nodes[$method])) {
if (!isset(static::$nodes[$method])) {
return null;
}
if (!isset($this->nodes[$method]['*'])) {
if (!isset(static::$nodes[$method]['*'])) {
return null;
}
return $this->nodes[$method]['*'];
return static::$nodes[$method]['*'];
}