Skip to content

Commit 1886595

Browse files
committed
feat: add command to list all routes registered
Signed-off-by: Ferdinand Thiessen <[email protected]>
1 parent 4a71e8d commit 1886595

4 files changed

Lines changed: 104 additions & 0 deletions

File tree

core/Command/Router/ListRoutes.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
}

core/register_command.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
use OC\Core\Command\Memcache\RedisCommand;
7272
use OC\Core\Command\Preview\Generate;
7373
use OC\Core\Command\Preview\ResetRenderedTexts;
74+
use OC\Core\Command\Router\ListRoutes;
7475
use OC\Core\Command\Security\BruteforceAttempts;
7576
use OC\Core\Command\Security\BruteforceResetAttempts;
7677
use OC\Core\Command\Security\ExportCertificates;
@@ -243,6 +244,8 @@
243244
$application->add(Server::get(Statistics::class));
244245

245246
$application->add(Server::get(RedisCommand::class));
247+
248+
$application->add(Server::get(ListRoutes::class));
246249
} else {
247250
$application->add(Server::get(Command\Maintenance\Install::class));
248251
}

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,7 @@
12901290
'OC\\Core\\Command\\Preview\\Generate' => $baseDir . '/core/Command/Preview/Generate.php',
12911291
'OC\\Core\\Command\\Preview\\Repair' => $baseDir . '/core/Command/Preview/Repair.php',
12921292
'OC\\Core\\Command\\Preview\\ResetRenderedTexts' => $baseDir . '/core/Command/Preview/ResetRenderedTexts.php',
1293+
'OC\\Core\\Command\\Router\\ListRoutes' => $baseDir . '/core/Command/Router/ListRoutes.php',
12931294
'OC\\Core\\Command\\Security\\BruteforceAttempts' => $baseDir . '/core/Command/Security/BruteforceAttempts.php',
12941295
'OC\\Core\\Command\\Security\\BruteforceResetAttempts' => $baseDir . '/core/Command/Security/BruteforceResetAttempts.php',
12951296
'OC\\Core\\Command\\Security\\ExportCertificates' => $baseDir . '/core/Command/Security/ExportCertificates.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
13311331
'OC\\Core\\Command\\Preview\\Generate' => __DIR__ . '/../../..' . '/core/Command/Preview/Generate.php',
13321332
'OC\\Core\\Command\\Preview\\Repair' => __DIR__ . '/../../..' . '/core/Command/Preview/Repair.php',
13331333
'OC\\Core\\Command\\Preview\\ResetRenderedTexts' => __DIR__ . '/../../..' . '/core/Command/Preview/ResetRenderedTexts.php',
1334+
'OC\\Core\\Command\\Router\\ListRoutes' => __DIR__ . '/../../..' . '/core/Command/Router/ListRoutes.php',
13341335
'OC\\Core\\Command\\Security\\BruteforceAttempts' => __DIR__ . '/../../..' . '/core/Command/Security/BruteforceAttempts.php',
13351336
'OC\\Core\\Command\\Security\\BruteforceResetAttempts' => __DIR__ . '/../../..' . '/core/Command/Security/BruteforceResetAttempts.php',
13361337
'OC\\Core\\Command\\Security\\ExportCertificates' => __DIR__ . '/../../..' . '/core/Command/Security/ExportCertificates.php',

0 commit comments

Comments
 (0)