From 1985bf4e38445dd5e7828497f9946216e162b0fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Thu, 5 Aug 2021 11:19:12 +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 | 6 +++--- HttpServer/Route/Router.php | 26 +++++++++----------------- Server/HTTPServerListener.php | 2 +- 3 files changed, 13 insertions(+), 21 deletions(-) diff --git a/HttpServer/Route/Node.php b/HttpServer/Route/Node.php index 2068214f..b9f1d258 100644 --- a/HttpServer/Route/Node.php +++ b/HttpServer/Route/Node.php @@ -285,9 +285,9 @@ class Node extends HttpService if (isset($this->childes[$search])) { return $this->childes[$search]; } - foreach ($this->childes as $key => $val) { - if ($search == $key) { - return $this->childes[$key]; + foreach ($this->childes as $val) { + if ($search == $val->path) { + return $this->childes[$val->path]; } } return null; diff --git a/HttpServer/Route/Router.php b/HttpServer/Route/Router.php index 0cf762c4..3cc39f6b 100644 --- a/HttpServer/Route/Router.php +++ b/HttpServer/Route/Router.php @@ -96,7 +96,7 @@ class Router extends HttpService implements RouterInterface if ($handler instanceof Closure) { $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 { - list($first, $explode) = $this->split($path); + [$first, $explode] = $this->split($path); $parent = static::$nodes[$method][$first] ?? null; if (empty($parent)) { @@ -442,26 +442,18 @@ class Router extends HttpService implements RouterInterface /** * @param $path * @return array - * '*' */ public function split($path): array { - $prefix = $this->addPrefix(); - $path = ltrim($path, '/'); - if (!empty($prefix)) { - $path = $prefix . '/' . $path; - } - - $explode = array_filter(explode('/', $path)); - if (empty($explode)) { + $path = $this->addPrefix() . '/' . ltrim($path, '/'); + if ($path === '/') { return ['/', []]; } - - $first = array_shift($explode); - if (empty($explode)) { - $explode = []; + $filter = array_filter(explode('/', $path)); + if (empty($filter)) { + return ['/', []]; } - return [$first, $explode]; + return [array_shift($filter), $filter]; } /** @@ -552,7 +544,7 @@ class Router extends HttpService implements RouterInterface * @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()); if ($node instanceof Node) { diff --git a/Server/HTTPServerListener.php b/Server/HTTPServerListener.php index ba677bb2..0678ded8 100644 --- a/Server/HTTPServerListener.php +++ b/Server/HTTPServerListener.php @@ -117,7 +117,7 @@ class HTTPServerListener extends Abstracts\Server public function onRequest(Request $request, Response $response) { try { - $node = $this->router->find_path(HSRequest::create($request)); + $node = $this->router->Branch_search(HSRequest::create($request)); if (!($node instanceof Node)) { throw new RequestException('

HTTP 404 Not Found


Powered by Swoole', 404); }