This commit is contained in:
thosakwe 2016-10-22 20:52:28 -04:00
parent 7a699456d2
commit 5241e66eff
7 changed files with 65 additions and 48 deletions

View file

@ -170,36 +170,41 @@ class Route {
Iterable handlers: const [], Iterable handlers: const [],
method: "GET", method: "GET",
String name: null}) { String name: null}) {
final segments = path
.toString()
.split('/')
.where((str) => str.isNotEmpty)
.toList(growable: false);
Route result; Route result;
if (segments.isEmpty) { if (path is RegExp) {
return new Route('/', result = new Route(path, debug: debug);
children: children, } else {
debug: debug, final segments = path
handlers: handlers, .toString()
method: method, .split('/')
name: name); .where((str) => str.isNotEmpty)
} .toList(growable: false);
for (int i = 0; i < segments.length; i++) { if (segments.isEmpty) {
final segment = segments[i]; return new Route('/',
children: children,
debug: debug,
handlers: handlers,
method: method,
name: name);
}
if (i == segments.length - 1) { for (int i = 0; i < segments.length; i++) {
if (result == null) { final segment = segments[i];
result = new Route(segment, debug: debug);
if (i == segments.length - 1) {
if (result == null) {
result = new Route(segment, debug: debug);
} else {
result = result.child(segment, debug: debug);
}
} else { } else {
result = result.child(segment, debug: debug); if (result == null) {
} result = new Route(segment, debug: debug, method: "*");
} else { } else {
if (result == null) { result = result.child(segment, debug: debug, method: "*");
result = new Route(segment, debug: debug, method: "*"); }
} else {
result = result.child(segment, debug: debug, method: "*");
} }
} }
} }
@ -354,6 +359,8 @@ class Route {
/// ///
/// Can be used to navigate a route hierarchy like a file system. /// Can be used to navigate a route hierarchy like a file system.
Route resolve(String path, {bool filter(Route route), String fullPath}) { Route resolve(String path, {bool filter(Route route), String fullPath}) {
_printDebug(
'Path to resolve: "/${path.replaceAll(_straySlashes, '')}", our matcher: ${matcher.pattern}');
bool _filter(route) { bool _filter(route) {
if (filter == null) { if (filter == null) {
_printDebug('No filter provided, returning true for $route'); _printDebug('No filter provided, returning true for $route');
@ -484,15 +491,15 @@ class Route {
'Trying to match full $_fullPath for ${route.path} on ${this.path}'); 'Trying to match full $_fullPath for ${route.path} on ${this.path}');
if ((route.match(_fullPath) != null || if ((route.match(_fullPath) != null ||
route._resolver.firstMatch(_fullPath) != null) && route._resolver.firstMatch(_fullPath) != null) &&
_filter(route)) _filter(route)) {
_printDebug('Matched full path!');
return route.resolve(''); return route.resolve('');
else if ((route.match(_fullPath) != null || } else if ((route.match('/$_fullPath') != null ||
route._resolver.firstMatch(_fullPath) != null) &&
_filter(route))
return route.resolve('');
else if ((route.match('/$_fullPath') != null ||
route._resolver.firstMatch('/$_fullPath') != null) && route._resolver.firstMatch('/$_fullPath') != null) &&
_filter(route)) return route.resolve(''); _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 // Lastly, check to see if we have an index route to resolve with

View file

@ -55,7 +55,8 @@ class Router extends Extensible {
return new Route('/', debug: debug, handlers: handlers, method: method) return new Route('/', debug: debug, handlers: handlers, method: method)
..debug = debug; ..debug = debug;
} else { } else {
result = resolve(segments[0], (route) => route.method == method || route.method == '*'); result = resolve(segments[0],
(route) => route.method == method || route.method == '*');
if (result != null) { if (result != null) {
if (segments.length > 1) { if (segments.length > 1) {
@ -65,7 +66,9 @@ class Router extends Extensible {
Route existing; Route existing;
do { do {
existing = result.resolve(segments[0], filter: (route) => route.method == method || route.method == '*'); existing = result.resolve(segments[0],
filter: (route) =>
route.method == method || route.method == '*');
if (existing != null) { if (existing != null) {
result = existing; result = existing;
@ -106,29 +109,31 @@ class Router extends Extensible {
var tabs = 0; var tabs = 0;
final buf = new StringBuffer(); final buf = new StringBuffer();
void dumpRoute(Route route, {String replace: null}) { void dumpRoute(Route route, {Pattern replace: null}) {
for (var i = 0; i < tabs; i++) buf.write(tab); for (var i = 0; i < tabs; i++) buf.write(tab);
if (route == root) if (route == root)
buf.write('(root) ${route.method} '); buf.write('(root)');
else else {
buf.write('- ${route.method} '); buf.write('- ${route.method} ');
final p = final p =
replace != null ? route.path.replaceAll(replace, '') : route.path; replace != null ? route.path.replaceAll(replace, '') : route.path;
if (p.isEmpty) if (p.isEmpty)
buf.write("'/'"); buf.write("'/'");
else else
buf.write("'${p.replaceAll(_straySlashes, '')}'"); buf.write("'${p.replaceAll(_straySlashes, '')}'");
if (route.handlers.isNotEmpty) if (route.handlers.isNotEmpty)
buf.writeln(' => ${route.handlers.length} handler(s)'); buf.writeln(' => ${route.handlers.length} handler(s)');
else else
buf.writeln(); buf.writeln();
}
tabs++; tabs++;
route.children.forEach((r) => dumpRoute(r, replace: route.path)); route.children
.forEach((r) => dumpRoute(r, replace: new RegExp("^${route.path}")));
tabs--; tabs--;
} }

1
test/server/packages Symbolic link
View file

@ -0,0 +1 @@
../../packages

1
web/hash/packages Symbolic link
View file

@ -0,0 +1 @@
../../packages

1
web/packages Symbolic link
View file

@ -0,0 +1 @@
../packages

1
web/push_state/packages Symbolic link
View file

@ -0,0 +1 @@
../../packages

1
web/shared/packages Symbolic link
View file

@ -0,0 +1 @@
../../packages