Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions build/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4335,14 +4335,6 @@
<code><![CDATA[$out]]></code>
</ParamNameMismatch>
</file>
<file src="lib/private/Route/Router.php">
<InvalidNullableReturnType>
<code><![CDATA[string]]></code>
</InvalidNullableReturnType>
<NullableReturnStatement>
<code><![CDATA[$this->collectionName]]></code>
</NullableReturnStatement>
</file>
<file src="lib/private/Security/CSP/ContentSecurityPolicyNonceManager.php">
<NoInterfaceProperties>
<code><![CDATA[$this->request->server]]></code>
Expand Down
99 changes: 99 additions & 0 deletions core/Command/Router/ListRoutes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OC\Core\Command\Router;

use OC\Core\Command\Base;
use OC\Route\Route;
use OC\Route\Router;
use OCP\App\IAppManager;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class ListRoutes extends Base {

private InputInterface $input;

public function __construct(
private IAppManager $appManager,
private Router $router,
) {
parent::__construct();
}

protected function configure(): void {
parent::configure();
$this
->setName('router:list')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#[AsCommand(
	name: 'router:list',
	description: 'List registered routes',
)]

nit

->setDescription('List registered routes')
->addArgument('appId', InputArgument::OPTIONAL, 'Limit routes to specific app', '')
->addOption('grouped', 'g', InputOption::VALUE_NONE, 'Group routes by app');
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$this->input = $input;

$app = (string)$input->getArgument('appId');
$apps = $app === '' ? $this->appManager->getEnabledApps() : [$app];

$this->router->loadRoutes();
$allRoutes = [];
foreach ($apps as $appId) {
$routes = $this->router->getCollection($appId)->all();
$routes = array_merge($routes, $this->router->getCollection("$appId.ocs")->all());
$allRoutes = array_merge($allRoutes, $routes);

if (!empty($routes) && $input->getOption('grouped')) {
$output->writeln("\nRoutes of $appId:");
$rows = $this->formatRoutes($routes);
$this->writeTableInOutputFormat($input, $output, $rows);
}
}
if (!$input->getOption('grouped')) {
$rows = $this->formatRoutes($allRoutes);
$this->writeTableInOutputFormat($input, $output, $rows);
}
return 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return 0;
return self::SUCCESS;

}

/**
* @param Route[] $routes
*/
private function formatRoutes(array $routes): array {
$rows = [];
foreach ($routes as $route) {
[$realApp, $controller, $function] = $route->getDefault('caller');
//print_r($route->__serialize());
$rows[] = [
'app' => $realApp,
'controller' => $controller,
'function' => $function,
'method' => $route->getMethods()[0],
'path' => str_replace('/ocsapp', '/ocs/v2.php', $route->getPath()),
'defaults' => $this->formatDefaults($route->getDefaults()),
'requirements' => $this->formatArray($route->getRequirements()),
];
}
return $rows;
}

private function formatDefaults(array $defaults): array|string {
$defaults = array_filter($defaults, fn (string $name) => !in_array($name, ['action', 'caller']), ARRAY_FILTER_USE_KEY);
return $this->formatArray($defaults);
}

private function formatArray(array $value): array|string {
if (str_starts_with($this->input->getOption('output'), self::OUTPUT_FORMAT_JSON)) {
return $value;
}
return empty($value) ? '' : json_encode($value);
}
}
3 changes: 3 additions & 0 deletions core/register_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
use OC\Core\Command\Memcache\RedisCommand;
use OC\Core\Command\Preview\Generate;
use OC\Core\Command\Preview\ResetRenderedTexts;
use OC\Core\Command\Router\ListRoutes;
use OC\Core\Command\Security\BruteforceAttempts;
use OC\Core\Command\Security\BruteforceResetAttempts;
use OC\Core\Command\Security\ExportCertificates;
Expand Down Expand Up @@ -243,6 +244,8 @@
$application->add(Server::get(Statistics::class));

$application->add(Server::get(RedisCommand::class));

$application->add(Server::get(ListRoutes::class));
} else {
$application->add(Server::get(Command\Maintenance\Install::class));
}
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,7 @@
'OC\\Core\\Command\\Preview\\Generate' => $baseDir . '/core/Command/Preview/Generate.php',
'OC\\Core\\Command\\Preview\\Repair' => $baseDir . '/core/Command/Preview/Repair.php',
'OC\\Core\\Command\\Preview\\ResetRenderedTexts' => $baseDir . '/core/Command/Preview/ResetRenderedTexts.php',
'OC\\Core\\Command\\Router\\ListRoutes' => $baseDir . '/core/Command/Router/ListRoutes.php',
'OC\\Core\\Command\\Security\\BruteforceAttempts' => $baseDir . '/core/Command/Security/BruteforceAttempts.php',
'OC\\Core\\Command\\Security\\BruteforceResetAttempts' => $baseDir . '/core/Command/Security/BruteforceResetAttempts.php',
'OC\\Core\\Command\\Security\\ExportCertificates' => $baseDir . '/core/Command/Security/ExportCertificates.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Core\\Command\\Preview\\Generate' => __DIR__ . '/../../..' . '/core/Command/Preview/Generate.php',
'OC\\Core\\Command\\Preview\\Repair' => __DIR__ . '/../../..' . '/core/Command/Preview/Repair.php',
'OC\\Core\\Command\\Preview\\ResetRenderedTexts' => __DIR__ . '/../../..' . '/core/Command/Preview/ResetRenderedTexts.php',
'OC\\Core\\Command\\Router\\ListRoutes' => __DIR__ . '/../../..' . '/core/Command/Router/ListRoutes.php',
'OC\\Core\\Command\\Security\\BruteforceAttempts' => __DIR__ . '/../../..' . '/core/Command/Security/BruteforceAttempts.php',
'OC\\Core\\Command\\Security\\BruteforceResetAttempts' => __DIR__ . '/../../..' . '/core/Command/Security/BruteforceResetAttempts.php',
'OC\\Core\\Command\\Security\\ExportCertificates' => __DIR__ . '/../../..' . '/core/Command/Security/ExportCertificates.php',
Expand Down
11 changes: 6 additions & 5 deletions lib/private/Route/CachingRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ public function __construct(
*
* @param string $name Name of the route to use.
* @param array $parameters Parameters for the route
* @param bool $absolute
* @return string
*/
public function generate($name, $parameters = [], $absolute = false) {
public function generate(
string $name,
array $parameters = [],
bool $absolute = false,
): string {
asort($parameters);
$key = $this->context->getHost() . '#' . $this->context->getBaseUrl() . $name . sha1(json_encode($parameters)) . (int)$absolute;
$cachedKey = $this->cache->get($key);
Expand Down Expand Up @@ -115,7 +117,6 @@ protected function callLegacyActionRoute(array $parameters): void {
* Closures cannot be serialized to cache, so for legacy routes calling an action we have to include the routes.php file again
*/
$app = $parameters['app'];
$this->useCollection($app);
parent::requireRouteFile($parameters['route-file'], $app);
$collection = $this->getCollection($app);
$parameters['action'] = $collection->get($parameters['_route'])?->getDefault('action');
Expand Down Expand Up @@ -143,7 +144,7 @@ protected function requireRouteFile(string $file, string $appName): void {
$this->legacyCreatedRoutes = [];
parent::requireRouteFile($file, $appName);
foreach ($this->legacyCreatedRoutes as $routeName) {
$route = $this->collection?->get($routeName);
$route = $this->getCollection($appName)->get($routeName);
if ($route === null) {
/* Should never happen */
throw new \Exception("Could not find route $routeName");
Expand Down
Loading
Loading