Skip to content

Commit b95d300

Browse files
Merge pull request #53672 from nextcloud/backport/53669/stable31
[stable31] feat(occ): Add commands to list all routes and match a single one
2 parents 46794bd + d1a554f commit b95d300

File tree

6 files changed

+247
-2
lines changed

6 files changed

+247
-2
lines changed

core/Command/Router/ListRoutes.php

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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\Router;
14+
use OCP\App\AppPathNotFoundException;
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+
public function __construct(
24+
protected IAppManager $appManager,
25+
protected Router $router,
26+
) {
27+
parent::__construct();
28+
}
29+
30+
protected function configure(): void {
31+
parent::configure();
32+
$this
33+
->setName('router:list')
34+
->setDescription('Find the target of a route or all routes of an app')
35+
->addArgument(
36+
'app',
37+
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
38+
'Only list routes of these apps',
39+
)
40+
->addOption(
41+
'ocs',
42+
null,
43+
InputOption::VALUE_NONE,
44+
'Only list OCS routes',
45+
)
46+
->addOption(
47+
'index',
48+
null,
49+
InputOption::VALUE_NONE,
50+
'Only list index.php routes',
51+
)
52+
;
53+
}
54+
55+
protected function execute(InputInterface $input, OutputInterface $output): int {
56+
$apps = $input->getArgument('app');
57+
if (empty($apps)) {
58+
$this->router->loadRoutes();
59+
} else {
60+
foreach ($apps as $app) {
61+
if ($app === 'core') {
62+
$this->router->loadRoutes($app, false);
63+
continue;
64+
}
65+
66+
try {
67+
$this->appManager->getAppPath($app);
68+
} catch (AppPathNotFoundException) {
69+
$output->writeln('<comment>App ' . $app . ' not found</comment>');
70+
return self::FAILURE;
71+
}
72+
73+
if (!$this->appManager->isInstalled($app)) {
74+
$output->writeln('<comment>App ' . $app . ' is not enabled</comment>');
75+
return self::FAILURE;
76+
}
77+
78+
$this->router->loadRoutes($app, true);
79+
}
80+
}
81+
82+
$ocsOnly = $input->getOption('ocs');
83+
$indexOnly = $input->getOption('index');
84+
85+
$rows = [];
86+
$collection = $this->router->getRouteCollection();
87+
foreach ($collection->all() as $routeName => $route) {
88+
if (str_starts_with($routeName, 'ocs.')) {
89+
if ($indexOnly) {
90+
continue;
91+
}
92+
$routeName = substr($routeName, 4);
93+
} elseif ($ocsOnly) {
94+
continue;
95+
}
96+
97+
$path = $route->getPath();
98+
if (str_starts_with($path, '/ocsapp/')) {
99+
$path = '/ocs/v2.php/' . substr($path, strlen('/ocsapp/'));
100+
}
101+
$row = [
102+
'route' => $routeName,
103+
'request' => implode(', ', $route->getMethods()),
104+
'path' => $path,
105+
];
106+
107+
if ($output->isVerbose()) {
108+
$row['requirements'] = json_encode($route->getRequirements());
109+
}
110+
111+
$rows[] = $row;
112+
}
113+
114+
usort($rows, static function (array $a, array $b): int {
115+
$aRoute = $a['route'];
116+
if (str_starts_with($aRoute, 'ocs.')) {
117+
$aRoute = substr($aRoute, 4);
118+
}
119+
$bRoute = $b['route'];
120+
if (str_starts_with($bRoute, 'ocs.')) {
121+
$bRoute = substr($bRoute, 4);
122+
}
123+
return $aRoute <=> $bRoute;
124+
});
125+
126+
$this->writeTableInOutputFormat($input, $output, $rows);
127+
return self::SUCCESS;
128+
}
129+
}

core/Command/Router/MatchRoute.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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\Router;
14+
use OCP\App\IAppManager;
15+
use OCP\Server;
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+
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
21+
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
22+
use Symfony\Component\Routing\RequestContext;
23+
24+
class MatchRoute extends Base {
25+
26+
public function __construct(
27+
private Router $router,
28+
) {
29+
parent::__construct();
30+
}
31+
32+
protected function configure(): void {
33+
parent::configure();
34+
$this
35+
->setName('router:match')
36+
->setDescription('Match a URL to the target route')
37+
->addArgument(
38+
'path',
39+
InputArgument::REQUIRED,
40+
'Path of the request',
41+
)
42+
->addOption(
43+
'method',
44+
null,
45+
InputOption::VALUE_REQUIRED,
46+
'HTTP method',
47+
'GET',
48+
)
49+
;
50+
}
51+
52+
protected function execute(InputInterface $input, OutputInterface $output): int {
53+
$context = new RequestContext(method: strtoupper($input->getOption('method')));
54+
$this->router->setContext($context);
55+
56+
$path = $input->getArgument('path');
57+
if (str_starts_with($path, '/index.php/')) {
58+
$path = substr($path, 10);
59+
}
60+
if (str_starts_with($path, '/ocs/v1.php/') || str_starts_with($path, '/ocs/v2.php/')) {
61+
$path = '/ocsapp' . substr($path, strlen('/ocs/v2.php'));
62+
}
63+
64+
try {
65+
$route = $this->router->findMatchingRoute($path);
66+
} catch (MethodNotAllowedException) {
67+
$output->writeln('<error>Method not allowed on this path</error>');
68+
return self::FAILURE;
69+
} catch (ResourceNotFoundException) {
70+
$output->writeln('<error>Path not matched</error>');
71+
if (preg_match('/\/apps\/([^\/]+)\//', $path, $matches)) {
72+
$appManager = Server::get(IAppManager::class);
73+
if (!$appManager->isInstalled($matches[1])) {
74+
$output->writeln('');
75+
$output->writeln('<comment>App ' . $matches[1] . ' is not enabled</comment>');
76+
}
77+
}
78+
return self::FAILURE;
79+
}
80+
81+
$row = [
82+
'route' => $route['_route'],
83+
'appid' => $route['caller'][0] ?? null,
84+
'controller' => $route['caller'][1] ?? null,
85+
'method' => $route['caller'][2] ?? null,
86+
];
87+
88+
if ($output->isVerbose()) {
89+
$route = $this->router->getRouteCollection()->get($row['route']);
90+
$row['path'] = $route->getPath();
91+
if (str_starts_with($row['path'], '/ocsapp/')) {
92+
$row['path'] = '/ocs/v2.php/' . substr($row['path'], strlen('/ocsapp/'));
93+
}
94+
$row['requirements'] = json_encode($route->getRequirements());
95+
}
96+
97+
$this->writeTableInOutputFormat($input, $output, [$row]);
98+
return self::SUCCESS;
99+
}
100+
}

core/register_command.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* SPDX-License-Identifier: AGPL-3.0-only
99
*/
1010
use OC\Core\Command;
11+
use OC\Core\Command\Router\ListRoutes;
12+
use OC\Core\Command\Router\MatchRoute;
1113
use OCP\IConfig;
1214
use OCP\Server;
1315
use Stecman\Component\Symfony\Console\BashCompletion\CompletionCommand;
@@ -20,6 +22,8 @@
2022
$application->add(Server::get(Command\Integrity\SignCore::class));
2123
$application->add(Server::get(Command\Integrity\CheckApp::class));
2224
$application->add(Server::get(Command\Integrity\CheckCore::class));
25+
$application->add(Server::get(ListRoutes::class));
26+
$application->add(Server::get(MatchRoute::class));
2327

2428
$config = Server::get(IConfig::class);
2529

lib/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,8 @@
12881288
'OC\\Core\\Command\\Preview\\Generate' => $baseDir . '/core/Command/Preview/Generate.php',
12891289
'OC\\Core\\Command\\Preview\\Repair' => $baseDir . '/core/Command/Preview/Repair.php',
12901290
'OC\\Core\\Command\\Preview\\ResetRenderedTexts' => $baseDir . '/core/Command/Preview/ResetRenderedTexts.php',
1291+
'OC\\Core\\Command\\Router\\ListRoutes' => $baseDir . '/core/Command/Router/ListRoutes.php',
1292+
'OC\\Core\\Command\\Router\\MatchRoute' => $baseDir . '/core/Command/Router/MatchRoute.php',
12911293
'OC\\Core\\Command\\Security\\BruteforceAttempts' => $baseDir . '/core/Command/Security/BruteforceAttempts.php',
12921294
'OC\\Core\\Command\\Security\\BruteforceResetAttempts' => $baseDir . '/core/Command/Security/BruteforceResetAttempts.php',
12931295
'OC\\Core\\Command\\Security\\ExportCertificates' => $baseDir . '/core/Command/Security/ExportCertificates.php',

lib/composer/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,8 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
13371337
'OC\\Core\\Command\\Preview\\Generate' => __DIR__ . '/../../..' . '/core/Command/Preview/Generate.php',
13381338
'OC\\Core\\Command\\Preview\\Repair' => __DIR__ . '/../../..' . '/core/Command/Preview/Repair.php',
13391339
'OC\\Core\\Command\\Preview\\ResetRenderedTexts' => __DIR__ . '/../../..' . '/core/Command/Preview/ResetRenderedTexts.php',
1340+
'OC\\Core\\Command\\Router\\ListRoutes' => __DIR__ . '/../../..' . '/core/Command/Router/ListRoutes.php',
1341+
'OC\\Core\\Command\\Router\\MatchRoute' => __DIR__ . '/../../..' . '/core/Command/Router/MatchRoute.php',
13401342
'OC\\Core\\Command\\Security\\BruteforceAttempts' => __DIR__ . '/../../..' . '/core/Command/Security/BruteforceAttempts.php',
13411343
'OC\\Core\\Command\\Security\\BruteforceResetAttempts' => __DIR__ . '/../../..' . '/core/Command/Security/BruteforceResetAttempts.php',
13421344
'OC\\Core\\Command\\Security\\ExportCertificates' => __DIR__ . '/../../..' . '/core/Command/Security/ExportCertificates.php',

lib/private/Route/Router.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ public function __construct(
7474
$this->root = $this->getCollection('root');
7575
}
7676

77+
public function setContext(RequestContext $context): void {
78+
$this->context = $context;
79+
}
80+
81+
public function getRouteCollection() {
82+
return $this->root;
83+
}
84+
7785
/**
7886
* Get the files to load the routes from
7987
*
@@ -102,7 +110,7 @@ public function getRoutingFiles() {
102110
*
103111
* @param null|string $app
104112
*/
105-
public function loadRoutes($app = null) {
113+
public function loadRoutes(?string $app = null, bool $skipLoadingCore = false): void {
106114
if (is_string($app)) {
107115
$app = $this->appManager->cleanAppId($app);
108116
}
@@ -161,7 +169,7 @@ public function loadRoutes($app = null) {
161169
}
162170
}
163171

164-
if (!isset($this->loadedApps['core'])) {
172+
if (!$skipLoadingCore && !isset($this->loadedApps['core'])) {
165173
$this->loadedApps['core'] = true;
166174
$this->useCollection('root');
167175
$this->setupRoutes($this->getAttributeRoutes('core'), 'core');

0 commit comments

Comments
 (0)