This commit is contained in:
2021-08-05 11:19:12 +08:00
parent a79997e6cc
commit 1985bf4e38
3 changed files with 13 additions and 21 deletions
+3 -3
View File
@@ -285,9 +285,9 @@ class Node extends HttpService
if (isset($this->childes[$search])) { if (isset($this->childes[$search])) {
return $this->childes[$search]; return $this->childes[$search];
} }
foreach ($this->childes as $key => $val) { foreach ($this->childes as $val) {
if ($search == $key) { if ($search == $val->path) {
return $this->childes[$key]; return $this->childes[$val->path];
} }
} }
return null; return null;
+9 -17
View File
@@ -96,7 +96,7 @@ class Router extends HttpService implements RouterInterface
if ($handler instanceof Closure) { if ($handler instanceof Closure) {
$handler = Closure::bind($handler, di(Controller::class)); $handler = Closure::bind($handler, di(Controller::class));
} }
return $this->hash($path, $handler, $method); return $this->tree($path, $handler, $method);
} }
@@ -142,7 +142,7 @@ class Router extends HttpService implements RouterInterface
*/ */
private function tree($path, $handler, string $method = 'any'): Node private function tree($path, $handler, string $method = 'any'): Node
{ {
list($first, $explode) = $this->split($path); [$first, $explode] = $this->split($path);
$parent = static::$nodes[$method][$first] ?? null; $parent = static::$nodes[$method][$first] ?? null;
if (empty($parent)) { if (empty($parent)) {
@@ -442,26 +442,18 @@ class Router extends HttpService implements RouterInterface
/** /**
* @param $path * @param $path
* @return array * @return array
* '*'
*/ */
public function split($path): array public function split($path): array
{ {
$prefix = $this->addPrefix(); $path = $this->addPrefix() . '/' . ltrim($path, '/');
$path = ltrim($path, '/'); if ($path === '/') {
if (!empty($prefix)) {
$path = $prefix . '/' . $path;
}
$explode = array_filter(explode('/', $path));
if (empty($explode)) {
return ['/', []]; return ['/', []];
} }
$filter = array_filter(explode('/', $path));
$first = array_shift($explode); if (empty($filter)) {
if (empty($explode)) { return ['/', []];
$explode = [];
} }
return [$first, $explode]; return [array_shift($filter), $filter];
} }
/** /**
@@ -552,7 +544,7 @@ class Router extends HttpService implements RouterInterface
* @return Node|null * @return Node|null
* 树杈搜索 * 树杈搜索
*/ */
private function Branch_search(Request $request): ?Node public function Branch_search(Request $request): ?Node
{ {
$node = $this->tree_search($request->getExplode(), $request->getMethod()); $node = $this->tree_search($request->getExplode(), $request->getMethod());
if ($node instanceof Node) { if ($node instanceof Node) {
+1 -1
View File
@@ -117,7 +117,7 @@ class HTTPServerListener extends Abstracts\Server
public function onRequest(Request $request, Response $response) public function onRequest(Request $request, Response $response)
{ {
try { try {
$node = $this->router->find_path(HSRequest::create($request)); $node = $this->router->Branch_search(HSRequest::create($request));
if (!($node instanceof Node)) { if (!($node instanceof Node)) {
throw new RequestException('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404); throw new RequestException('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404);
} }