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