|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +/** |
| 5 | + * Copyright 2016 - 2024, Cake Development Corporation (http://cakedc.com) |
| 6 | + * |
| 7 | + * Licensed under The MIT License |
| 8 | + * Redistributions of files must retain the above copyright notice. |
| 9 | + * |
| 10 | + * @copyright Copyright 2016 - 2024, Cake Development Corporation (http://cakedc.com) |
| 11 | + * @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CakeDC\Api\Command; |
| 15 | + |
| 16 | +use Cake\Command\Command; |
| 17 | +use Cake\Console\Arguments; |
| 18 | +use Cake\Console\ConsoleIo; |
| 19 | +use Cake\Console\ConsoleOptionParser; |
| 20 | +use CakeDC\Api\Service\ServiceRegistry; |
| 21 | + |
| 22 | +/** |
| 23 | + * Provides interactive CLI tools for CakeDC Api routing. |
| 24 | + */ |
| 25 | +class ServiceRoutesCommand extends Command |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @inheritDoc |
| 29 | + */ |
| 30 | + public static function defaultName(): string |
| 31 | + { |
| 32 | + return 'service routes'; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Build the option parser. |
| 37 | + * |
| 38 | + * @param \Cake\Console\ConsoleOptionParser $parser The option parser to update |
| 39 | + * @return \Cake\Console\ConsoleOptionParser |
| 40 | + */ |
| 41 | + protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser |
| 42 | + { |
| 43 | + $parser = parent::buildOptionParser($parser); |
| 44 | + $parser->setDescription(__('Display all routes in a service')); |
| 45 | + $parser->addArgument('service', [ |
| 46 | + 'help' => __('The name of the service to display routes for.'), |
| 47 | + 'required' => true, |
| 48 | + ]); |
| 49 | + |
| 50 | + return $parser; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Display all routes in an application |
| 55 | + * |
| 56 | + * @param \Cake\Console\Arguments $args The command arguments. |
| 57 | + * @param \Cake\Console\ConsoleIo $io The console io |
| 58 | + * @return int|null The exit code or null for success |
| 59 | + */ |
| 60 | + public function execute(Arguments $args, ConsoleIo $io): ?int |
| 61 | + { |
| 62 | + $serviceName = $args->getArgument('service'); |
| 63 | + $header = ['Route name', 'Method(s)', 'URI template', 'Service', 'Action', 'Plugin']; |
| 64 | + if ($args->getOption('verbose')) { |
| 65 | + $header[] = 'Defaults'; |
| 66 | + } |
| 67 | + |
| 68 | + $service = ServiceRegistry::getServiceLocator()->get($serviceName); |
| 69 | + if ($service === null) { |
| 70 | + $io->error(__('Service "{0}" not found', $serviceName)); |
| 71 | + |
| 72 | + return Command::CODE_ERROR; |
| 73 | + } |
| 74 | + |
| 75 | + $availableRoutes = $service->routes(); |
| 76 | + |
| 77 | + $output = $duplicateRoutesCounter = []; |
| 78 | + |
| 79 | + foreach ($availableRoutes as $route) { |
| 80 | + $methods = isset($route->defaults['_method']) ? (array)$route->defaults['_method'] : ['']; |
| 81 | + |
| 82 | + $item = [ |
| 83 | + $route->options['_name'] ?? $route->getName(), |
| 84 | + implode(', ', $methods), |
| 85 | + $route->template, |
| 86 | + $route->defaults['controller'] ?? '', |
| 87 | + $route->defaults['action'] ?? '', |
| 88 | + $route->defaults['plugin'] ?? '', |
| 89 | + ]; |
| 90 | + |
| 91 | + if ($args->getOption('verbose')) { |
| 92 | + ksort($route->defaults); |
| 93 | + $item[] = json_encode($route->defaults); |
| 94 | + } |
| 95 | + |
| 96 | + $output[] = $item; |
| 97 | + |
| 98 | + foreach ($methods as $method) { |
| 99 | + if (!isset($duplicateRoutesCounter[$route->template][$method])) { |
| 100 | + $duplicateRoutesCounter[$route->template][$method] = 0; |
| 101 | + } |
| 102 | + |
| 103 | + $duplicateRoutesCounter[$route->template][$method]++; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + if ($args->getOption('sort')) { |
| 108 | + usort($output, function ($a, $b) { |
| 109 | + return strcasecmp($a[0], $b[0]); |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + array_unshift($output, $header); |
| 114 | + |
| 115 | + $io->helper('table')->output($output); |
| 116 | + $io->out(); |
| 117 | + |
| 118 | + $duplicateRoutes = []; |
| 119 | + |
| 120 | + foreach ($availableRoutes as $route) { |
| 121 | + $methods = isset($route->defaults['_method']) ? (array)$route->defaults['_method'] : ['']; |
| 122 | + |
| 123 | + foreach ($methods as $method) { |
| 124 | + if ( |
| 125 | + $duplicateRoutesCounter[$route->template][$method] > 1 || |
| 126 | + ($method === '' && count($duplicateRoutesCounter[$route->template]) > 1) || |
| 127 | + ($method !== '' && isset($duplicateRoutesCounter[$route->template][''])) |
| 128 | + ) { |
| 129 | + $duplicateRoutes[] = [ |
| 130 | + $route->options['_name'] ?? $route->getName(), |
| 131 | + $route->template, |
| 132 | + $route->defaults['plugin'] ?? '', |
| 133 | + $route->defaults['prefix'] ?? '', |
| 134 | + $route->defaults['controller'] ?? '', |
| 135 | + $route->defaults['action'] ?? '', |
| 136 | + implode(', ', $methods), |
| 137 | + ]; |
| 138 | + |
| 139 | + break; |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + if ($duplicateRoutes) { |
| 145 | + array_unshift($duplicateRoutes, $header); |
| 146 | + $io->warning('The following possible route collisions were detected.'); |
| 147 | + $io->helper('table')->output($duplicateRoutes); |
| 148 | + $io->out(); |
| 149 | + } |
| 150 | + |
| 151 | + return static::CODE_SUCCESS; |
| 152 | + } |
| 153 | +} |
0 commit comments