18
18
*/
19
19
class AutoRoute
20
20
{
21
- /**
22
- * @var Container
23
- */
21
+ /** @var Container */
24
22
protected $ app ;
25
23
26
- /**
27
- * @var Router
28
- */
24
+ /** @var Router */
29
25
protected $ router ;
30
26
31
- /**
32
- * @var string
33
- */
27
+ /** @var string */
34
28
protected $ namespace ;
35
29
36
- /**
37
- * @var string[] Laravel Routing Available Methods.
38
- */
30
+ /** @var string[] Laravel Routing Available Methods. */
39
31
protected $ availableMethods = ['GET ' , 'POST ' , 'PUT ' , 'PATCH ' , 'DELETE ' , 'OPTIONS ' ];
40
32
41
- /**
42
- * @var string[] Custom methods for the package
43
- */
33
+ /** @var string[] Custom methods for the package */
44
34
protected $ customMethods = ['XGET ' , 'XPOST ' , 'XPUT ' , 'XPATCH ' , 'XDELETE ' , 'XOPTIONS ' , 'XANY ' ];
45
35
46
- /**
47
- * @var string Main Method
48
- */
36
+ /** @var string Main Method */
49
37
protected $ mainMethod ;
50
38
51
- /**
52
- * @var string
53
- */
39
+ /** @var string */
54
40
protected $ defaultHttpMethods ;
55
41
56
- /**
57
- * @var string Ajax Middleware class for the Requests
58
- */
42
+ /** @var string Ajax Middleware class for the Requests */
59
43
protected $ ajaxMiddleware ;
60
44
61
- /**
62
- * @var string[]
63
- */
45
+ /** @var string[] */
64
46
protected $ defaultPatterns = [
65
47
':any ' => '([^/]+) ' ,
66
48
':int ' => '(\d+) ' ,
@@ -70,9 +52,6 @@ class AutoRoute
70
52
71
53
/**
72
54
* AutoRoute constructor.
73
- *
74
- * @param Container $app
75
- *
76
55
* @throws
77
56
*/
78
57
public function __construct (Container $ app )
@@ -83,8 +62,6 @@ public function __construct(Container $app)
83
62
84
63
/**
85
64
* Initialize for the AutoRoute
86
- *
87
- * @param array $config
88
65
*/
89
66
public function setConfigurations (array $ config ): void
90
67
{
@@ -100,11 +77,6 @@ public function setConfigurations(array $config): void
100
77
}
101
78
102
79
/**
103
- * @param string $prefix
104
- * @param string $controller
105
- * @param array $options
106
- *
107
- * @return void
108
80
* @throws
109
81
*/
110
82
public function auto (string $ prefix , string $ controller , array $ options = []): void
@@ -162,11 +134,6 @@ function () use ($controller, $only, $except, $patterns) {
162
134
);
163
135
}
164
136
165
- /**
166
- * @param string $methodName
167
- *
168
- * @return array
169
- */
170
137
private function getHttpMethodAndName (string $ methodName ): array
171
138
{
172
139
$ httpMethods = $ this ->defaultHttpMethods ;
@@ -191,12 +158,6 @@ private function getHttpMethodAndName(string $methodName): array
191
158
return [$ httpMethods , $ methodName , $ middleware ];
192
159
}
193
160
194
- /**
195
- * @param ReflectionMethod $method
196
- * @param array $patterns
197
- *
198
- * @return array
199
- */
200
161
private function getRouteValues (ReflectionMethod $ method , array $ patterns = []): array
201
162
{
202
163
$ routePatterns = $ endpoints = [];
@@ -205,11 +166,8 @@ private function getRouteValues(ReflectionMethod $method, array $patterns = []):
205
166
$ paramName = $ param ->getName ();
206
167
$ typeHint = $ param ->hasType () ? $ param ->getType ()->getName () : null ;
207
168
208
- if ($ typeHint !== null && class_exists ($ typeHint )) {
209
- if (!in_array ($ typeHint , ['int ' , 'float ' , 'string ' , 'bool ' ]) && !in_array ($ typeHint , $ patterns )
210
- && !is_subclass_of ($ typeHint , Model::class)) {
211
- continue ;
212
- }
169
+ if (!$ this ->isValidRouteParam ($ typeHint )) {
170
+ continue ;
213
171
}
214
172
215
173
$ routePatterns [$ paramName ] = $ patterns [$ paramName ] ??
@@ -220,11 +178,6 @@ private function getRouteValues(ReflectionMethod $method, array $patterns = []):
220
178
return [$ endpoints , $ routePatterns ];
221
179
}
222
180
223
- /**
224
- * @param string $controller
225
- *
226
- * @return array
227
- */
228
181
private function resolveControllerName (string $ controller ): array
229
182
{
230
183
$ controller = str_replace (['. ' , $ this ->namespace ], ['\\' , '' ], $ controller );
@@ -233,4 +186,21 @@ private function resolveControllerName(string $controller): array
233
186
$ controller ,
234
187
];
235
188
}
189
+
190
+ private function isValidRouteParam (?string $ type ): bool
191
+ {
192
+ if (is_null ($ type ) || in_array ($ type , ['int ' , 'float ' , 'string ' , 'bool ' , 'mixed ' ])) {
193
+ return true ;
194
+ }
195
+
196
+ if (class_exists ($ type ) && is_subclass_of ($ type , Model::class)) {
197
+ return true ;
198
+ }
199
+
200
+ if (function_exists ('enum_exists ' ) && enum_exists ($ type )) {
201
+ return true ;
202
+ }
203
+
204
+ return false ;
205
+ }
236
206
}
0 commit comments