aboutsummaryrefslogtreecommitdiffstats
path: root/core/Command/Router
diff options
context:
space:
mode:
Diffstat (limited to 'core/Command/Router')
-rw-r--r--core/Command/Router/ListRoutes.php129
-rw-r--r--core/Command/Router/MatchRoute.php100
2 files changed, 229 insertions, 0 deletions
diff --git a/core/Command/Router/ListRoutes.php b/core/Command/Router/ListRoutes.php
new file mode 100644
index 00000000000..8932b549a65
--- /dev/null
+++ b/core/Command/Router/ListRoutes.php
@@ -0,0 +1,129 @@
+<?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\Router;
+use OCP\App\AppPathNotFoundException;
+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 {
+
+ public function __construct(
+ protected IAppManager $appManager,
+ protected Router $router,
+ ) {
+ parent::__construct();
+ }
+
+ protected function configure(): void {
+ parent::configure();
+ $this
+ ->setName('router:list')
+ ->setDescription('Find the target of a route or all routes of an app')
+ ->addArgument(
+ 'app',
+ InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
+ 'Only list routes of these apps',
+ )
+ ->addOption(
+ 'ocs',
+ null,
+ InputOption::VALUE_NONE,
+ 'Only list OCS routes',
+ )
+ ->addOption(
+ 'index',
+ null,
+ InputOption::VALUE_NONE,
+ 'Only list index.php routes',
+ )
+ ;
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output): int {
+ $apps = $input->getArgument('app');
+ if (empty($apps)) {
+ $this->router->loadRoutes();
+ } else {
+ foreach ($apps as $app) {
+ if ($app === 'core') {
+ $this->router->loadRoutes($app, false);
+ continue;
+ }
+
+ try {
+ $this->appManager->getAppPath($app);
+ } catch (AppPathNotFoundException $e) {
+ $output->writeln('<comment>App ' . $app . ' not found</comment>');
+ return self::FAILURE;
+ }
+
+ if (!$this->appManager->isEnabledForAnyone($app)) {
+ $output->writeln('<comment>App ' . $app . ' is not enabled</comment>');
+ return self::FAILURE;
+ }
+
+ $this->router->loadRoutes($app, true);
+ }
+ }
+
+ $ocsOnly = $input->getOption('ocs');
+ $indexOnly = $input->getOption('index');
+
+ $rows = [];
+ $collection = $this->router->getRouteCollection();
+ foreach ($collection->all() as $routeName => $route) {
+ if (str_starts_with($routeName, 'ocs.')) {
+ if ($indexOnly) {
+ continue;
+ }
+ $routeName = substr($routeName, 4);
+ } elseif ($ocsOnly) {
+ continue;
+ }
+
+ $path = $route->getPath();
+ if (str_starts_with($path, '/ocsapp/')) {
+ $path = '/ocs/v2.php/' . substr($path, strlen('/ocsapp/'));
+ }
+ $row = [
+ 'route' => $routeName,
+ 'request' => implode(', ', $route->getMethods()),
+ 'path' => $path,
+ ];
+
+ if ($output->isVerbose()) {
+ $row['requirements'] = json_encode($route->getRequirements());
+ }
+
+ $rows[] = $row;
+ }
+
+ usort($rows, static function (array $a, array $b): int {
+ $aRoute = $a['route'];
+ if (str_starts_with($aRoute, 'ocs.')) {
+ $aRoute = substr($aRoute, 4);
+ }
+ $bRoute = $b['route'];
+ if (str_starts_with($bRoute, 'ocs.')) {
+ $bRoute = substr($bRoute, 4);
+ }
+ return $aRoute <=> $bRoute;
+ });
+
+ $this->writeTableInOutputFormat($input, $output, $rows);
+ return self::SUCCESS;
+ }
+}
diff --git a/core/Command/Router/MatchRoute.php b/core/Command/Router/MatchRoute.php
new file mode 100644
index 00000000000..3b90463c7b2
--- /dev/null
+++ b/core/Command/Router/MatchRoute.php
@@ -0,0 +1,100 @@
+<?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\Router;
+use OCP\App\IAppManager;
+use OCP\Server;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Routing\Exception\MethodNotAllowedException;
+use Symfony\Component\Routing\Exception\ResourceNotFoundException;
+use Symfony\Component\Routing\RequestContext;
+
+class MatchRoute extends Base {
+
+ public function __construct(
+ private Router $router,
+ ) {
+ parent::__construct();
+ }
+
+ protected function configure(): void {
+ parent::configure();
+ $this
+ ->setName('router:match')
+ ->setDescription('Match a URL to the target route')
+ ->addArgument(
+ 'path',
+ InputArgument::REQUIRED,
+ 'Path of the request',
+ )
+ ->addOption(
+ 'method',
+ null,
+ InputOption::VALUE_REQUIRED,
+ 'HTTP method',
+ 'GET',
+ )
+ ;
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output): int {
+ $context = new RequestContext(method: strtoupper($input->getOption('method')));
+ $this->router->setContext($context);
+
+ $path = $input->getArgument('path');
+ if (str_starts_with($path, '/index.php/')) {
+ $path = substr($path, 10);
+ }
+ if (str_starts_with($path, '/ocs/v1.php/') || str_starts_with($path, '/ocs/v2.php/')) {
+ $path = '/ocsapp' . substr($path, strlen('/ocs/v2.php'));
+ }
+
+ try {
+ $route = $this->router->findMatchingRoute($path);
+ } catch (MethodNotAllowedException) {
+ $output->writeln('<error>Method not allowed on this path</error>');
+ return self::FAILURE;
+ } catch (ResourceNotFoundException) {
+ $output->writeln('<error>Path not matched</error>');
+ if (preg_match('/\/apps\/([^\/]+)\//', $path, $matches)) {
+ $appManager = Server::get(IAppManager::class);
+ if (!$appManager->isEnabledForAnyone($matches[1])) {
+ $output->writeln('');
+ $output->writeln('<comment>App ' . $matches[1] . ' is not enabled</comment>');
+ }
+ }
+ return self::FAILURE;
+ }
+
+ $row = [
+ 'route' => $route['_route'],
+ 'appid' => $route['caller'][0] ?? null,
+ 'controller' => $route['caller'][1] ?? null,
+ 'method' => $route['caller'][2] ?? null,
+ ];
+
+ if ($output->isVerbose()) {
+ $route = $this->router->getRouteCollection()->get($row['route']);
+ $row['path'] = $route->getPath();
+ if (str_starts_with($row['path'], '/ocsapp/')) {
+ $row['path'] = '/ocs/v2.php/' . substr($row['path'], strlen('/ocsapp/'));
+ }
+ $row['requirements'] = json_encode($route->getRequirements());
+ }
+
+ $this->writeTableInOutputFormat($input, $output, [$row]);
+ return self::SUCCESS;
+ }
+}