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 [],
method: "GET",
String name: null}) {
final segments = path
.toString()
.split('/')
.where((str) => str.isNotEmpty)
.toList(growable: false);
Route result;
if (segments.isEmpty) {
return new Route('/',
children: children,
debug: debug,
handlers: handlers,
method: method,
name: name);
}
if (path is RegExp) {
result = new Route(path, debug: debug);
} else {
final segments = path
.toString()
.split('/')
.where((str) => str.isNotEmpty)
.toList(growable: false);
for (int i = 0; i < segments.length; i++) {
final segment = segments[i];
if (segments.isEmpty) {
return new Route('/',
children: children,
debug: debug,
handlers: handlers,
method: method,
name: name);
}
if (i == segments.length - 1) {
if (result == null) {
result = new Route(segment, debug: debug);
for (int i = 0; i < segments.length; i++) {
final segment = segments[i];
if (i == segments.length - 1) {
if (result == null) {
result = new Route(segment, debug: debug);
} else {
result = result.child(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: "*");
if (result == null) {
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.
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');
@ -484,15 +491,15 @@ class Route {
'Trying to match full $_fullPath for ${route.path} on ${this.path}');
if ((route.match(_fullPath) != null ||
route._resolver.firstMatch(_fullPath) != null) &&
_filter(route))
_filter(route)) {
_printDebug('Matched full path!');
return route.resolve('');
else if ((route.match(_fullPath) != null ||
route._resolver.firstMatch(_fullPath) != null) &&
_filter(route))
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('');
_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

View file

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