From e3f30a2735c760f97297181a4bbf5582f61d00ea Mon Sep 17 00:00:00 2001 From: topnuomi <1130395124@qq.com> Date: Sat, 27 Jul 2024 16:34:08 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B7=AF=E7=94=B1=E7=BB=84=E5=A2=9E=E5=8A=A0mi?= =?UTF-8?q?ddleware=E9=83=A8=E5=88=86=E6=B3=A8=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/library/Router.php | 90 +++++++++++++++++++++++------------- 1 file changed, 59 insertions(+), 31 deletions(-) diff --git a/framework/library/Router.php b/framework/library/Router.php index 0fe172f..2468539 100644 --- a/framework/library/Router.php +++ b/framework/library/Router.php @@ -387,12 +387,22 @@ class Router $files = scandir($controllerPath); for ($i = 2; $i < count($files); $i++) { $className = $namespace . '\\' . pathinfo($files[$i])['filename']; - // 获取组名 + // 获取路由组名称 $groupName = ''; $clsasAnnotation = Annotation::getClassAnnotation($className); if (isset($clsasAnnotation['group']) && ($group = trim($clsasAnnotation['group'])) != '') { $groupName = '/' . trim($group, '/'); } + // 路由组接受的中间件 + $groupAcceptMiddleware = []; + if (isset($clsasAnnotation['acceptMiddleware']) && $clsasAnnotation['acceptMiddleware'] != '') { + $groupAcceptMiddleware = explode('|', $clsasAnnotation['acceptMiddleware']); + } + // 路由组拒绝的中间件 + $groupExceptMiddleware = []; + if (isset($clsasAnnotation['exceptMiddleware']) && $clsasAnnotation['exceptMiddleware'] != '') { + $groupExceptMiddleware = explode('|', $clsasAnnotation['exceptMiddleware']); + } $reflectionClass = Application::getReflectionClass($className); foreach ($reflectionClass->getMethods() as $method) { if ($method->class == $className && substr($method->name, 0, 1) != '_') { @@ -406,50 +416,68 @@ class Router $result[$requestMethod][$rule] = [ 'class' => $className, 'method' => $method->name, - 'except_middleware' => [], 'accept_middleware' => [], + 'except_middleware' => [], ]; - if (isset($annotation['exceptMiddleware']) && $annotation['exceptMiddleware'] != '') { - foreach (explode('|', $annotation['exceptMiddleware']) as $exceptMiddleware) { - $result[$requestMethod][$rule]['except_middleware'][] = $exceptMiddleware; - } - } if (isset($annotation['acceptMiddleware']) && $annotation['acceptMiddleware'] != '') { foreach (explode('|', $annotation['acceptMiddleware']) as $acceptMiddleware) { $result[$requestMethod][$rule]['accept_middleware'][] = $acceptMiddleware; } + } else { + $result[$requestMethod][$rule]['accept_middleware'] = $groupAcceptMiddleware; } - } - } - } - - // 加载配置文件中的路由配置 - $routeConfigFile = CONFIG_DIR . 'route.php'; - if (is_file($routeConfigFile)) { - $routeConfig = require $routeConfigFile; - foreach ($routeConfig as $key => $value) { - if (isset($result[$key])) { // 存在当前请求方法的配置就检查含有的路由配置 - foreach ($value as $uri => $config) { - $uri = ($uri == '/') ? $uri : trim($uri, '/'); - if (isset($result[$key][$uri])) { // 如果已经存在这个路由配置,可能不完全,直接合并覆盖已有项 - $result[$key][$uri] = array_merge($result[$key][$uri], $config); - } else { - $result[$key][$uri] = $config; + if (isset($annotation['exceptMiddleware']) && $annotation['exceptMiddleware'] != '') { + foreach (explode('|', $annotation['exceptMiddleware']) as $exceptMiddleware) { + $result[$requestMethod][$rule]['except_middleware'][] = $exceptMiddleware; } + } else { + $result[$requestMethod][$rule]['except_middleware'] = $groupExceptMiddleware; } - } else { - $result[$key] = $value; } } } - // 写入文件 - ob_start(); - var_export($result); - $content = ob_get_contents(); - ob_clean(); - file_put_contents($fileName, "createCacheFile($fileName, $result); + return $result; } } + + /** + * 创建缓存文件 + * @param string $fileName + * @param array $rules + * @return void + */ + private function createCacheFile($fileName, $rules) + { + // 加载配置文件中的路由配置 + $routeConfigFile = CONFIG_DIR . 'route.php'; + if (is_file($routeConfigFile)) { + $routeConfig = require $routeConfigFile; + foreach ($routeConfig as $key => $value) { + if (isset($rules[$key])) { // 存在当前请求方法的配置就检查含有的路由配置 + foreach ($value as $uri => $config) { + $uri = ($uri == '/') ? $uri : trim($uri, '/'); + if (isset($rules[$key][$uri])) { // 如果已经存在这个路由配置,可能不完全,直接合并覆盖已有项 + $rules[$key][$uri] = array_merge($rules[$key][$uri], $config); + } else { + $rules[$key][$uri] = $config; + } + } + } else { + $rules[$key] = $value; + } + } + } + + // 写入文件 + ob_start(); + var_export($rules); + $content = ob_get_contents(); + ob_clean(); + file_put_contents($fileName, "