2016-11-22 03:31:09 +00:00
|
|
|
part of angel_route.src.router;
|
2016-10-11 18:53:32 +00:00
|
|
|
|
2016-10-19 22:04:06 +00:00
|
|
|
/// Represents a virtual location within an application.
|
2018-08-20 18:59:51 +00:00
|
|
|
class Route<T> {
|
2017-11-27 02:21:19 +00:00
|
|
|
final String method;
|
|
|
|
final String path;
|
2018-08-20 18:59:51 +00:00
|
|
|
final List<T> handlers;
|
2017-11-28 21:07:14 +00:00
|
|
|
final Map<String, Map<String, dynamic>> _cache = {};
|
|
|
|
final RouteDefinition _routeDefinition;
|
2017-11-27 02:21:19 +00:00
|
|
|
String name;
|
2017-11-28 21:07:14 +00:00
|
|
|
Parser<Map<String, dynamic>> _parser;
|
2016-10-14 03:07:34 +00:00
|
|
|
|
2017-11-27 02:21:19 +00:00
|
|
|
Route(this.path, {@required this.method, @required this.handlers})
|
2017-11-28 21:07:14 +00:00
|
|
|
: _routeDefinition = RouteGrammar.routeDefinition
|
2017-11-27 02:21:19 +00:00
|
|
|
.parse(new SpanScanner(path.replaceAll(_straySlashes, '')))
|
|
|
|
.value {
|
2018-08-20 18:59:51 +00:00
|
|
|
if (_routeDefinition?.segments?.isNotEmpty != true)
|
|
|
|
_parser =
|
|
|
|
match<Map<String, dynamic>>('').value((r) => <String, dynamic>{});
|
2016-10-14 03:07:34 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 18:59:51 +00:00
|
|
|
factory Route.join(Route<T> a, Route<T> b) {
|
2017-11-27 02:21:19 +00:00
|
|
|
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);
|
2016-10-21 03:13:13 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 21:07:14 +00:00
|
|
|
Parser<Map<String, dynamic>> get parser =>
|
2017-11-27 02:21:19 +00:00
|
|
|
_parser ??= _routeDefinition.compile();
|
2016-10-11 18:53:32 +00:00
|
|
|
|
2017-11-27 02:21:19 +00:00
|
|
|
@override
|
|
|
|
String toString() {
|
|
|
|
return '$method $path => $handlers';
|
2016-10-11 18:53:32 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 18:59:51 +00:00
|
|
|
Route<T> clone() {
|
|
|
|
return new Route<T>(path, method: method, handlers: handlers)
|
2017-11-27 02:21:19 +00:00
|
|
|
.._cache.addAll(_cache);
|
2016-11-23 18:58:34 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 02:21:19 +00:00
|
|
|
String makeUri(Map<String, dynamic> params) {
|
|
|
|
var b = new StringBuffer();
|
|
|
|
int i = 0;
|
2016-10-15 00:45:22 +00:00
|
|
|
|
2017-11-27 02:21:19 +00:00
|
|
|
for (var seg in _routeDefinition.segments) {
|
|
|
|
if (i++ > 0) b.write('/');
|
2017-11-28 21:07:14 +00:00
|
|
|
if (seg is ConstantSegment)
|
2017-11-27 02:21:19 +00:00
|
|
|
b.write(seg.text);
|
2017-11-28 21:07:14 +00:00
|
|
|
else if (seg is ParameterSegment) {
|
2017-11-27 02:21:19 +00:00
|
|
|
if (!params.containsKey(seg.name))
|
|
|
|
throw new ArgumentError('Missing parameter "${seg.name}".');
|
|
|
|
b.write(params[seg.name]);
|
2016-10-21 03:13:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-27 02:21:19 +00:00
|
|
|
return b.toString();
|
2016-10-11 18:53:32 +00:00
|
|
|
}
|
2017-10-08 22:44:11 +00:00
|
|
|
}
|