|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OC\Core\Command\Router; |
| 11 | + |
| 12 | +use OC\Core\Command\Base; |
| 13 | +use OC\Route\Route; |
| 14 | +use OC\Route\Router; |
| 15 | +use OCP\App\IAppManager; |
| 16 | +use Symfony\Component\Console\Input\InputArgument; |
| 17 | +use Symfony\Component\Console\Input\InputInterface; |
| 18 | +use Symfony\Component\Console\Input\InputOption; |
| 19 | +use Symfony\Component\Console\Output\OutputInterface; |
| 20 | + |
| 21 | +class ListRoutes extends Base { |
| 22 | + |
| 23 | + private InputInterface $input; |
| 24 | + |
| 25 | + public function __construct( |
| 26 | + private IAppManager $appManager, |
| 27 | + private Router $router, |
| 28 | + ) { |
| 29 | + parent::__construct(); |
| 30 | + } |
| 31 | + |
| 32 | + protected function configure(): void { |
| 33 | + parent::configure(); |
| 34 | + $this |
| 35 | + ->setName('router:list') |
| 36 | + ->setDescription('List registered routes') |
| 37 | + ->addArgument('appId', InputArgument::OPTIONAL, 'Limit routes to specific app', '') |
| 38 | + ->addOption('grouped', 'g', InputOption::VALUE_NONE, 'Group routes by app'); |
| 39 | + } |
| 40 | + |
| 41 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 42 | + $this->input = $input; |
| 43 | + |
| 44 | + $app = (string)$input->getArgument('appId'); |
| 45 | + $apps = $app === '' ? $this->appManager->getEnabledApps() : [$app]; |
| 46 | + |
| 47 | + $this->router->loadRoutes(); |
| 48 | + $allRoutes = []; |
| 49 | + foreach ($apps as $appId) { |
| 50 | + $routes = $this->router->getCollection($appId)->all(); |
| 51 | + $routes = array_merge($routes, $this->router->getCollection("$appId.ocs")->all()); |
| 52 | + $allRoutes = array_merge($allRoutes, $routes); |
| 53 | + |
| 54 | + if (!empty($routes) && $input->getOption('grouped')) { |
| 55 | + $output->writeln("\nRoutes of $appId:"); |
| 56 | + $rows = $this->formatRoutes($routes); |
| 57 | + $this->writeTableInOutputFormat($input, $output, $rows); |
| 58 | + } |
| 59 | + } |
| 60 | + if (!$input->getOption('grouped')) { |
| 61 | + $rows = $this->formatRoutes($allRoutes); |
| 62 | + $this->writeTableInOutputFormat($input, $output, $rows); |
| 63 | + } |
| 64 | + return 0; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @param Route[] $routes |
| 69 | + */ |
| 70 | + private function formatRoutes(array $routes): array { |
| 71 | + $rows = []; |
| 72 | + foreach ($routes as $route) { |
| 73 | + [$realApp, $controller, $function] = $route->getDefault('caller'); |
| 74 | + //print_r($route->__serialize()); |
| 75 | + $rows[] = [ |
| 76 | + 'app' => $realApp, |
| 77 | + 'controller' => $controller, |
| 78 | + 'function' => $function, |
| 79 | + 'method' => $route->getMethods()[0], |
| 80 | + 'path' => str_replace('/ocsapp', '/ocs/v2.php', $route->getPath()), |
| 81 | + 'defaults' => $this->formatDefaults($route->getDefaults()), |
| 82 | + 'requirements' => $this->formatArray($route->getRequirements()), |
| 83 | + ]; |
| 84 | + } |
| 85 | + return $rows; |
| 86 | + } |
| 87 | + |
| 88 | + private function formatDefaults(array $defaults): array|string { |
| 89 | + $defaults = array_filter($defaults, fn (string $name) => !in_array($name, ['action', 'caller']), ARRAY_FILTER_USE_KEY); |
| 90 | + return $this->formatArray($defaults); |
| 91 | + } |
| 92 | + |
| 93 | + private function formatArray(array $value): array|string { |
| 94 | + if (str_starts_with($this->input->getOption('output'), self::OUTPUT_FORMAT_JSON)) { |
| 95 | + return $value; |
| 96 | + } |
| 97 | + return empty($value) ? '' : json_encode($value); |
| 98 | + } |
| 99 | +} |
0 commit comments