33namespace Mtrajano \LaravelSwagger ;
44
55use Illuminate \Foundation \Http \FormRequest ;
6- use Illuminate \Routing \Route ;
76use Illuminate \Support \Str ;
8- use Illuminate \Support \Arr ;
97use phpDocumentor \Reflection \DocBlockFactory ;
108use ReflectionMethod ;
119
@@ -18,11 +16,8 @@ class Generator
1816 protected $ config ;
1917 protected $ routeFilter ;
2018 protected $ docs ;
21- protected $ uri ;
22- protected $ originalUri ;
19+ protected $ route ;
2320 protected $ method ;
24- protected $ action ;
25- protected $ middleware ;
2621 protected $ docParser ;
2722 protected $ hasSecurityDefinitions ;
2823
@@ -44,26 +39,18 @@ public function generate()
4439 }
4540
4641 foreach ($ this ->getAppRoutes () as $ route ) {
47- $ this ->originalUri = $ uri = $ this ->getRouteUri ($ route );
48- $ this ->uri = strip_optional_char ($ uri );
42+ $ this ->route = $ route ;
4943
5044 if ($ this ->routeFilter && $ this ->isFilteredRoute ()) {
5145 continue ;
5246 }
5347
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 ()] = [];
6350 }
6451
65- foreach ($ methods as $ method ) {
66- $ this ->method = strtolower ( $ method) ;
52+ foreach ($ route -> methods () as $ method ) {
53+ $ this ->method = $ method ;
6754
6855 if (in_array ($ this ->method , $ this ->config ['ignoredMethods ' ])) {
6956 continue ;
@@ -108,18 +95,9 @@ protected function getBaseInfo()
10895
10996 protected function getAppRoutes ()
11097 {
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 ());
123101 }
124102
125103 protected function generateSecurityDefinitions ()
@@ -151,12 +129,12 @@ protected function generateSecurityDefinitions()
151129
152130 protected function generatePath ()
153131 {
154- $ actionInstance = is_string ($ this ->action ) ? $ this ->getActionClassInstance ($ this ->action ) : null ;
132+ $ actionInstance = is_string ($ this ->route -> action ()) ? $ this ->getActionClassInstance ($ this ->route -> action () ) : null ;
155133 $ docBlock = $ actionInstance ? ($ actionInstance ->getDocComment () ?: '' ) : '' ;
156134
157135 [$ isDeprecated , $ summary , $ description ] = $ this ->parseActionDocBlock ($ docBlock );
158136
159- $ this ->docs ['paths ' ][$ this ->uri ][$ this ->method ] = [
137+ $ this ->docs ['paths ' ][$ this ->route -> uri () ][$ this ->method ] = [
160138 'summary ' => $ summary ,
161139 'description ' => $ description ,
162140 'deprecated ' => $ isDeprecated ,
@@ -178,7 +156,7 @@ protected function addActionParameters()
178156 {
179157 $ rules = $ this ->getFormRules () ?: [];
180158
181- $ parameters = (new Parameters \PathParameterGenerator ($ this ->originalUri ))->getParameters ();
159+ $ parameters = (new Parameters \PathParameterGenerator ($ this ->route -> originalUri () ))->getParameters ();
182160
183161 if (!empty ($ rules )) {
184162 $ parameterGenerator = $ this ->getParameterGenerator ($ rules );
@@ -187,28 +165,28 @@ protected function addActionParameters()
187165 }
188166
189167 if (!empty ($ parameters )) {
190- $ this ->docs ['paths ' ][$ this ->uri ][$ this ->method ]['parameters ' ] = $ parameters ;
168+ $ this ->docs ['paths ' ][$ this ->route -> uri () ][$ this ->method ]['parameters ' ] = $ parameters ;
191169 }
192170 }
193171
194172 protected function addActionScopes ()
195173 {
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 ()
200178 ];
201179 }
202180 }
203181 }
204182
205183 protected function getFormRules ()
206184 {
207- if (!is_string ($ this ->action )) {
185+ if (!is_string ($ this ->route -> action () )) {
208186 return false ;
209187 }
210188
211- $ parameters = $ this ->getActionClassInstance ($ this ->action )->getParameters ();
189+ $ parameters = $ this ->getActionClassInstance ($ this ->route -> action () )->getParameters ();
212190
213191 foreach ($ parameters as $ parameter ) {
214192 $ class = (string ) $ parameter ->getClass ();
@@ -260,7 +238,7 @@ private function parseActionDocBlock(string $docBlock)
260238
261239 private function isFilteredRoute ()
262240 {
263- return !preg_match ('/^ ' . preg_quote ($ this ->routeFilter , '/ ' ) . '/ ' , $ this ->uri );
241+ return !preg_match ('/^ ' . preg_quote ($ this ->routeFilter , '/ ' ) . '/ ' , $ this ->route -> uri () );
264242 }
265243
266244 /**
@@ -269,7 +247,7 @@ private function isFilteredRoute()
269247 private function hasOauthRoutes ()
270248 {
271249 foreach ($ this ->getAppRoutes () as $ route ) {
272- $ uri = $ this -> getRouteUri ( $ route );
250+ $ uri = $ route -> uri ( );
273251
274252 if ($ uri === self ::OAUTH_TOKEN_PATH || $ uri === self ::OAUTH_AUTHORIZE_PATH ) {
275253 return true ;
@@ -301,17 +279,4 @@ private function validateAuthFlow(string $flow)
301279 throw new LaravelSwaggerException ('Invalid OAuth flow passed ' );
302280 }
303281 }
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- }
317282}
0 commit comments