|
| 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 $e) { |
| 69 | + $output->writeln('<comment>App ' . $app . ' not found</comment>'); |
| 70 | + return self::FAILURE; |
| 71 | + } |
| 72 | + |
| 73 | + if (!$this->appManager->isEnabledForAnyone($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 | +} |
0 commit comments