setDescription('Lists all routes registered in the application.'); } protected function execute(InputInterface $input, OutputInterface $output): int { // We need to instantiate the application to load the routes // But be careful not to execute session_start() if it is already active or causes output // Simulate application environment if (session_status() == PHP_SESSION_NONE) { // Avoid headers error if something attempts to start session // But Application initSession attempts to start. // Let's suppress session errors for the command @session_start(); } $app = new Application(); // The Application constructor already loads routes via initRouter and initModuleLoader // But we must ensure it loads everything that adds routes // Since Application is a singleton, we can try to get the instance or create it // If we create new Application(), it runs the constructor. $routes = $app->getRoutes(); if (empty($routes)) { $output->writeln('Nenhuma rota encontrada.'); return Command::SUCCESS; } $table = new Table($output); $table->setHeaders(['Método', 'URI', 'Handler']); foreach ($routes as $route) { [$method, $uri, $handler] = $route; $handlerName = 'Closure'; if (is_array($handler)) { if (is_object($handler[0])) { $handlerName = get_class($handler[0]) . '@' . $handler[1]; } elseif (is_string($handler[0])) { $handlerName = $handler[0] . '@' . $handler[1]; } } elseif (is_string($handler)) { $handlerName = $handler; } $table->addRow([ $method, $uri, $handlerName ]); } $table->render(); return Command::SUCCESS; } }