Split Controller.configureServer
This commit is contained in:
parent
07d1ce0944
commit
97d52a6984
2 changed files with 19 additions and 11 deletions
|
@ -1,5 +1,6 @@
|
||||||
# 2.0.2
|
# 2.0.2
|
||||||
* Make `ResponseContext` *explicitly* implement `StreamConsumer` (though technically it already did???)
|
* Make `ResponseContext` *explicitly* implement `StreamConsumer` (though technically it already did???)
|
||||||
|
* Split `Controller.configureServer` to create `Controller.applyRoutes`.
|
||||||
|
|
||||||
# 2.0.1
|
# 2.0.1
|
||||||
* Tracked down a bug in `Driver.runPipeline` that allowed fallback
|
* Tracked down a bug in `Driver.runPipeline` that allowed fallback
|
||||||
|
|
|
@ -35,31 +35,37 @@ class Controller {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return applyRoutes(app, app.container.reflector).then((name) {
|
||||||
|
app.controllers[name] = this;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Applies the routes from this [Controller] to some [router].
|
||||||
|
Future<String> applyRoutes(Router router, Reflector reflector) {
|
||||||
// Load global expose decl
|
// Load global expose decl
|
||||||
var classMirror = app.container.reflector.reflectClass(this.runtimeType);
|
var classMirror = reflector.reflectClass(this.runtimeType);
|
||||||
Expose exposeDecl = findExpose(app.container.reflector);
|
Expose exposeDecl = findExpose(reflector);
|
||||||
|
|
||||||
if (exposeDecl == null) {
|
if (exposeDecl == null) {
|
||||||
throw Exception("All controllers must carry an @Expose() declaration.");
|
throw Exception("All controllers must carry an @Expose() declaration.");
|
||||||
}
|
}
|
||||||
|
|
||||||
var routable = Routable();
|
var routable = Routable();
|
||||||
app.mount(exposeDecl.path, routable);
|
router.mount(exposeDecl.path, routable);
|
||||||
var typeMirror = app.container.reflector.reflectType(this.runtimeType);
|
var typeMirror = reflector.reflectType(this.runtimeType);
|
||||||
String name =
|
|
||||||
exposeDecl.as?.isNotEmpty == true ? exposeDecl.as : typeMirror.name;
|
|
||||||
|
|
||||||
app.controllers[name] = this;
|
|
||||||
|
|
||||||
// Pre-reflect methods
|
// Pre-reflect methods
|
||||||
var instanceMirror = app.container.reflector.reflectInstance(this);
|
var instanceMirror = reflector.reflectInstance(this);
|
||||||
final handlers = <RequestHandler>[]
|
final handlers = <RequestHandler>[]
|
||||||
..addAll(exposeDecl.middleware)
|
..addAll(exposeDecl.middleware)
|
||||||
..addAll(middleware);
|
..addAll(middleware);
|
||||||
final routeBuilder = _routeBuilder(instanceMirror, routable, handlers);
|
final routeBuilder = _routeBuilder(instanceMirror, routable, handlers);
|
||||||
classMirror.declarations.forEach(routeBuilder);
|
classMirror.declarations.forEach(routeBuilder);
|
||||||
configureRoutes(routable);
|
configureRoutes(routable);
|
||||||
return Future.value();
|
|
||||||
|
// Return the name.
|
||||||
|
return Future.value(
|
||||||
|
exposeDecl.as?.isNotEmpty == true ? exposeDecl.as : typeMirror.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Function(ReflectedDeclaration) _routeBuilder(
|
void Function(ReflectedDeclaration) _routeBuilder(
|
||||||
|
@ -106,7 +112,8 @@ class Controller {
|
||||||
var injection = preInject(reflectedMethod, app.container.reflector);
|
var injection = preInject(reflectedMethod, app.container.reflector);
|
||||||
|
|
||||||
if (exposeDecl?.allowNull?.isNotEmpty == true) {
|
if (exposeDecl?.allowNull?.isNotEmpty == true) {
|
||||||
injection.optional?.addAll(exposeDecl.allowNull);}
|
injection.optional?.addAll(exposeDecl.allowNull);
|
||||||
|
}
|
||||||
|
|
||||||
routeMappings[name] = routable.addRoute(exposeDecl.method,
|
routeMappings[name] = routable.addRoute(exposeDecl.method,
|
||||||
exposeDecl.path, handleContained(reflectedMethod, injection),
|
exposeDecl.path, handleContained(reflectedMethod, injection),
|
||||||
|
|
Loading…
Reference in a new issue