function findByArray($paths, $params, &$matched_path = null)
{
$paths = array_filter($paths, function ($i) {
return $i !== '/' && !empty($i);
});
array_unshift($paths, '/');
$paths[] = Tree::$end;
//最后总是插入Tree::$end表示路径结束
$route = null;
$valid_path = null;
//最深一个不为空的路径
$visited = 0;
$geted = 0;
$walked_path = array();
//找到匹配的最深路径,/a/b/c匹配的路径可以是/,/a,/a/c,/a/b/c,但最深的是/a/b/c
$this->routes->visit($paths, function ($node, $value) use(&$valid_path, &$route, &$visited, &$geted, &$walked_path) {
if (!is_null($value)) {
$route = $value;
if (!$route instanceof \phprs\util\Tree) {
$valid_path = $value;
}
$geted = $visited;
}
$walked_path[] = $node;
$visited++;
return true;
});
if (is_null($route)) {
return null;
}
if ($matched_path !== null) {
$matched_path = $walked_path;
}
//如果匹配完整的路径, 则还需要匹配querystring参数
if (count($paths) == $geted + 1) {
if ($route instanceof \phprs\util\Tree) {
//条件要求有querystring,所以请求也需要有querystring
if (empty($params)) {
return $valid_path;
}
$found = $this->findParams($route, $params);
return is_null($found) ? $valid_path : $found;
} else {
return $route;
}
} else {
//如果不匹配全路径,则只能处理没有querystring的条件
if ($route instanceof \phprs\util\Tree) {
return $valid_path;
}
return $route;
}
return null;
}