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])) {
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;
+9 -17
View File
@@ -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) {
+1 -1
View File
@@ -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('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404);
}