From 874bb55fcf16f60f4efc09d8eba85c68c0280b88 Mon Sep 17 00:00:00 2001 From: hatik92 Date: Fri, 19 Dec 2025 19:21:34 +0400 Subject: [PATCH] Implement route:list CLI command for Quantum modules --- src/Console/Commands/RouteListCommand.php | 122 ++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 src/Console/Commands/RouteListCommand.php diff --git a/src/Console/Commands/RouteListCommand.php b/src/Console/Commands/RouteListCommand.php new file mode 100644 index 000000000..672f1a373 --- /dev/null +++ b/src/Console/Commands/RouteListCommand.php @@ -0,0 +1,122 @@ + + * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) + * @link http://quantum.softberg.org/ + * @since 2.9.9 + */ + +namespace Quantum\Console\Commands; + +use Quantum\Module\Exceptions\ModuleException; +use Quantum\Router\Exceptions\RouteException; +use Symfony\Component\Console\Helper\Table; +use Quantum\Module\ModuleLoader; +use Quantum\Console\QtCommand; +use Quantum\Router\Router; + +/** + * Class ServeCommand + * @package Quantum\Console + */ +class RouteListCommand extends QtCommand +{ + /** + * The console command name. + * @var string + */ + protected $name = 'route:list'; + + /** + * The console command description. + * @var string + */ + protected $description = 'Display all registered routes'; + + /** + * Command options + * @var string + */ + protected $options = [ + ['module', 'm', 'optional', 'Filter by module name'], + ]; + + /** + * Executes the command + */ + public function exec() + { + try { + $modulesRoutes = ModuleLoader::getInstance()->loadModulesRoutes(); + + Router::setRoutes($modulesRoutes); + + $routes = Router::getRoutes(); + + $module = $this->getOption('module'); + + if ($module) { + $routes = array_filter($routes, function ($route) use ($module) { + return strtolower($route['module']) === strtolower($module); + }); + + if (empty($routes)) { + $this->error('The module is not found'); + return; + } + } + + $rows = []; + + foreach ($routes as $route) { + $rows[] = $this->composeTableRow($route, 50); + } + + $table = new Table($this->output); + + $table->setHeaderTitle('Routes') + ->setHeaders(['MODULE', 'METHOD', 'URI', 'ACTION', 'MIDDLEWARE']) + ->setRows($rows) + ->render(); + } catch (ModuleException|RouteException $e) { + $this->error($e->getMessage()); + } + } + + /** + * Composes a table row + * @param array $route + * @param int $maxContentLength + * @return array + */ + private function composeTableRow(array $route, int $maxContentLength = 25): array + { + $action = $route['action'] + . '\\' + . $route['controller'] + . '@' + . $route['action']; + + if (mb_strlen($action) > $maxContentLength) { + $action = mb_substr($action, 0, $maxContentLength) . '...'; + } + + $middlewares = isset($route['middlewares']) + ? implode(',', $route['middlewares']) + : '-'; + + return [ + $route['module'] ?? '', + $route['method'] ?? '', + $route['route'] ?? '', + $action, + $middlewares, + ]; + } +} \ No newline at end of file