This commit is contained in:
2021-08-05 11:39:04 +08:00
parent 1fc24197af
commit 40a7d8f123
2 changed files with 11 additions and 17 deletions
-1
View File
@@ -194,7 +194,6 @@ class Request extends HttpService
public function getExplode(): array public function getExplode(): array
{ {
return Context::getContext(\Swoole\Http\Request::class)->explode; return Context::getContext(\Swoole\Http\Request::class)->explode;
return explode('/', $this->getUri());
} }
/** /**
+11 -16
View File
@@ -144,13 +144,12 @@ 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]['/'])) {
$parent = static::$nodes[$method][$first] ?? null; static::$nodes[$method][$first] = $this->NodeInstance('/', 0, $method);
if (empty($parent)) {
static::$nodes[$method][$first] = $parent = $this->NodeInstance('/', 0, $method);
} }
if ($first !== '/') { $parent = static::$nodes[$method]['/'];
if (!empty($first) && !empty($explode)) {
$parent = $this->bindNode($parent, $explode, $method); $parent = $this->bindNode($parent, $explode, $method);
} }
$parent->path = $path; $parent->path = $path;
@@ -168,15 +167,11 @@ class Router extends HttpService implements RouterInterface
private function bindNode(Node $parent, array $explode, $method): Node private function bindNode(Node $parent, array $explode, $method): Node
{ {
$a = 0; $a = 0;
if (empty($explode)) {
return $parent->addChild($this->NodeInstance('/', $a, $method), '/');
}
foreach ($explode as $value) { foreach ($explode as $value) {
if (empty($value)) { if (empty($value)) {
continue; continue;
} }
++$a; ++$a;
$search = $parent->findNode($value); $search = $parent->findNode($value);
if ($search === null) { if ($search === null) {
$parent = $parent->addChild($this->NodeInstance($value, $a, $method), $value); $parent = $parent->addChild($this->NodeInstance($value, $a, $method), $value);
@@ -425,7 +420,6 @@ class Router extends HttpService implements RouterInterface
return null; return null;
} }
$parent = static::$nodes[$method]['/']; $parent = static::$nodes[$method]['/'];
var_dump($parent);
while ($value = array_shift($explode)) { while ($value = array_shift($explode)) {
$node = $parent->findNode($value); $node = $parent->findNode($value);
if (!$node) { if (!$node) {
@@ -438,19 +432,19 @@ class Router extends HttpService implements RouterInterface
/** /**
* @param $path * @param $path
* @return array * @return array|null
*/ */
public function split($path): array public function split($path): ?array
{ {
$path = $this->addPrefix() . '/' . ltrim($path, '/'); $path = $this->addPrefix() . '/' . ltrim($path, '/');
if ($path === '/') { if ($path === '/') {
return ['/', []]; return ['', null];
} }
$filter = array_filter(explode('/', $path)); $filter = array_filter(explode('/', $path));
if (empty($filter)) { if (!empty($filter)) {
return ['/', []]; return [array_shift($filter), $filter];
} }
return [array_shift($filter), $filter]; return ['', null];
} }
/** /**
@@ -540,6 +534,7 @@ class Router extends HttpService implements RouterInterface
* @param Request $request * @param Request $request
* @return Node|null * @return Node|null
* 树杈搜索 * 树杈搜索
* @throws RequestException
*/ */
public function Branch_search(Request $request): ?Node public function Branch_search(Request $request): ?Node
{ {