This commit is contained in:
Tobe O 2017-11-26 21:21:19 -05:00
parent 685205adde
commit b08bca3d6f
9 changed files with 297 additions and 635 deletions

View file

@ -66,25 +66,6 @@ needs a lot of flexibility with which to handle requests.
## Hierarchy
```dart
main() {
final foo = new Route('/');
final bar = foo.child('bar');
final baz = foo.child('baz');
final a = bar.child('a');
/*
* Relative paths:
* a.resolve('../baz') = baz;
* bar.resolve('a') = a;
*
* Absolute paths:
* a.resolve('/bar/a') = a;
*/
}
```
```dart
main() {
final router = new Router();

165
lib/src/grammar.dart Normal file
View file

@ -0,0 +1,165 @@
part of angel_route.src.router;
class _RouteGrammar {
static final Parser<String> notSlash =
match(new RegExp(r'[^/]+')).value((r) => r.span.text);
static final Parser<RegExp> regExp = new _RegExpParser();
static final Parser<String> parameterName =
match(new RegExp(r':([A-Za-z0-9_]+)'))
.value((r) => r.span.text.substring(1));
static final Parser<_ParameterSegment> parameterSegment = chain([
parameterName,
match('?').value((r) => true).opt(),
regExp.opt(),
]).map((r) {
var s = new _ParameterSegment(r.value[0], r.value[2]);
return r.value[1] == true ? new _OptionalSegment(s) : s;
});
static final Parser<_WildcardSegment> wildcardSegment =
match('*').value((r) => new _WildcardSegment());
static final Parser<_ConstantSegment> constantSegment =
notSlash.map((r) => new _ConstantSegment(r.value));
static final Parser<_RouteSegment> routeSegment =
any([parameterSegment, wildcardSegment, constantSegment]);
static final Parser<_RouteDefinition> routeDefinition = routeSegment
.separatedBy(match('/'))
.map((r) => new _RouteDefinition(r.value ?? []))
.surroundedBy(match('/').star().opt());
}
class _RegExpParser extends Parser<RegExp> {
static final RegExp rgx = new RegExp(r'\((.+)\)');
@override
ParseResult<RegExp> parse(SpanScanner scanner, [int depth = 1]) {
if (!scanner.matches(rgx)) return new ParseResult(this, false, []);
return new ParseResult(this, true, [],
span: scanner.lastSpan, value: new RegExp(scanner.lastMatch[1]));
}
}
class _RouteDefinition {
final List<_RouteSegment> segments;
_RouteDefinition(this.segments);
Parser<Map<String, String>> compile() {
Parser<Map<String, String>> out;
for (var s in segments) {
if (out == null)
out = s.compile();
else
out = s.compileNext(out.then(match('/')).index(0));
}
return out;
}
}
abstract class _RouteSegment {
Parser<Map<String, String>> compile();
Parser<Map<String, String>> compileNext(Parser<Map<String, String>> p);
}
class _ConstantSegment extends _RouteSegment {
final String text;
_ConstantSegment(this.text);
@override
String toString() {
return 'Constant: $text';
}
@override
Parser<Map<String, String>> compile() {
return match(text).value((r) => {});
}
@override
Parser<Map<String, String>> compileNext(Parser<Map<String, String>> p) {
return p.then(compile()).index(0);
}
}
class _WildcardSegment extends _RouteSegment {
@override
String toString() {
return 'Wildcard segment';
}
Parser<Map<String, String>> _compile() {
return match(new RegExp(r'[^/]*'));
}
@override
Parser<Map<String, String>> compile() {
return _compile().map((r) => {});
}
@override
Parser<Map<String, String>> compileNext(Parser<Map<String, String>> p) {
return p.then(_compile()).index(0);
}
}
class _OptionalSegment extends _ParameterSegment {
final _ParameterSegment parameter;
_OptionalSegment(this.parameter) : super(parameter.name, parameter.regExp);
@override
String toString() {
return 'Optional: $parameter';
}
@override
Parser<Map<String, String>> compile() {
return super.compile().opt();
}
@override
Parser<Map<String, String>> compileNext(Parser<Map<String, String>> p) {
return p.then(_compile().opt()).map((r) {
if (r.value[1] == null) return r.value[0];
return r.value[0]..addAll({name: Uri.decodeComponent(r.value[1])});
});
}
}
class _ParameterSegment extends _RouteSegment {
final String name;
final RegExp regExp;
_ParameterSegment(this.name, this.regExp);
@override
String toString() {
if (regExp != null) return 'Param: $name (${regExp.pattern})';
return 'Param: $name';
}
Parser<Map<String, String>> _compile() {
return regExp != null
? match(regExp).value((r) => r.span.text)
: _RouteGrammar.notSlash;
}
@override
Parser<Map<String, String>> compile() {
return _compile().map((r) => {name: Uri.decodeComponent(r.span.text)});
}
@override
Parser<Map<String, String>> compileNext(Parser<Map<String, String>> p) {
return p.then(_compile()).map((r) {
return r.value[0]..addAll({name: Uri.decodeComponent(r.value[1])});
});
}
}

View file

@ -1,529 +1,64 @@
part of angel_route.src.router;
String _matcherify(String path, {bool expand: true}) {
var p = path.replaceAll(new RegExp(r'/\*$'), "*").replaceAll('/', r'\/');
if (expand) {
var match = _param.firstMatch(p);
while (match != null) {
if (match.group(3) == null)
p = p.replaceAll(match[0], '([^\/]+)');
else
p = p.replaceAll(match[0], '(${match[3]})');
match = _param.firstMatch(p);
}
}
p = p.replaceAll(new RegExp('\\*'), '.*');
p = '^$p\$';
return p;
}
String _pathify(String path) {
var p = path.replaceAll(_straySlashes, '');
Map<String, String> replace = {};
for (Match match in _param.allMatches(p)) {
if (match[3] != null) replace[match[0]] = ':${match[1]}';
}
replace.forEach((k, v) {
p = p.replaceAll(k, v);
});
return p;
}
/// Represents a virtual location within an application.
class Route {
final Map<String, Match> _cache = {};
final List<Route> _children = [];
final List _handlers = [];
RegExp _head;
RegExp _matcher;
String _method;
String _name;
Route _parent;
RegExp _parentResolver;
String _path;
String _pathified;
RegExp _resolver;
RegExp _stub;
final String method;
final String path;
final List handlers;
final Map<String, Map<String, String>> _cache = {};
final _RouteDefinition _routeDefinition;
String name;
Parser<Map<String, String>> _parser;
/// Set to `true` to print verbose debug output when interacting with this route.
bool debug;
/// Contains any child routes attached to this one.
List<Route> get children => new List.unmodifiable(_children);
/// A `List` of arbitrary objects chosen to respond to this request.
List get handlers => new List.unmodifiable(_handlers);
/// A `RegExp` that matches requests to this route.
RegExp get matcher => _matcher;
/// The HTTP method this route is designated for.
String get method => _method;
/// The name of this route, if any.
String get name => _name;
/// The hierarchical parent of this route.
Route get parent => _parent;
/// The virtual path on which this route is mounted.
String get path => _path;
/// The [Route] at the top of the hierarchy this route is found in.
Route get absoluteParent {
Route result = this;
while (result.parent != null) result = result.parent;
return result;
Route(this.path, {@required this.method, @required this.handlers})
: _routeDefinition = _RouteGrammar.routeDefinition
.parse(new SpanScanner(path.replaceAll(_straySlashes, '')))
.value {
if (_routeDefinition.segments.isEmpty) _parser = match('').value((r) => {});
}
/// Returns the [Route] instances that will respond to requests
/// to the index of this instance's path.
///
/// May return `this`.
Iterable<Route> get allIndices {
return children.where((r) => r.path.replaceAll(path, '').isEmpty);
factory Route.join(Route a, Route b) {
var start = a.path.replaceAll(_straySlashes, '');
var end = b.path.replaceAll(_straySlashes, '');
return new Route('$start/$end'.replaceAll(_straySlashes, ''),
method: b.method, handlers: b.handlers);
}
/// Backtracks up the hierarchy, and builds
/// a sequential list of all handlers from both
/// this route, and every found parent route.
///
/// The resulting list puts handlers from routes
/// higher in the tree at lower indices. Thus,
/// this can be used in a routing-enabled application
/// to evaluate multiple middleware on a single route,
/// and apply them to all children.
List get handlerSequence {
final result = [];
var r = this;
Parser<Map<String, String>> get parser =>
_parser ??= _routeDefinition.compile();
while (r != null) {
result.insertAll(0, r.handlers);
r = r.parent;
}
return result;
}
/// Returns the [Route] instance that will respond to requests
/// to the index of this instance's path.
///
/// May return `this`.
Route get indexRoute {
return children.firstWhere((r) => r.path.replaceAll(path, '').isEmpty,
orElse: () => this);
}
Route._base();
Route(Pattern path,
{Iterable<Route> children: const [],
this.debug: false,
Iterable handlers: const [],
method: "GET",
String name: null}) {
if (children != null) _children.addAll(children);
if (handlers != null) _handlers.addAll(handlers);
_method = method;
_name = name;
if (path is RegExp) {
_matcher = path;
_path = path.pattern;
} else {
_matcher = new RegExp(
_matcherify(path.toString().replaceAll(_straySlashes, '')));
_path = _pathified = _pathify(path.toString());
_resolver = new RegExp(_matcherify(
path.toString().replaceAll(_straySlashes, ''),
expand: false));
}
_parentResolver = new RegExp(_matcher.pattern.replaceAll(_rgxEnd, ''));
}
/// Splits a route path into a list of segments, and then
/// builds a hierarchy of off that.
///
/// This should generally be used instead of the original
/// Route constructor.
///
/// All children and handlers, as well as the method, will be
/// assigned to the last child route created.
///
/// The final child route is returned.
factory Route.build(Pattern path,
{Iterable<Route> children: const [],
bool debug: false,
Iterable handlers: const [],
method: "GET",
String name: null}) {
Route result;
if (path is RegExp) {
result = new Route(path,
debug: debug, handlers: handlers, method: method, name: name);
} else {
final segments = path
.toString()
.split('/')
.where((str) => str.isNotEmpty)
.toList(growable: false);
if (segments.isEmpty) {
return new Route('/',
children: children,
debug: debug,
handlers: handlers,
method: method,
name: name);
}
var head = '';
for (int i = 0; i < segments.length; i++) {
final segment = segments[i];
head = (head + '/$segment').replaceAll(_straySlashes, '');
if (i == segments.length - 1) {
if (result == null) {
result = new Route(segment, debug: debug);
} else {
result = result.child(segment, debug: debug);
}
} else {
if (result == null) {
result = new Route(segment, debug: debug, method: "*");
} else {
result = result.child(segment, debug: debug, method: "*");
}
}
result._head = new RegExp(_matcherify(head).replaceAll(_rgxEnd, ''));
}
}
result._children.addAll(children);
result._handlers.addAll(handlers);
result._method = method;
result._name = name;
return result..debug = debug;
}
/// Combines the paths and matchers of two [Route] instances, and creates a new instance.
factory Route.join(Route parent, Route child, {bool debug: false}) {
final String path1 = parent.path
.replaceAll(_rgxStart, '')
.replaceAll(_rgxEnd, '')
.replaceAll(_straySlashes, '');
final String path2 = child.path
.replaceAll(_rgxStart, '')
.replaceAll(_rgxEnd, '')
.replaceAll(_straySlashes, '');
final String pattern1 = parent.matcher.pattern
.replaceAll(_rgxEnd, '')
.replaceAll(_rgxStraySlashes, '');
final String pattern2 = child.matcher.pattern
.replaceAll(_rgxStart, '')
.replaceAll(_rgxStraySlashes, '');
final route = new Route('$path1/$path2',
children: child.children,
handlers: child.handlers,
method: child.method,
name: child.name);
String separator = (pattern1.isEmpty || pattern1 == '^' || pattern2 == r'$') ? '' : '\\/';
parent._children.add(route
.._matcher = new RegExp('$pattern1$separator$pattern2')
.._head = new RegExp(
_matcherify('$path1/$path2'.replaceAll(_straySlashes, ''))
.replaceAll(_rgxEnd, ''))
.._parent = parent
.._stub = child.matcher);
/*parent._printDebug(
"Joined '/$path1' and '/$path2', created head: ${route._head.pattern} and stub: ${route._stub.pattern}");*/
return route..debug = parent.debug || child.debug || debug;
}
/// Calls [addChild] on all given routes.
List<Route> addAll(Iterable<Route> routes, {bool join: true}) {
return routes.map((route) => addChild(route, join: join)).toList();
}
/// Adds the given route as a hierarchical child of this one.
Route addChild(Route route, {bool join: true}) {
Route created;
if (join) {
created = new Route.join(this, route);
} else {
_children.add(created = route.._parent = this);
}
return created..debug = debug;
}
/// Assigns a name to this route.
Route as(String name) => this.._name = name;
/// Creates a hierarchical child of this route with the given path.
Route child(Pattern path,
{Iterable<Route> children: const [],
bool debug: false,
Iterable handlers: const [],
String method: "GET",
String name: null}) {
final route = new Route.build(path,
children: children,
debug: debug,
handlers: handlers,
method: method,
name: name);
return addChild(route);
@override
String toString() {
return '$method $path => $handlers';
}
Route clone() {
final Route route = new Route('', debug: debug);
return route
.._children.addAll(children)
.._handlers.addAll(handlers)
.._head = _head
.._matcher = _matcher
.._method = _method
.._name = name
.._parent = _parent
.._parentResolver = _parentResolver
.._path = _path
.._pathified = _pathified
.._resolver = _resolver
.._stub = _stub;
return new Route(path, method: method, handlers: handlers)
.._cache.addAll(_cache);
}
/// Generates a URI to this route with the given parameters.
String makeUri([Map<String, dynamic> params]) {
String result = _pathify(path);
if (params != null) {
for (String key in (params.keys)) {
result = result.replaceAll(
new RegExp(":$key" + r"\??"), params[key].toString());
/// Use the setter instead.
@deprecated
void as(String n) {
name = n;
}
String makeUri(Map<String, dynamic> params) {
var b = new StringBuffer();
int i = 0;
for (var seg in _routeDefinition.segments) {
if (i++ > 0) b.write('/');
if (seg is _ConstantSegment)
b.write(seg.text);
else if (seg is _ParameterSegment) {
if (!params.containsKey(seg.name))
throw new ArgumentError('Missing parameter "${seg.name}".');
b.write(params[seg.name]);
}
}
return result.replaceAll("*", "");
return b.toString();
}
/// Attempts to match a path against this route.
Match match(String path) =>
_cache.putIfAbsent(path, () => matcher.firstMatch(path.replaceAll(_straySlashes, '')));
/// Extracts route parameters from a given path.
Map<String, String> parseParameters(String requestPath) {
Map<String, String> result = {};
Iterable<String> values =
_parseParameters(requestPath.replaceAll(_straySlashes, ''));
// _printDebug(
// 'Searched request path $requestPath and found these values: $values');
// _printDebug('This route\'s path is "$path".');
final pathString = _pathify(path).replaceAll(new RegExp('\/'), r'\/');
Iterable<Match> matches = _param.allMatches(pathString);
// _printDebug(
// 'All param names parsed in "$pathString": ${matches.map((m) => m[1])}');
for (int i = 0; i < matches.length && i < values.length; i++) {
Match match = matches.elementAt(i);
String paramName = match.group(1);
String value = Uri.decodeComponent(values.elementAt(i));
// _printDebug('Setting param "$paramName" to "$value"...');
result[paramName] = value;
}
return result;
}
_parseParameters(String requestPath) sync* {
Match routeMatch = matcher.firstMatch(requestPath);
if (routeMatch != null)
for (int i = 1; i <= routeMatch.groupCount; i++)
yield routeMatch.group(i);
}
/// Finds the first route available within this hierarchy that can respond to the given path.
///
/// Can be used to navigate a route hierarchy like a file system.
Route resolve(String path, {bool filter(Route route), String fullPath}) {
// _printDebug(
// 'Path to resolve: "/${path.replaceAll(_straySlashes, '')}", our matcher: ${matcher.pattern}');
bool _filter(route) {
if (filter == null) {
// _printDebug('No filter provided, returning true for $route');
return true;
} else {
// _printDebug('Running filter on $route');
final result = filter(route);
// _printDebug('Filter result: $result');
return result;
}
}
final _fullPath = fullPath ?? path;
if ((path.isEmpty || path == '.') && _filter(indexRoute)) {
// Try to find index
// _printDebug('Empty path, resolving with indexRoute: $indexRoute');
return indexRoute;
} else if (path == '/') {
return absoluteParent.resolve('');
} else if (path.replaceAll(_straySlashes, '').isEmpty) {
for (Route route in children) {
final stub = route.path.replaceAll(this.path, '');
if ((stub == '/' || stub.isEmpty) && _filter(route))
return route.resolve('');
}
if (_filter(indexRoute)) {
// _printDebug(
// 'Path "/$path" is technically empty, sending to indexRoute: $indexRoute');
return indexRoute;
} else
return null;
} else if (path == '..') {
if (parent != null)
return parent;
else
throw new RoutingException.orphan();
} else if (path.startsWith('/') &&
path.length > 1 &&
path[1] != '/' &&
absoluteParent != null) {
return absoluteParent.resolve(path.substring(1),
filter: _filter, fullPath: _fullPath);
} else if (matcher.hasMatch(path.replaceAll(_straySlashes, '')) ||
_resolver.hasMatch(path.replaceAll(_straySlashes, ''))) {
// _printDebug(
// 'Path "/$path" matched our matcher, sending to indexRoute: $indexRoute');
return indexRoute;
} else {
final segments = path.split('/').where((str) => str.isNotEmpty).toList();
// _printDebug('Segments: $segments on "/${this.path}"');
if (segments.isEmpty) {
// _printDebug('Empty segments, sending to indexRoute: $indexRoute');
return indexRoute;
}
if (segments[0] == '..') {
if (parent != null)
return parent.resolve(segments.skip(1).join('/'),
filter: _filter, fullPath: _fullPath);
else
throw new RoutingException.orphan();
} else if (segments[0] == '.') {
return resolve(segments.skip(1).join('/'),
filter: _filter, fullPath: _fullPath);
}
for (Route route in children) {
final subPath = '${this.path}/${segments[0]}';
// _printDebug(
// 'seg0: ${segments[0]}, stub: ${route._stub?.pattern}, path: $path, route.path: ${route.path}, route.matcher: ${route.matcher.pattern}, this.matcher: ${matcher.pattern}');
if (route.match(subPath) != null ||
route._resolver.firstMatch(subPath) != null) {
if (segments.length == 1 && _filter(route))
return route.resolve('');
else {
return route.resolve(segments.skip(1).join('/'),
filter: _filter,
fullPath: this.path.replaceAll(_straySlashes, '') +
'/' +
_fullPath.replaceAll(_straySlashes, ''));
}
} else if (route._stub != null && route._stub.hasMatch(segments[0])) {
if (segments.length == 1) {
// _printDebug('Stub perhaps matches');
return route.resolve('');
} else {
// _printDebug(
// 'Maybe stub matches. Sending remaining segments to $route');
return route.resolve(segments.skip(1).join('/'));
}
}
}
// Try to match "subdirectory"
for (Route route in children) {
// _printDebug(
// 'Trying to match subdir for $path; child ${route.path} on ${this.path}');
final match = route._parentResolver.firstMatch(path);
if (match != null) {
final subPath =
path.replaceFirst(match[0], '').replaceAll(_straySlashes, '');
// _printDebug("Subdir path: $subPath");
for (Route child in route.children) {
final testPath = child.path
.replaceFirst(route.path, '')
.replaceAll(_straySlashes, '');
if (subPath == testPath &&
(child.match(_fullPath) != null ||
child._resolver.firstMatch(_fullPath) != null) &&
_filter(child)) {
return child.resolve('');
}
}
}
}
// Try to match the whole route, if nothing else works
for (Route route in children) {
// _printDebug(
// 'Trying to match full $_fullPath for ${route.path} on ${this.path}');
if ((route.match(_fullPath) != null ||
route._resolver.firstMatch(_fullPath) != null) &&
_filter(route)) {
// _printDebug('Matched full path!');
return route.resolve('');
} else if ((route.match('/$_fullPath') != null ||
route._resolver.firstMatch('/$_fullPath') != null) &&
_filter(route)) {
// _printDebug('Matched full path (with a leading slash!)');
return route.resolve('');
}
}
// Lastly, check to see if we have an index route to resolve with
if (indexRoute != this) {
// _printDebug('Forwarding "/$path" to indexRoute');
return indexRoute.resolve(path);
}
return null;
}
}
@override
String toString() => "$method '$path' => ${handlers.length} handler(s)";
}

View file

@ -1,7 +1,11 @@
library angel_route.src.router;
import 'package:combinator/combinator.dart';
import 'package:meta/meta.dart';
import 'package:string_scanner/string_scanner.dart';
import 'routing_exception.dart';
import '../string_util.dart';
part 'grammar.dart';
part 'symlink_route.dart';
part 'route.dart';
part 'routing_result.dart';
@ -63,7 +67,7 @@ class Router {
/// Adds a route that responds to the given path
/// for requests with the given method (case-insensitive).
/// Provide '*' as the method to respond to all methods.
Route addRoute(String method, Pattern path, Object handler,
Route addRoute(String method, String path, Object handler,
{List middleware: const []}) {
if (_useCache == true)
throw new StateError('Cannot add routes after caching is enabled.');
@ -73,10 +77,9 @@ class Router {
if (middleware != null) handlers.insertAll(0, middleware);
final route =
new Route(path, debug: debug, method: method, handlers: handlers);
final route = new Route(path, method: method, handlers: handlers);
_routes.add(route);
return route.._path = _pathify(path);
return route;
}
/// Prepends the given middleware to any routes created
@ -87,7 +90,7 @@ class Router {
/// The resulting router can be chained, too.
_ChainedRouter chain(middleware) {
var piped = new _ChainedRouter(this, middleware);
var route = new SymlinkRoute('/', '/', piped);
var route = new SymlinkRoute('/', piped);
_routes.add(route);
return piped;
}
@ -103,8 +106,7 @@ class Router {
} else if (route is SymlinkRoute) {
final newRouter = route.router.clone();
newMounted[route.path] = newRouter;
final symlink = new SymlinkRoute(route.path, route.pattern, newRouter)
.._head = route._head;
final symlink = new SymlinkRoute(route.path, newRouter);
router._routes.add(symlink);
}
}
@ -117,8 +119,7 @@ class Router {
void dumpTree(
{callback(String tree),
String header: 'Dumping route tree:',
String tab: ' ',
bool showMatchers: false}) {
String tab: ' '}) {
final buf = new StringBuffer();
int tabs = 0;
@ -138,14 +139,14 @@ class Router {
for (Route route in router.routes) {
indent();
buf.write('- ${route.path.isNotEmpty ? route.path : '/'}');
buf.write('- ');
if (route is! SymlinkRoute) buf.write('${route.method} ');
buf.write('${route.path.isNotEmpty ? route.path : '/'}');
if (route is SymlinkRoute) {
buf.writeln();
dumpRouter(route.router);
} else {
if (showMatchers) buf.write(' (${route.matcher.pattern})');
buf.writeln(' => ${route.handlers.length} handler(s)');
}
}
@ -163,13 +164,13 @@ class Router {
///
/// Returns the created route.
/// You can also register middleware within the router.
SymlinkRoute group(Pattern path, void callback(Router router),
SymlinkRoute group(String path, void callback(Router router),
{Iterable middleware: const [],
String name: null,
String namespace: null}) {
final router = new Router().._middleware.addAll(middleware);
callback(router..debug = debug);
return mount(path, router, namespace: namespace).._name = name;
return mount(path, router, namespace: namespace)..name = name;
}
/// Generates a URI string based on the given input.
@ -224,17 +225,24 @@ class Router {
}
// Search by path
for (Route route in search.routes) {
if (route.match(param) != null) {
segments.add(route.path.replaceAll(_straySlashes, ''));
lastRoute = route;
if (!resolved) {
var scanner = new SpanScanner(param.replaceAll(_straySlashes, ''));
for (Route route in search.routes) {
int pos = scanner.position;
if (route.parser
.parse(scanner)
.successful && scanner.isDone) {
segments.add(route.path.replaceAll(_straySlashes, ''));
lastRoute = route;
if (route is SymlinkRoute) {
search = route.router;
}
if (route is SymlinkRoute) {
search = route.router;
}
resolved = true;
break;
resolved = true;
break;
} else
scanner.position = pos;
}
}
@ -272,72 +280,44 @@ class Router {
/// with the given method.
bool resolve(String absolute, String relative, List<RoutingResult> out,
{String method: 'GET', bool strip: true}) {
//final cleanAbsolute =
// strip == false ? absolute : stripStraySlashes(absolute);
final cleanRelative =
strip == false ? relative : stripStraySlashes(relative);
//final segments = cleanRelative.split('/').where((str) => str.isNotEmpty);
bool success = false;
//print(
// 'Now resolving $method "/$cleanRelative", absolute: $cleanAbsolute');
//print('Path segments: ${segments.toList()}');
var scanner = new SpanScanner(cleanRelative);
for (Route route in routes) {
/* // No longer necessary
if (route is SymlinkRoute && route._head != null && segments.isNotEmpty) {
final s = [];
bool crawl(Router r) {
bool success = false;
for (String seg in segments) {
s.add(seg);
final match = route._head.firstMatch(s.join('/'));
for (Route route in r.routes) {
int pos = scanner.position;
if (match != null) {
final cleaned = s.join('/').replaceFirst(match[0], '');
var tail = cleanRelative.replaceAll(route._head, '');
tail = stripStraySlashes(tail);
if (cleaned.isEmpty) {
//print(
// 'Matched relative "$cleanRelative" to head ${route._head
// .pattern} on $route. Tail: "$tail"');
route.router.debug = route.router.debug || debug;
var nested = <RoutingResult>[];
route.router.resolve(cleanAbsolute, tail, nested,
method: method, strip: false);
var result = _dumpResult(
cleanRelative,
new RoutingResult(
match: match,
nested: nested,
params: route.parseParameters(match[0]),
shallowRoute: route,
shallowRouter: this,
tail: tail));
out.add(result);
success = true;
}
if (route is SymlinkRoute) {
if (route.parser.parse(scanner).successful) {
var s = crawl(route.router);
if (s) success = true;
}
scanner.position = pos;
} else if (route.method == '*' || route.method == method) {
var parseResult = route.parser.parse(scanner);
if (parseResult.successful && scanner.isDone) {
var result = new RoutingResult(
parseResult: parseResult,
params: parseResult.value,
shallowRoute: route,
shallowRouter: this);
out.add(result);
success = true;
}
scanner.position = pos;
}
}
*/
if (route.method == '*' || route.method == method) {
final match = route.match(cleanRelative);
if (match != null) {
var result = new RoutingResult(
match: match,
params: route.parseParameters(cleanRelative),
shallowRoute: route,
shallowRouter: this);
out.add(result);
success = true;
}
}
return success;
}
//print('Could not resolve path "/$cleanRelative".');
return success;
return crawl(this);
}
/// Returns the result of [resolve] with [path] passed as
@ -378,7 +358,7 @@ class Router {
/// For example, if the [Router] has a middleware 'y', and the `namespace`
/// is 'x', then that middleware will be available as 'x.y' in the main router.
/// These namespaces can be nested.
SymlinkRoute mount(Pattern path, Router router,
SymlinkRoute mount(String path, Router router,
{bool hooked: true, String namespace: null}) {
// Let's copy middleware, heeding the optional middleware namespace.
String middlewarePrefix = namespace != null ? "$namespace." : "";
@ -389,52 +369,51 @@ class Router {
copiedMiddleware[middlewareName];
}
final route =
new SymlinkRoute(path, path, router..debug = debug || router.debug);
final route = new SymlinkRoute(path, router);
_mounted[route.path] = router;
_routes.add(route);
route._head = new RegExp(route.matcher.pattern.replaceAll(_rgxEnd, ''));
//route._head = new RegExp(route.matcher.pattern.replaceAll(_rgxEnd, ''));
return route.._name = namespace;
return route..name = namespace;
}
/// Adds a route that responds to any request matching the given path.
Route all(Pattern path, Object handler, {List middleware}) {
Route all(String path, Object handler, {List middleware}) {
return addRoute('*', path, handler, middleware: middleware);
}
/// Adds a route that responds to a DELETE request.
Route delete(Pattern path, Object handler, {List middleware}) {
Route delete(String path, Object handler, {List middleware}) {
return addRoute('DELETE', path, handler, middleware: middleware);
}
/// Adds a route that responds to a GET request.
Route get(Pattern path, Object handler, {List middleware}) {
Route get(String path, Object handler, {List middleware}) {
return addRoute('GET', path, handler, middleware: middleware);
}
/// Adds a route that responds to a HEAD request.
Route head(Pattern path, Object handler, {List middleware}) {
Route head(String path, Object handler, {List middleware}) {
return addRoute('HEAD', path, handler, middleware: middleware);
}
/// Adds a route that responds to a OPTIONS request.
Route options(Pattern path, Object handler, {List middleware}) {
Route options(String path, Object handler, {List middleware}) {
return addRoute('OPTIONS', path, handler, middleware: middleware);
}
/// Adds a route that responds to a POST request.
Route post(Pattern path, Object handler, {List middleware}) {
Route post(String path, Object handler, {List middleware}) {
return addRoute('POST', path, handler, middleware: middleware);
}
/// Adds a route that responds to a PATCH request.
Route patch(Pattern path, Object handler, {List middleware}) {
Route patch(String path, Object handler, {List middleware}) {
return addRoute('PATCH', path, handler, middleware: middleware);
}
/// Adds a route that responds to a PUT request.
Route put(Pattern path, Object handler, {List middleware}) {
Route put(String path, Object handler, {List middleware}) {
return addRoute('PUT', path, handler, middleware: middleware);
}
}
@ -451,7 +430,7 @@ class _ChainedRouter extends Router {
}
@override
Route addRoute(String method, Pattern path, handler,
Route addRoute(String method, String path, handler,
{List middleware: const []}) {
var route = super.addRoute(method, path, handler,
middleware: []..addAll(_handlers)..addAll(middleware ?? []));
@ -459,18 +438,18 @@ class _ChainedRouter extends Router {
return route;
}
SymlinkRoute group(Pattern path, void callback(Router router),
SymlinkRoute group(String path, void callback(Router router),
{Iterable middleware: const [],
String name: null,
String namespace: null}) {
final router =
new _ChainedRouter(_root, []..addAll(_handlers)..addAll(middleware));
callback(router..debug = debug);
return mount(path, router, namespace: namespace).._name = name;
return mount(path, router, namespace: namespace)..name = name;
}
@override
SymlinkRoute mount(Pattern path, Router router,
SymlinkRoute mount(String path, Router router,
{bool hooked: true, String namespace: null}) {
final route =
super.mount(path, router, hooked: hooked, namespace: namespace);
@ -485,7 +464,7 @@ class _ChainedRouter extends Router {
piped._handlers.addAll([]
..addAll(_handlers)
..addAll(middleware is Iterable ? middleware : [middleware]));
var route = new SymlinkRoute('/', '/', piped);
var route = new SymlinkRoute('/', piped);
_routes.add(route);
return piped;
}

View file

@ -2,8 +2,8 @@ part of angel_route.src.router;
/// Represents a complex result of navigating to a path.
class RoutingResult {
/// The Regex match that matched the given sub-path.
final Match match;
/// The parse result that matched the given sub-path.
final ParseResult<Map<String, String>> parseResult;
/// A nested instance, if a sub-path was matched.
final Iterable<RoutingResult> nested;
@ -80,7 +80,7 @@ class RoutingResult {
}
RoutingResult(
{this.match,
{this.parseResult,
Map<String, dynamic> params: const {},
this.nested,
this.shallowRoute,

View file

@ -3,8 +3,6 @@ part of angel_route.src.router;
/// Placeholder [Route] to serve as a symbolic link
/// to a mounted [Router].
class SymlinkRoute extends Route {
final Pattern pattern;
final Router router;
SymlinkRoute(Pattern path, this.pattern, this.router) : super(path);
SymlinkRoute(String path, this.router) : super(path, method: null, handlers: null);
}

View file

@ -1,8 +1,12 @@
name: angel_route
description: A powerful, isomorphic routing library for Dart.
version: 2.0.2
version: 2.0.3
author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/angel_route
environment:
sdk: ">=1.19.0"
dependencies:
combinator: ^1.0.0-beta
dev_dependencies:
browser: ">=0.10.0 < 0.11.0"
http: ">=0.11.3 <0.12.0"

View file

@ -4,9 +4,9 @@ import 'package:test/test.dart';
main() {
final router = new Router();
router.get('/', 'GET').as('root');
router.get('/', 'GET').name = 'root';
router.get('/user/:id', 'GET');
router.get('/first/:first/last/:last', 'GET').as('full_name');
router.get('/first/:first/last/:last', 'GET').name = 'full_name';
navigate(params) {
final uri = router.navigate(params);
@ -14,7 +14,7 @@ main() {
return uri;
}
router.dumpTree(showMatchers: true);
router.dumpTree();
group('top-level', () {
test('named', () {

View file

@ -25,8 +25,8 @@ basic(BrowserRouter router) {
router.get('a', 'a handler');
router.group('b', (router) {
router.get('a', 'b/a handler').as('b/a');
router.get('b', 'b/b handler', middleware: ['b/b middleware']).as('b/b');
router.get('a', 'b/a handler').name = 'b/a';
router.get('b', 'b/b handler', middleware: ['b/b middleware']).name = 'b/b';
}, middleware: ['b middleware']);
router.get('c', 'c handler');