@@ -13,6 +13,8 @@ class Generator
1313
1414 protected $ routeFilter ;
1515
16+ protected $ auth ;
17+
1618 protected $ docs ;
1719
1820 protected $ uri ;
@@ -23,10 +25,11 @@ class Generator
2325
2426 protected $ action ;
2527
26- public function __construct ($ config , $ routeFilter = null )
28+ public function __construct ($ config , $ routeFilter = null , $ auth = null )
2729 {
2830 $ this ->config = $ config ;
2931 $ this ->routeFilter = $ routeFilter ;
32+ $ this ->auth = $ auth ;
3033 }
3134
3235 public function generate ()
@@ -39,7 +42,7 @@ public function generate()
3942
4043 if ($ this ->routeFilter && !preg_match ('/^ ' . preg_quote ($ this ->routeFilter , '/ ' ) . '/ ' , $ this ->uri )) {
4144 continue ;
42- }
45+ }
4346
4447 $ this ->action = $ route ->getAction ('uses ' );
4548 $ methods = $ route ->methods ();
@@ -48,6 +51,8 @@ public function generate()
4851 $ this ->docs ['paths ' ][$ this ->uri ] = [];
4952 }
5053
54+ $ this ->addAuthParameters ($ route ->middleware ());
55+
5156 foreach ($ methods as $ method ) {
5257 $ this ->method = strtolower ($ method );
5358
@@ -85,6 +90,24 @@ protected function getBaseInfo()
8590 $ baseInfo ['produces ' ] = $ this ->config ['produces ' ];
8691 }
8792
93+ if (!empty ($ this ->auth )) {
94+ switch ($ this ->auth ) {
95+ case 'jwt ' :
96+ $ baseInfo ['securityDefinitions ' ] = [
97+ 'api_key ' => [
98+ 'type ' => 'apiKey ' ,
99+ 'name ' => 'Authorization ' ,
100+ 'in ' => 'header '
101+ ]
102+ ];
103+ break ;
104+
105+ default :
106+ $ baseInfo ['securityDefinitions ' ] = [];
107+ break ;
108+ }
109+ }
110+
88111 $ baseInfo ['paths ' ] = [];
89112
90113 return $ baseInfo ;
@@ -156,6 +179,24 @@ protected function getFormRules()
156179 }
157180 }
158181
182+ protected function addAuthParameters ($ middlewares )
183+ {
184+ if (!empty ($ this ->auth )) {
185+ switch ($ this ->auth ) {
186+ case 'jwt ' :
187+ $ hasAuth = array_filter ($ middlewares , function ($ var ) {
188+ return (strpos ($ var , 'jwt ' ) > -1 );
189+ });
190+ if ($ hasAuth ) {
191+ $ this ->docs ['paths ' ]['security ' ] = [
192+ 'api_key ' => []
193+ ];
194+ }
195+ break ;
196+ }
197+ }
198+ }
199+
159200 protected function getParameterGenerator ($ rules )
160201 {
161202 switch ($ this ->method ) {
0 commit comments