forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoutes.php
More file actions
231 lines (185 loc) · 6.99 KB
/
Routes.php
File metadata and controls
231 lines (185 loc) · 6.99 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Commands\Utilities;
use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use CodeIgniter\Commands\Utilities\Routes\AutoRouteCollector;
use CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\AutoRouteCollector as AutoRouteCollectorImproved;
use CodeIgniter\Commands\Utilities\Routes\FilterCollector;
use CodeIgniter\Commands\Utilities\Routes\SampleURIGenerator;
use CodeIgniter\Router\DefinedRouteCollector;
use CodeIgniter\Router\Router;
use Config\Feature;
use Config\Routing;
/**
* Lists all the routes. This will include any Routes files
* that can be discovered, and will include routes that are not defined
* in routes files, but are instead discovered through auto-routing.
*/
class Routes extends BaseCommand
{
/**
* The group the command is lumped under
* when listing commands.
*
* @var string
*/
protected $group = 'CodeIgniter';
/**
* The Command's name
*
* @var string
*/
protected $name = 'routes';
/**
* the Command's short description
*
* @var string
*/
protected $description = 'Displays all routes.';
/**
* the Command's usage
*
* @var string
*/
protected $usage = 'routes';
/**
* the Command's Arguments
*
* @var array<string, string>
*/
protected $arguments = [];
/**
* the Command's Options
*
* @var array<string, string>
*/
protected $options = [
'--sort-by-handler' => 'Sort by handler.',
'--host' => 'Specify hostname in request URI.',
];
/**
* Displays the help for the spark cli script itself.
*/
public function run(array $params)
{
$sortByHandler = array_key_exists('sort-by-handler', $params);
if (! $sortByHandler && array_key_exists('h', $params)) {
// @todo to remove support in v4.8.0
// Support -h as a shortcut but print a warning that it is not the intended use of -h.
CLI::write('Warning: -h will be used as shortcut for --help in v4.8.0. Please use --sort-by-handler to sort by handler.', 'yellow');
CLI::newLine();
$sortByHandler = true;
}
$host = $params['host'] ?? null;
// Set HTTP_HOST
if ($host !== null) {
service('superglobals')->setServer('HTTP_HOST', $host);
}
$collection = service('routes')->loadRoutes();
// Reset HTTP_HOST
if ($host !== null) {
service('superglobals')->unsetServer('HTTP_HOST');
}
$methods = Router::HTTP_METHODS;
$tbody = [];
$uriGenerator = new SampleURIGenerator();
$filterCollector = new FilterCollector();
$definedRouteCollector = new DefinedRouteCollector($collection);
foreach ($definedRouteCollector->collect() as $route) {
$sampleUri = $uriGenerator->get($route['route']);
$filters = $filterCollector->get($route['method'], $sampleUri);
$routeName = ($route['route'] === $route['name']) ? '»' : $route['name'];
$tbody[] = [
strtoupper($route['method']),
$route['route'],
$routeName,
$route['handler'],
implode(' ', array_map(class_basename(...), $filters['before'])),
implode(' ', array_map(class_basename(...), $filters['after'])),
];
}
if ($collection->shouldAutoRoute()) {
$autoRoutesImproved = config(Feature::class)->autoRoutesImproved;
if ($autoRoutesImproved) {
$autoRouteCollector = new AutoRouteCollectorImproved(
$collection->getDefaultNamespace(),
$collection->getDefaultController(),
$collection->getDefaultMethod(),
$methods,
$collection->getRegisteredControllers('*'),
);
$autoRoutes = $autoRouteCollector->get();
// Check for Module Routes.
$routingConfig = config(Routing::class);
if ($routingConfig instanceof Routing) {
foreach ($routingConfig->moduleRoutes as $uri => $namespace) {
$autoRouteCollector = new AutoRouteCollectorImproved(
$namespace,
$collection->getDefaultController(),
$collection->getDefaultMethod(),
$methods,
$collection->getRegisteredControllers('*'),
$uri,
);
$autoRoutes = [...$autoRoutes, ...$autoRouteCollector->get()];
}
}
} else {
$autoRouteCollector = new AutoRouteCollector(
$collection->getDefaultNamespace(),
$collection->getDefaultController(),
$collection->getDefaultMethod(),
);
$autoRoutes = $autoRouteCollector->get();
foreach ($autoRoutes as &$routes) {
// There is no `AUTO` method, but it is intentional not to get route filters.
$filters = $filterCollector->get('AUTO', $uriGenerator->get($routes[1]));
$routes[] = implode(' ', array_map(class_basename(...), $filters['before']));
$routes[] = implode(' ', array_map(class_basename(...), $filters['after']));
}
}
$tbody = [...$tbody, ...$autoRoutes];
}
$thead = [
'Method',
'Route',
'Name',
$sortByHandler ? 'Handler ↓' : 'Handler',
'Before Filters',
'After Filters',
];
// Sort by Handler.
if ($sortByHandler) {
usort($tbody, static fn ($handler1, $handler2): int => strcmp($handler1[3], $handler2[3]));
}
if ($host !== null) {
CLI::write('Host: ' . $host);
}
CLI::table($tbody, $thead);
$this->showRequiredFilters();
}
private function showRequiredFilters(): void
{
$filterCollector = new FilterCollector();
$required = $filterCollector->getRequiredFilters();
$filters = [];
foreach ($required['before'] as $filter) {
$filters[] = CLI::color($filter, 'yellow');
}
CLI::write('Required Before Filters: ' . implode(', ', $filters));
$filters = [];
foreach ($required['after'] as $filter) {
$filters[] = CLI::color($filter, 'yellow');
}
CLI::write(' Required After Filters: ' . implode(', ', $filters));
}
}