applyRoutes method
Applies the routes from this Controller to some router.
When reflection is disabled (see canReflect), or the reflector has no
information about this class, only the routes added in
configureRoutes are applied; @Expose methods are not discovered.
Implementation
Future<String> applyRoutes(
Router<RequestHandler> router,
Reflector reflector,
) async {
// Load global expose decl
var exposeDecl = findExpose(reflector);
if (exposeDecl == null) {
throw Exception('All controllers must carry an @Expose() declaration.');
}
var routable = Routable();
_mountPoint = router.mount(exposeDecl.path, routable);
await configureRoutes(routable);
String name;
// A reflector without information about this class (e.g. a
// StaticReflector) is treated like disabled reflection: only the routes
// added in configureRoutes are applied.
var classMirror = canReflect(reflector)
? tryReflect(() => reflector.reflectClass(runtimeType))
: null;
var logger = _app?.logger ?? (router is Angel ? router.logger : null);
if (logger != null) {
warnIfUndescribed(
reflector,
runtimeType,
logger,
'@Expose routes and @Middleware annotations',
);
}
var instanceMirror = classMirror == null
? null
: tryReflect(() => reflector.reflectInstance(this));
if (classMirror != null && instanceMirror != null) {
// Pre-reflect methods
final handlers = <RequestHandler>[
...exposeDecl.middleware,
...middleware,
];
classMirror.declarations.forEach(
_routeBuilder(reflector, instanceMirror, routable, handlers),
);
name = classMirror.name;
} else {
name = runtimeType.toString();
}
// Return the name.
return exposeDecl.as?.isNotEmpty == true ? exposeDecl.as! : name;
}