3
3
namespace Mtrajano \LaravelSwagger ;
4
4
5
5
use Illuminate \Foundation \Http \FormRequest ;
6
- use Illuminate \Routing \Route ;
7
6
use Illuminate \Support \Str ;
8
- use Illuminate \Support \Arr ;
9
7
use phpDocumentor \Reflection \DocBlockFactory ;
10
8
use ReflectionMethod ;
11
9
@@ -18,11 +16,8 @@ class Generator
18
16
protected $ config ;
19
17
protected $ routeFilter ;
20
18
protected $ docs ;
21
- protected $ uri ;
22
- protected $ originalUri ;
19
+ protected $ route ;
23
20
protected $ method ;
24
- protected $ action ;
25
- protected $ middleware ;
26
21
protected $ docParser ;
27
22
protected $ hasSecurityDefinitions ;
28
23
@@ -44,26 +39,18 @@ public function generate()
44
39
}
45
40
46
41
foreach ($ this ->getAppRoutes () as $ route ) {
47
- $ this ->originalUri = $ uri = $ this ->getRouteUri ($ route );
48
- $ this ->uri = strip_optional_char ($ uri );
42
+ $ this ->route = $ route ;
49
43
50
44
if ($ this ->routeFilter && $ this ->isFilteredRoute ()) {
51
45
continue ;
52
46
}
53
47
54
- $ middleware = isset ($ route ->getAction ()['middleware ' ]) ? $ route ->getAction ()['middleware ' ] : [];
55
-
56
- $ this ->action = $ route ->getAction ()['uses ' ];
57
- $ this ->middleware = $ this ->formatMiddleware ($ middleware );
58
-
59
- $ methods = $ route ->methods ();
60
-
61
- if (!isset ($ this ->docs ['paths ' ][$ this ->uri ])) {
62
- $ this ->docs ['paths ' ][$ this ->uri ] = [];
48
+ if (!isset ($ this ->docs ['paths ' ][$ this ->route ->uri ()])) {
49
+ $ this ->docs ['paths ' ][$ this ->route ->uri ()] = [];
63
50
}
64
51
65
- foreach ($ methods as $ method ) {
66
- $ this ->method = strtolower ( $ method) ;
52
+ foreach ($ route -> methods () as $ method ) {
53
+ $ this ->method = $ method ;
67
54
68
55
if (in_array ($ this ->method , $ this ->config ['ignoredMethods ' ])) {
69
56
continue ;
@@ -108,18 +95,9 @@ protected function getBaseInfo()
108
95
109
96
protected function getAppRoutes ()
110
97
{
111
- return app ('router ' )->getRoutes ();
112
- }
113
-
114
- protected function getRouteUri (Route $ route )
115
- {
116
- $ uri = $ route ->uri ();
117
-
118
- if (!Str::startsWith ($ uri , '/ ' )) {
119
- $ uri = '/ ' . $ uri ;
120
- }
121
-
122
- return $ uri ;
98
+ return array_map (function ($ route ) {
99
+ return new DataObjects \Route ($ route );
100
+ }, app ('router ' )->getRoutes ()->getRoutes ());
123
101
}
124
102
125
103
protected function generateSecurityDefinitions ()
@@ -151,12 +129,12 @@ protected function generateSecurityDefinitions()
151
129
152
130
protected function generatePath ()
153
131
{
154
- $ actionInstance = is_string ($ this ->action ) ? $ this ->getActionClassInstance ($ this ->action ) : null ;
132
+ $ actionInstance = is_string ($ this ->route -> action ()) ? $ this ->getActionClassInstance ($ this ->route -> action () ) : null ;
155
133
$ docBlock = $ actionInstance ? ($ actionInstance ->getDocComment () ?: '' ) : '' ;
156
134
157
135
[$ isDeprecated , $ summary , $ description ] = $ this ->parseActionDocBlock ($ docBlock );
158
136
159
- $ this ->docs ['paths ' ][$ this ->uri ][$ this ->method ] = [
137
+ $ this ->docs ['paths ' ][$ this ->route -> uri () ][$ this ->method ] = [
160
138
'summary ' => $ summary ,
161
139
'description ' => $ description ,
162
140
'deprecated ' => $ isDeprecated ,
@@ -178,7 +156,7 @@ protected function addActionParameters()
178
156
{
179
157
$ rules = $ this ->getFormRules () ?: [];
180
158
181
- $ parameters = (new Parameters \PathParameterGenerator ($ this ->originalUri ))->getParameters ();
159
+ $ parameters = (new Parameters \PathParameterGenerator ($ this ->route -> originalUri () ))->getParameters ();
182
160
183
161
if (!empty ($ rules )) {
184
162
$ parameterGenerator = $ this ->getParameterGenerator ($ rules );
@@ -187,28 +165,28 @@ protected function addActionParameters()
187
165
}
188
166
189
167
if (!empty ($ parameters )) {
190
- $ this ->docs ['paths ' ][$ this ->uri ][$ this ->method ]['parameters ' ] = $ parameters ;
168
+ $ this ->docs ['paths ' ][$ this ->route -> uri () ][$ this ->method ]['parameters ' ] = $ parameters ;
191
169
}
192
170
}
193
171
194
172
protected function addActionScopes ()
195
173
{
196
- foreach ($ this ->middleware as $ middleware ) {
197
- if ($ middleware[ ' name ' ] === 'scope ' || $ middleware[ ' name ' ] === 'scopes ' ) {
198
- $ this ->docs ['paths ' ][$ this ->uri ][$ this ->method ]['security ' ] = [
199
- self ::SECURITY_DEFINITION_NAME => $ middleware[ ' parameters ' ]
174
+ foreach ($ this ->route -> middleware () as $ middleware ) {
175
+ if ($ middleware-> name () === 'scope ' || $ middleware-> name () === 'scopes ' ) {
176
+ $ this ->docs ['paths ' ][$ this ->route -> uri () ][$ this ->method ]['security ' ] = [
177
+ self ::SECURITY_DEFINITION_NAME => $ middleware-> parameters ()
200
178
];
201
179
}
202
180
}
203
181
}
204
182
205
183
protected function getFormRules ()
206
184
{
207
- if (!is_string ($ this ->action )) {
185
+ if (!is_string ($ this ->route -> action () )) {
208
186
return false ;
209
187
}
210
188
211
- $ parameters = $ this ->getActionClassInstance ($ this ->action )->getParameters ();
189
+ $ parameters = $ this ->getActionClassInstance ($ this ->route -> action () )->getParameters ();
212
190
213
191
foreach ($ parameters as $ parameter ) {
214
192
$ class = (string ) $ parameter ->getClass ();
@@ -260,7 +238,7 @@ private function parseActionDocBlock(string $docBlock)
260
238
261
239
private function isFilteredRoute ()
262
240
{
263
- return !preg_match ('/^ ' . preg_quote ($ this ->routeFilter , '/ ' ) . '/ ' , $ this ->uri );
241
+ return !preg_match ('/^ ' . preg_quote ($ this ->routeFilter , '/ ' ) . '/ ' , $ this ->route -> uri () );
264
242
}
265
243
266
244
/**
@@ -269,7 +247,7 @@ private function isFilteredRoute()
269
247
private function hasOauthRoutes ()
270
248
{
271
249
foreach ($ this ->getAppRoutes () as $ route ) {
272
- $ uri = $ this -> getRouteUri ( $ route );
250
+ $ uri = $ route -> uri ( );
273
251
274
252
if ($ uri === self ::OAUTH_TOKEN_PATH || $ uri === self ::OAUTH_AUTHORIZE_PATH ) {
275
253
return true ;
@@ -301,17 +279,4 @@ private function validateAuthFlow(string $flow)
301
279
throw new LaravelSwaggerException ('Invalid OAuth flow passed ' );
302
280
}
303
281
}
304
-
305
- private function formatMiddleware ($ middleware )
306
- {
307
- $ middleware = Arr::wrap ($ middleware );
308
-
309
- return array_map (function ($ mw ) {
310
- $ tokens = explode (': ' , $ mw , 2 );
311
- $ name = $ tokens [0 ];
312
- $ parameters = isset ($ tokens [1 ]) ? explode (', ' , $ tokens [1 ]) : [];
313
-
314
- return compact ('name ' , 'parameters ' );
315
- }, $ middleware );
316
- }
317
282
}
0 commit comments