@@ -13,6 +13,8 @@ class Generator
13
13
14
14
protected $ routeFilter ;
15
15
16
+ protected $ auth ;
17
+
16
18
protected $ docs ;
17
19
18
20
protected $ uri ;
@@ -23,10 +25,11 @@ class Generator
23
25
24
26
protected $ action ;
25
27
26
- public function __construct ($ config , $ routeFilter = null )
28
+ public function __construct ($ config , $ routeFilter = null , $ auth = null )
27
29
{
28
30
$ this ->config = $ config ;
29
31
$ this ->routeFilter = $ routeFilter ;
32
+ $ this ->auth = $ auth ;
30
33
}
31
34
32
35
public function generate ()
@@ -39,7 +42,7 @@ public function generate()
39
42
40
43
if ($ this ->routeFilter && !preg_match ('/^ ' . preg_quote ($ this ->routeFilter , '/ ' ) . '/ ' , $ this ->uri )) {
41
44
continue ;
42
- }
45
+ }
43
46
44
47
$ this ->action = $ route ->getAction ('uses ' );
45
48
$ methods = $ route ->methods ();
@@ -48,6 +51,8 @@ public function generate()
48
51
$ this ->docs ['paths ' ][$ this ->uri ] = [];
49
52
}
50
53
54
+ $ this ->addAuthParameters ($ route ->middleware ());
55
+
51
56
foreach ($ methods as $ method ) {
52
57
$ this ->method = strtolower ($ method );
53
58
@@ -85,6 +90,24 @@ protected function getBaseInfo()
85
90
$ baseInfo ['produces ' ] = $ this ->config ['produces ' ];
86
91
}
87
92
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
+
88
111
$ baseInfo ['paths ' ] = [];
89
112
90
113
return $ baseInfo ;
@@ -156,6 +179,24 @@ protected function getFormRules()
156
179
}
157
180
}
158
181
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
+
159
200
protected function getParameterGenerator ($ rules )
160
201
{
161
202
switch ($ this ->method ) {
0 commit comments