input->getOption('path'); $server = $this->input->getOption('server'); $factory = $this->container->get(DispatcherFactory::class); $router = $factory->getRouter($server); $this->show( $this->analyzeRouter($server, $router, $path), $this->output ); } protected function configure() { $this->setDescription('Describe the routes information.') ->addOption('path', 'p', InputOption::VALUE_OPTIONAL, 'Get the detail of the specified route information by path') ->addOption('server', 'S', InputOption::VALUE_OPTIONAL, 'Which server you want to describe routes.', 'http'); } protected function analyzeRouter(string $server, RouteCollector $router, ?string $path) { $data = []; [$staticRouters, $variableRouters] = $router->getData(); foreach ($staticRouters as $method => $items) { foreach ($items as $handler) { $this->analyzeHandler($data, $server, $method, $path, $handler); } } foreach ($variableRouters as $method => $items) { foreach ($items as $item) { if (is_array($item['routeMap'] ?? false)) { foreach ($item['routeMap'] as $routeMap) { $this->analyzeHandler($data, $server, $method, $path, $routeMap[0]); } } } } return $data; } protected function analyzeHandler(array &$data, string $serverName, string $method, ?string $path, Handler $handler) { $uri = $handler->route; if (! is_null($path) && ! Str::contains($uri, $path)) { return; } if (is_array($handler->callback)) { $action = $handler->callback[0] . '::' . $handler->callback[1]; } elseif (is_string($handler->callback)) { $action = $handler->callback; } else { $action = 'Closure'; } $unique = "{$serverName}|{$uri}|{$action}"; if (isset($data[$unique])) { $data[$unique]['method'][] = $method; } else { // method,uri,name,action,middleware $registeredMiddlewares = MiddlewareManager::get($serverName, $uri, $method); $middlewares = $this->config->get('middlewares.' . $serverName, []); $middlewares = array_merge($middlewares, $registeredMiddlewares); $middlewares = MiddlewareManager::sortMiddlewares($middlewares); $data[$unique] = [ 'server' => $serverName, 'method' => [$method], 'uri' => $uri, 'action' => $action, 'middleware' => implode(PHP_EOL, $middlewares), ]; } } private function show(array $data, OutputInterface $output) { $rows = []; foreach ($data as $route) { $route['method'] = implode('|', $route['method']); $rows[] = $route; $rows[] = new TableSeparator(); } $rows = array_slice($rows, 0, count($rows) - 1); $table = new Table($output); $table ->setHeaders(['Server', 'Method', 'URI', 'Action', 'Middleware']) ->setRows($rows); $table->render(); } }