private function isRoute(&$pathInfo)
{
empty($pathInfo) && ($pathInfo[0] = '/');
//网站根地址
$isSuccess = [];
$route = self::$rules;
$httpMethod = isset($_POST['_method']) ? strtoupper($_POST['_method']) : strtoupper($_SERVER['REQUEST_METHOD']);
switch ($httpMethod) {
case 'GET':
$rMethod = self::REQUEST_METHOD_GET;
break;
case 'POST':
$rMethod = self::REQUEST_METHOD_POST;
break;
case 'PUT':
$rMethod = self::REQUEST_METHOD_PUT;
break;
case 'PATCH':
$rMethod = self::REQUEST_METHOD_PATCH;
break;
case 'DELETE':
$rMethod = self::REQUEST_METHOD_DELETE;
break;
case 'OPTIONS':
$rMethod = self::REQUEST_METHOD_OPTIONS;
break;
default:
$rMethod = self::REQUEST_METHOD_ANY;
}
foreach ($route as $k => $v) {
$rulesMethod = substr($k, 0, 1);
if ($rulesMethod != $rMethod && $rulesMethod != self::REQUEST_METHOD_ANY && $rulesMethod != self::REST_ROUTE) {
//此条路由不符合当前请求方式
continue;
}
unset($v);
$singleRule = substr($k, 1);
$arr = $singleRule === '/' ? [$singleRule] : explode('/', ltrim($singleRule, '/'));
if ($arr[0] == $pathInfo[0]) {
array_shift($arr);
foreach ($arr as $key => $val) {
if (isset($pathInfo[$key + 1]) && $pathInfo[$key + 1] !== '') {
if (strpos($val, '\\d') && !is_numeric($pathInfo[$key + 1])) {
//数字变量
$route[$k] = false;
//匹配失败
break 1;
} elseif (strpos($val, ':') === false && $val != $pathInfo[$key + 1]) {
//字符串
$route[$k] = false;
//匹配失败
break 1;
}
} else {
$route[$k] = false;
//匹配失败
break 1;
}
}
} else {
$route[$k] = false;
//匹配失败
}
if ($route[$k] !== false) {
//匹配成功的路由
$isSuccess[] = $k;
}
}
if (empty($isSuccess)) {
$returnArr[0] = false;
} else {
//匹配到多条路由时 选择最长的一条(匹配更精确)
usort($isSuccess, function ($item1, $item2) {
return strlen($item1) >= strlen($item2) ? 0 : 1;
});
if (is_callable($route[$isSuccess[0]])) {
call_user_func($route[$isSuccess[0]]);
Cml::cmlStop();
}
is_array($route[$isSuccess[0]]) || ($route[$isSuccess[0]] = trim(str_replace('\\', '/', $route[$isSuccess[0]]), '/'));
//判断路由的正确性
if (!is_array($route[$isSuccess[0]]) && count(explode('/', $route[$isSuccess[0]])) < 2) {
throw new \InvalidArgumentException(Lang::get('_ROUTE_PARAM_ERROR_', substr($isSuccess[0], 1)));
}
$returnArr[0] = true;
$successRoute = explode('/', $isSuccess[0]);
foreach ($successRoute as $key => $val) {
$t = explode('\\d', $val);
if (strpos($t[0], ':') !== false) {
$_GET[ltrim($t[0], ':')] = $pathInfo[$key];
}
unset($pathInfo[$key]);
}
if (substr($isSuccess[0], 0, 1) == self::REST_ROUTE) {
$actions = explode('/', $route[$isSuccess[0]]);
$arrKey = count($actions) - 1;
$actions[$arrKey] = strtolower($httpMethod) . ucfirst($actions[$arrKey]);
$route[$isSuccess[0]] = implode('/', $actions);
}
$returnArr['route'] = $route[$isSuccess[0]];
}
return $returnArr;
}