dumpTree method

void dumpTree (
  1. {dynamic callback(
    1. String tree
    ),
  2. String header: 'Dumping route tree:',
  3. String tab: ' '}
)

Creates a visual representation of the route hierarchy and passes it to a callback. If none is provided, print is called.

Implementation

void dumpTree(
    {callback(String tree),
    String header = 'Dumping route tree:',
    String tab = '  '}) {
  final buf = StringBuffer();
  int tabs = 0;

  if (header != null && header.isNotEmpty) {
    buf.writeln(header);
  }

  buf.writeln('<root>');

  indent() {
    for (int i = 0; i < tabs; i++) {
      buf.write(tab);
    }
  }

  dumpRouter(Router router) {
    indent();
    tabs++;

    for (Route route in router.routes) {
      indent();
      buf.write('- ');
      if (route is! SymlinkRoute) buf.write('${route.method} ');
      buf.write('${route.path.isNotEmpty ? route.path : '/'}');

      if (route is SymlinkRoute<T>) {
        buf.writeln();
        dumpRouter(route.router);
      } else {
        buf.writeln(' => ${route.handlers.length} handler(s)');
      }
    }

    tabs--;
  }

  dumpRouter(this);

  (callback ?? print)(buf.toString());
}