Incorporates another Routable's routes into this one's.
If hooked
is set to true
and a Service is provided,
then that service will be wired to a HookedService proxy.
If a middlewareNamespace
is provided, then any middleware
from the provided Routable will be prefixed by that namespace,
with a dot.
For example, if the Routable has a middleware 'y', and the middlewareNamespace
is 'x', then that middleware will be available as 'x.y' in the main application.
These namespaces can be nested.
Source
use(Pattern path, Routable routable,
{bool hooked: true, String middlewareNamespace: null}) {
Routable _routable = routable;
// If we need to hook this service, do it here. It has to be first, or
// else all routes will point to the old service.
if (_routable is Service) {
Hooked hookedDeclaration = _getAnnotation(_routable, Hooked);
Service service = (hookedDeclaration != null || hooked)
? new HookedService(_routable)
: _routable;
services[path.toString().trim().replaceAll(
new RegExp(r'(^\/+)|(\/+$)'), '')] = service;
_routable = service;
}
requestMiddleware.addAll(_routable.requestMiddleware);
for (Route route in _routable.routes) {
Route provisional = new Route('', path);
if (route.path == '/') {
route.path = '';
route.matcher = new RegExp(r'^\/?$');
}
route.matcher = new RegExp(route.matcher.pattern.replaceAll(
new RegExp('^\\^'),
provisional.matcher.pattern.replaceAll(new RegExp(r'\$$'), '')));
route.path = "$path${route.path}";
routes.add(route);
}
// Let's copy middleware, heeding the optional middleware namespace.
String middlewarePrefix = "";
if (middlewareNamespace != null)
middlewarePrefix = "$middlewareNamespace.";
for (String middlewareName in _routable.requestMiddleware.keys) {
requestMiddleware["$middlewarePrefix$middlewareName"] =
_routable.requestMiddleware[middlewareName];
}
// Copy services, too. :)
for (Pattern servicePath in _routable.services.keys) {
String newServicePath = path.toString().trim().replaceAll(
new RegExp(r'(^\/+)|(\/+$)'), '') + '/$servicePath';
services[newServicePath] = _routable.services[servicePath];
}
}