路由组增加middleware部分注解

This commit is contained in:
TOP糯米 2024-07-27 16:34:08 +08:00
parent a53419328f
commit e3f30a2735
1 changed files with 59 additions and 31 deletions

View File

@ -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, "<?php\nreturn " . $content . ';');
// 创建缓存文件
$this->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, "<?php\nreturn " . $content . ';');
}
}