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(
    {Function(String tree)? callback,
    String header = 'Dumping route tree:',
    String tab = '  '}) {
  final buf = StringBuffer();
  var tabs = 0;

  if (header.isNotEmpty) {
    buf.writeln(header);
  }

  buf.writeln('<root>');

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

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

    for (var 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());
}