-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathAutoRouteCollector.php
More file actions
154 lines (127 loc) · 4.83 KB
/
AutoRouteCollector.php
File metadata and controls
154 lines (127 loc) · 4.83 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
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved;
use CodeIgniter\Commands\Utilities\Routes\ControllerFinder;
use CodeIgniter\Commands\Utilities\Routes\FilterCollector;
/**
* Collects data for Auto Routing Improved.
*
* @see \CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\AutoRouteCollectorTest
*/
final class AutoRouteCollector
{
/**
* @param string $namespace namespace to search
* @param list<class-string> $protectedControllers List of controllers in Defined
* Routes that should not be accessed via Auto-Routing.
* @param list<string> $httpMethods
* @param string $prefix URI prefix for Module Routing
*/
public function __construct(
private readonly string $namespace,
private readonly string $defaultController,
private readonly string $defaultMethod,
private readonly array $httpMethods,
private readonly array $protectedControllers,
private readonly string $prefix = '',
) {
}
/**
* @return list<list<string>>
*/
public function get(): array
{
$finder = new ControllerFinder($this->namespace);
$reader = new ControllerMethodReader($this->namespace, $this->httpMethods);
$tbody = [];
foreach ($finder->find() as $class) {
// Exclude controllers in Defined Routes.
if (in_array('\\' . $class, $this->protectedControllers, true)) {
continue;
}
$routes = $reader->read(
$class,
$this->defaultController,
$this->defaultMethod,
);
if ($routes === []) {
continue;
}
$routes = $this->addFilters($routes);
foreach ($routes as $item) {
$route = $item['route'] . $item['route_params'];
// For module routing
if ($this->prefix !== '' && $route === '/') {
$route = $this->prefix;
} elseif ($this->prefix !== '') {
$route = $this->prefix . '/' . $route;
}
$tbody[] = [
strtoupper($item['method']) . '(auto)',
$route,
'',
$item['handler'],
$item['before'],
$item['after'],
];
}
}
return $tbody;
}
/**
* Adding Filters
*
* @param array<int,array<string,string|array<string>>> $routes List of route definitions
*
* @return array<int,array<string,string|array<string>>> $Updated route definitions with filters
*
*/
private function addFilters(array $routes): array
{
$filterCollector = new FilterCollector(true);
foreach ($routes as &$route) {
$routePath = $route['route'];
// For module routing
if ($this->prefix !== '' && $route === '/') {
$routePath = $this->prefix;
} elseif ($this->prefix !== '') {
$routePath = $this->prefix . '/' . $routePath;
}
// Search filters for the URI with all params
$sampleUri = $this->generateSampleUri($route);
$filtersLongest = $filterCollector->get($route['method'], $routePath . $sampleUri);
// Search filters for the URI without optional params
$sampleUri = $this->generateSampleUri($route, false);
$filtersShortest = $filterCollector->get($route['method'], $routePath . $sampleUri);
// Get common array elements
$filters = [
'before' => array_intersect($filtersLongest['before'], $filtersShortest['before']),
'after' => array_intersect($filtersLongest['after'], $filtersShortest['after']),
];
$route['before'] = implode(' ', array_map(class_basename(...), $filters['before']));
$route['after'] = implode(' ', array_map(class_basename(...), $filters['after']));
}
return $routes;
}
private function generateSampleUri(array $route, bool $longest = true): string
{
$sampleUri = '';
if (isset($route['params'])) {
$i = 1;
foreach ($route['params'] as $required) {
if ($longest && ! $required) {
$sampleUri .= '/' . $i++;
}
}
}
return $sampleUri;
}
}