-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRouteScanner.php
More file actions
300 lines (261 loc) · 9.39 KB
/
RouteScanner.php
File metadata and controls
300 lines (261 loc) · 9.39 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
namespace ProAI\Annotations\Metadata;
use ReflectionClass;
use Doctrine\Common\Annotations\AnnotationReader;
class RouteScanner
{
/**
* The annotation reader instance.
*
* @var \Doctrine\Common\Annotations\AnnotationReader
*/
protected $reader;
/**
* Create a new metadata builder instance.
*
* @param \Doctrine\Common\Annotations\AnnotationReader $reader
* @return void
*/
public function __construct(AnnotationReader $reader)
{
$this->reader = $reader;
}
/**
* Build metadata from all entity classes.
*
* @param array $classes
* @return array
*/
public function scan($classes)
{
$metadata = [];
foreach ($classes as $class) {
$controllerMetadata = $this->parseClass($class);
if ($controllerMetadata) {
$metadata[$class] = $controllerMetadata;
}
}
return $metadata;
}
/**
* Parse a class.
*
* @param string $class
* @return array|null
*/
public function parseClass($class)
{
$reflectionClass = new ReflectionClass($class);
// check if class is controller
if ($annotation = $this->reader->getClassAnnotation($reflectionClass, '\ProAI\Annotations\Annotations\Controller')) {
return $this->parseController($class);
} else {
return null;
}
}
/**
* Parse a controller class.
*
* @param string $class
* @return array
*/
public function parseController($class)
{
$reflectionClass = new ReflectionClass($class);
$classAnnotations = $this->reader->getClassAnnotations($reflectionClass);
$controllerMetadata = [];
// find entity parameters and plugins
foreach ($classAnnotations as $annotation) {
// controller attributes
if ($annotation instanceof \ProAI\Annotations\Annotations\Controller) {
$prefix = $annotation->prefix;
$middleware = $annotation->middleware;
}
if ($annotation instanceof \ProAI\Annotations\Annotations\Middleware) {
$middleware = $annotation->value;
}
// resource controller
if ($annotation instanceof \ProAI\Annotations\Annotations\Resource) {
$resourceMethods = ['index', 'create', 'store', 'show', 'edit', 'update', 'destroy'];
if (! empty($annotation->only)) {
$resourceMethods = array_intersect($resourceMethods, $annotation->only);
}
elseif (! empty($annotation->except)) {
$resourceMethods = array_diff($resourceMethods, $annotation->except);
}
$resource = [
'name' => $annotation->value,
'methods' => $resourceMethods
];
}
}
// find routes
foreach ($reflectionClass->getMethods() as $reflectionMethod) {
$name = $reflectionMethod->getName();
$methodAnnotations = $this->reader->getMethodAnnotations($reflectionMethod);
$routeMetadata = [];
// controller method is resource route
if (! empty($resource) && in_array($name, $resource['methods'])) {
$routeMetadata = [
'uri' => $resource['name'].$this->getResourcePath($name),
'controller' => $class,
'controllerMethod' => $name,
'httpMethod' => $this->getResourceHttpMethod($name),
'as' => $resource['name'].'.'.$name,
'middleware' => ''
];
}
// controller method is route
if ($routes = $this->hasHttpMethodAnnotation($name, $methodAnnotations)) {
$routeMetadata = [];
foreach($routes as $route){
$routeMetadata[] = [
'uri' => $route['uri'],
'controller' => $class,
'controllerMethod' => $name,
'httpMethod' => $route['httpMethod'],
'as' => $route['as'],
'middleware' => $route['middleware']
];
}
}
// add more route options to route metadata
if (! empty($routeMetadata)) {
if(!isset($routeMetadata[0])){
$temp = [];
$temp[] = $routeMetadata;
$routeMetadatas = $temp;
} else {
$routeMetadatas = $routeMetadata;
}
$idx = 0;
foreach($routeMetadatas as $routeMetadata){
$idx++;
// add other method annotations
foreach ($methodAnnotations as $annotation) {
if ($annotation instanceof \ProAI\Annotations\Annotations\Middleware) {
if (!empty($middleware) && isset($routeMetadata['middleware'])) {
$routeMetadata['middleware'] = [$middleware, $annotation->value];
continue;
}
$routeMetadata['middleware'] = $annotation->value;
}
}
// add global prefix and middleware
if (! empty($prefix)) {
$routeMetadata['uri'] = $prefix.'/'.$routeMetadata['uri'];
}
if (! empty($middleware) && empty($routeMetadata['middleware'])) {
$routeMetadata['middleware'] = $middleware;
}
$controllerMetadata[$name.$idx] = $routeMetadata;
}
}
}
return $controllerMetadata;
}
/**
* Get resource http method.
*
* @param string $method
* @return string
*/
protected function getResourceHttpMethod($method)
{
$resourceHttpMethods = [
'index' => 'GET',
'create' => 'GET',
'store' => 'POST',
'show' => 'GET',
'edit' => 'GET',
'update' => 'PUT',
'destroy' => 'DELETE'
];
return (isset($resourceHttpMethods[$method])) ? $resourceHttpMethods[$method] : null;
}
/**
* Get resource path.
*
* @param string $method
* @return string
*/
protected function getResourcePath($method)
{
$resourcePaths = [
'index' => '',
'create' => 'create',
'store' => '',
'show' => '/{id}',
'edit' => '/{id}/edit',
'update' => '/{id}',
'destroy' => '/{id}'
];
return (isset($resourcePaths[$method])) ? $resourcePaths[$method] : null;
}
/**
* Check for http method.
*
* @param string $name
* @param array $methodAnnotations
* @return string
*/
protected function hasHttpMethodAnnotation($name, $methodAnnotations)
{
$parseAnnotation = function ($httpMethod,$annotation){
// options
$as = (! empty($annotation->as)) ? $annotation->as : '';
$middleware = (! empty($annotation->middleware)) ? $annotation->middleware : '';
$uri = (empty($annotation->value)) ? str_replace("_", "-", snake_case($name)) : $annotation->value;
return [
'uri' => $uri,
'httpMethod' => $httpMethod,
'as' => $as,
'middleware' => $middleware
];
};
$return = [];
foreach ($methodAnnotations as $annotation) {
// check for http method annotation
if ($annotation instanceof \ProAI\Annotations\Annotations\Get) {
$httpMethod = 'GET';
$return[] = $parseAnnotation($httpMethod,$annotation);
// break;
}
if ($annotation instanceof \ProAI\Annotations\Annotations\Post) {
$httpMethod = 'POST';
$return[] = $parseAnnotation($httpMethod,$annotation);
//break;
}
if ($annotation instanceof \ProAI\Annotations\Annotations\Options) {
$httpMethod = 'OPTIONS';
$return[] = $parseAnnotation($httpMethod,$annotation);
// break;
}
if ($annotation instanceof \ProAI\Annotations\Annotations\Put) {
$httpMethod = 'PUT';
$return[] = $parseAnnotation($httpMethod,$annotation);
//break;
}
if ($annotation instanceof \ProAI\Annotations\Annotations\Patch) {
$httpMethod = 'PATCH';
$return[] = $parseAnnotation($httpMethod,$annotation);
// break;
}
if ($annotation instanceof \ProAI\Annotations\Annotations\Delete) {
$httpMethod = 'DELETE';
$return[] = $parseAnnotation($httpMethod,$annotation);
//break;
}
if ($annotation instanceof \ProAI\Annotations\Annotations\Any) {
$httpMethod = 'ANY';
$return[] = $parseAnnotation($httpMethod,$annotation);
//break;
}
}
if(count($return) > 0){
return $return;
} else {
return false;
}
}
}