feat: SimApiControllerScanner 增加 ISimApiAuthChecker 自动扫描

scanAuthCheckers():扫描调用者包及其子包中 ISimApiAuthChecker 的非抽象实现类
(复用控制器扫描的 PackageInfo 枚举机制,对齐 C# Assembly.GetTypes 扫描)
This commit is contained in:
2026-08-16 22:47:04 +08:00
parent 6cb79cf970
commit c591387288
+48
View File
@@ -15,6 +15,7 @@ import std.collection.*
import std.core.*
import std.reflect.*
import soulsoft_web_mvc.core.*
import simapi.interfaces.*
/**
* 控制器自动扫描器:从调用栈定位调用者包,枚举该包(含子包)中继承 Controller 的类型。
@@ -33,6 +34,18 @@ public class SimApiControllerScanner {
result.toArray()
}
/**
* 扫描调用者包及其所有子包中 ISimApiAuthChecker 的实现类。
* 对齐 C# AddSimApi 中遍历调用者程序集 AddScoped 注册 checker 的机制。
* @return 找到的 checker 实现类型列表(不含抽象类型与接口本身)。
*/
public static func scanAuthCheckers(): Array<TypeInfo> {
let callerPackage = getCallerPackage()
var result = ArrayList<TypeInfo>()
collectImplementations(callerPackage, TypeInfo.of<ISimApiAuthChecker>(), result)
result.toArray()
}
/**
* 获取调用者(应用)包名:遍历栈帧,跳过 simapi/soulsoft/std 等框架包,
* 返回第一个应用包的 declaringClass(对齐 C# 通过 StackTrace 找调用程序集)。
@@ -101,4 +114,39 @@ public class SimApiControllerScanner {
}
false
}
/// 收集指定包及其子包中实现指定接口的非抽象类
private static func collectImplementations(packageName: String, interfaceType: TypeInfo,
result: ArrayList<TypeInfo>): Unit {
if (packageName.isEmpty()) {
return
}
try {
let info = PackageInfo.get(packageName)
for (ti in info.typeInfos) {
if (isImplementation(ti, interfaceType)) {
result.add(ti)
}
}
for (sub in info.subPackages) {
collectImplementations("${packageName}.${sub.name}", interfaceType, result)
}
} catch (_: Exception) {
// 包不存在时跳过
}
}
/// 判断类型是否为接口的非抽象实现类
private static func isImplementation(typeInfo: TypeInfo, interfaceType: TypeInfo): Bool {
if (let classTypeInfo: ClassTypeInfo <- typeInfo) {
if (classTypeInfo.isAbstract()) {
return false
}
if (typeInfo == interfaceType) {
return false
}
return typeInfo.isSubtypeOf(interfaceType)
}
false
}
}