A generic event router

@return Router
548775
function Router () {
  if (!(this instanceof Router)) return new Router();

  var index = 0;

  /**
   * @api public
   */

  function router () {
    router.route.apply(router, slice.call(arguments));
  }

  router.__proto__ = Router.prototype;

  /**
   * Inc the index
   *
   * @api private
   * @return Number
   */

  router.index = function () {
    return index++;
  };

  /**
   * Pushes the socket and arguments through the middleware
   *
   * @api public
   * @param {Error} err *optional
   * @param {Socket} socket
   * @param {Array} args
   * @param {Function} cb
   */

  router.route = function () {
    var args = slice.call(arguments);
    var name = '*';
    var err = null;
    var cb = function () { console.warn('default callback', slice.call(arguments)); };
    var end = function (err) { 
      if (cb.called) return;
      cb.called = new Date(); 
      cb.apply(cb, [err ? err : null].concat(args)); 
    };

    if ('string' === typeof args[0]) name = args.shift();
    if ('object' === typeof args[0] && args[0] instanceof Error) err = args.shift();
    if ('function' === typeof args[args.length - 1]) cb = args.pop();

    var i = 0, path = router.getPath(name), len = path.length;

    debug('got path the length is %s', len);

    (function step (err) {
      
      if (cb.called) return;

      debug('current step %s of %s', i+1, len);

      function next (err) {
        if (++i >= len) {
          end(err);
        }
        else {
          step(err);
        }
      }

      var fn = path[i];
      if (!fn) return next();

      try {
        if (err) {
          if (fn.err) {
            if (fn instanceof Router) {
              fn.route.apply(fn,[name, err].concat(args).concat(next).concat(end));
            }
            else {
              fn.apply(fn, [err].concat(args).concat(next).concat(end));
            }
          }
          else {
            next(err);
          }
        }
        else {
          if (fn.err) {
            next();
          }
          else {
            if (fn instanceof Router) {
              fn.route.apply(fn,[name].concat(args).concat(next).concat(end));
            }
            else {
              fn.apply(fn, args.concat(next).concat(end));
            }
          }
        }
      }
      catch(e) {
        debug('caught error %s', e);
        debug('the fn causing problems', fn);
        console.error(e.stack);
        next(e);
      }
      
    })(err);

  };
  
  return router;

}
662868
public static function router()
    {
        if (isset(self::core()->router)) {
            return self::core()->router;
        }

        //====================================================================//
        // Initialize Tasks List
        self::core()->router = new Router();

        return self::core()->router;
    }
376466
public function getRouteBuilder()
    {
        if (null == $this->routeBuilder) {
            $this->setRouteBuilder(new RouteBuilder($this->getRouteFile()));
        }
        return $this->routeBuilder;
    }
936794
public function group(callable $callable = null) {
        $router = $this->createNestedRouter();

        if ($callable !== null) {
            $this->invokeCallable($callable, $router);
        }

        return $router;
    }
944155
public static function getDispatcher(): EventDispatcher
    {
        if (self::$dispatcher) {
            return self::$dispatcher;
        }

        return self::$dispatcher = self::createDispatcher();
    }
141554
public function & GetRouter () {
		if ($this->router === NULL) {
			$routerClass = $this->routerClass;
			$this->router = & $routerClass::GetInstance();
		}
		return $this->router;
	}
1350068
protected function configureEvents(): self
    {
        $container = $this->getContainer();
        $key = 'm6web_daemon.iterations_events';

        if ($container->hasParameter($key)) {
            $this->iterationsEvents = $container->getParameter($key);
        }

        return $this;
    }
1280344
public function getEventBus()
    {
        if (!$this->eventBus) {
            if (isset(static::$staticEventBus)) {
                return static::$staticEventBus;
            }
            $this->eventBus = new Bus;
        }
        return $this->eventBus;
    }
169069
protected function buildRouter()
    {
        $routes = new RouteCollection();
        $context = new RequestContext();
        $router = new UrlMatcher($routes, $context);

        return($router);
    }
384676
protected function createDispatcher()
    {
        return $this->dispatcher ?: \FastRoute\simpleDispatcher(function ($r) {
            foreach ($this->router->getRoutes() as $route) {
                $r->addRoute($route['method'], $route['uri'], $route['action']);
            }
        });
    }
1022217
protected function dispatchToRouter()
    {
        return function ($request) {
            $this->app->instance('request', $request);

            return $this->router->dispatch($request);
        };
    }
797401
public function routeDispatch()
    {
        $this->applyHook('sys.before.router');
        //分组
        $this->route->applyGroup();
        $this->route->next();
        $this->applyHook('sys.after.router');
        return $this;
    }
1041659
public function getEventQueue()
    {
        if (null === $this->eventQueue) {
            $this->eventQueue = new Event\EventQueue(
                $this->getConfigurationRegistry(),
                $this->getEventDispatcher()
            );
        }

        return $this->eventQueue;
    }
623708
public function build() : RouterInterface
    {
        $router = $this->router;
        try{
            // call bootstrap code
            $startup = $this->bootstrap;
            
            $startup($router);
        }
        catch(\Throwable $e)
        {
            throw new RouterBuildingException($e);
        }
        return $router;
    }
272294
public function getEvent()
    {
        if (null === $this->event) {
            $this->setEvent(new AdapterChainEvent);
            $this->event->setTarget($this);
        }

        return $this->event;
    }
987519
public static function all()
    {
        $router = new AltoRouter();

        foreach (static::$routes as $route) {
            isset($route['name']) ? $name = $route['name'] : $name = null;
            $router->map($route['method'], $route['target'], $route['handler'], $name);
        }

        return $router;
    }
950744
protected function getRouter()
    {
        if (null === $this->router)
        {
            $this->router = $this->getContainer()->get('router');
        }

        return $this->router;
    }