4
4
5
5
namespace DoclerLabs \ApiClientGenerator \Generator ;
6
6
7
+ use DoclerLabs \ApiClientGenerator \Ast \Builder \CodeBuilder ;
7
8
use DoclerLabs \ApiClientGenerator \Ast \ParameterNode ;
8
9
use DoclerLabs \ApiClientGenerator \Entity \Field ;
9
10
use DoclerLabs \ApiClientGenerator \Entity \Operation ;
@@ -21,29 +22,45 @@ class RequestGenerator extends MutatorAccessorClassGeneratorAbstract
21
22
public const NAMESPACE_SUBPATH = '\\Request ' ;
22
23
public const SUBDIRECTORY = 'Request/ ' ;
23
24
25
+ /** @var SecurityStrategyInterface[] */
26
+ private array $ securityStrategies ;
27
+
28
+ public function __construct (
29
+ string $ baseNamespace ,
30
+ CodeBuilder $ builder ,
31
+ SecurityStrategyInterface ...$ securityStrategies
32
+ ) {
33
+ parent ::__construct ($ baseNamespace , $ builder );
34
+
35
+ $ this ->securityStrategies = $ securityStrategies ;
36
+ }
37
+
24
38
public function generate (Specification $ specification , PhpFileCollection $ fileRegistry ): void
25
39
{
26
40
foreach ($ specification ->getOperations () as $ operation ) {
27
- $ this ->generateRequest ($ fileRegistry , $ operation );
41
+ $ this ->generateRequest ($ fileRegistry , $ operation, $ specification );
28
42
}
29
43
}
30
44
31
- protected function generateRequest (PhpFileCollection $ fileRegistry , Operation $ operation ): void
32
- {
45
+ protected function generateRequest (
46
+ PhpFileCollection $ fileRegistry ,
47
+ Operation $ operation ,
48
+ Specification $ specification
49
+ ): void {
33
50
$ className = RequestNaming::getClassName ($ operation );
34
51
$ request = $ operation ->getRequest ();
35
52
36
53
$ classBuilder = $ this ->builder
37
54
->class ($ className )
38
55
->implement ('RequestInterface ' )
39
56
->addStmts ($ this ->generateEnums ($ request ))
40
- ->addStmts ($ this ->generateProperties ($ request ))
41
- ->addStmt ($ this ->generateConstructor ($ request ))
57
+ ->addStmts ($ this ->generateProperties ($ request, $ operation , $ specification ))
58
+ ->addStmt ($ this ->generateConstructor ($ request, $ operation , $ specification ))
42
59
->addStmt ($ this ->generateGetContentType ())
43
60
->addStmts ($ this ->generateSetters ($ request ))
44
61
->addStmt ($ this ->generateGetMethod ($ request ))
45
62
->addStmt ($ this ->generateGetRoute ($ request ))
46
- ->addStmts ($ this ->generateGetParametersMethods ($ request ));
63
+ ->addStmts ($ this ->generateGetParametersMethods ($ request, $ operation , $ specification ));
47
64
48
65
$ this ->registerFile ($ fileRegistry , $ classBuilder , self ::SUBDIRECTORY , self ::NAMESPACE_SUBPATH );
49
66
}
@@ -62,7 +79,7 @@ protected function generateEnums(Request $request): array
62
79
return $ statements ;
63
80
}
64
81
65
- protected function generateProperties (Request $ request ): array
82
+ protected function generateProperties (Request $ request, Operation $ operation , Specification $ specification ): array
66
83
{
67
84
$ statements = [];
68
85
foreach ($ request ->getFields () as $ fields ) {
@@ -87,11 +104,19 @@ protected function generateProperties(Request $request): array
87
104
}
88
105
$ statements [] = $ this ->builder ->localProperty ('contentType ' , 'string ' , 'string ' , false , $ default );
89
106
107
+ foreach ($ this ->securityStrategies as $ securityStrategy ) {
108
+ /** @var SecurityStrategyInterface $securityStrategy */
109
+ array_push ($ statements , ...$ securityStrategy ->getProperties ($ operation , $ specification ));
110
+ }
111
+
90
112
return $ statements ;
91
113
}
92
114
93
- protected function generateConstructor (Request $ request ): ?ClassMethod
94
- {
115
+ protected function generateConstructor (
116
+ Request $ request ,
117
+ Operation $ operation ,
118
+ Specification $ specification
119
+ ): ?ClassMethod {
95
120
$ params = [];
96
121
$ paramInits = [];
97
122
foreach ($ request ->getFields () as $ fields ) {
@@ -117,6 +142,12 @@ protected function generateConstructor(Request $request): ?ClassMethod
117
142
}
118
143
}
119
144
145
+ foreach ($ this ->securityStrategies as $ securityStrategy ) {
146
+ /** @var SecurityStrategyInterface $securityStrategy */
147
+ array_push ($ params , ...$ securityStrategy ->getConstructorParams ($ operation , $ specification ));
148
+ array_push ($ paramInits , ...$ securityStrategy ->getConstructorParamInits ($ operation , $ specification ));
149
+ }
150
+
120
151
if (count ($ request ->getBodyContentTypes ()) > 1 ) {
121
152
$ contentTypeVariableName = 'contentType ' ;
122
153
@@ -224,8 +255,11 @@ private function generateGetRoute(Request $request): ClassMethod
224
255
->getNode ();
225
256
}
226
257
227
- private function generateGetParametersMethods (Request $ request ): array
228
- {
258
+ private function generateGetParametersMethods (
259
+ Request $ request ,
260
+ Operation $ operation ,
261
+ Specification $ specification
262
+ ): array {
229
263
$ methods = [];
230
264
$ fields = $ request ->getFields ();
231
265
$ methods [] = $ this ->generateGetParametersMethod (
@@ -240,7 +274,7 @@ private function generateGetParametersMethods(Request $request): array
240
274
'getCookies ' ,
241
275
$ fields ->getCookieFields ()
242
276
);
243
- $ methods [] = $ this ->generateGetHeadersMethod ($ request , $ fields ->getHeaderFields ());
277
+ $ methods [] = $ this ->generateGetHeadersMethod ($ request , $ fields ->getHeaderFields (), $ operation , $ specification );
244
278
$ methods [] = $ this ->generateGetBody ($ fields ->getBody ());
245
279
246
280
return $ methods ;
@@ -305,9 +339,13 @@ private function generateGetBody(?Field $body): ClassMethod
305
339
->getNode ();
306
340
}
307
341
308
- private function generateGetHeadersMethod (Request $ request , array $ fields ): ClassMethod
309
- {
310
- $ headers = [];
342
+ private function generateGetHeadersMethod (
343
+ Request $ request ,
344
+ array $ fields ,
345
+ Operation $ operation ,
346
+ Specification $ specification
347
+ ): ClassMethod {
348
+ $ headers = $ this ->getSecurityHeaders ($ operation , $ specification );
311
349
if (!empty ($ request ->getBodyContentTypes ())) {
312
350
$ headers ['Content-Type ' ] = $ this ->builder ->localPropertyFetch ('contentType ' );
313
351
}
@@ -334,6 +372,18 @@ private function generateGetHeadersMethod(Request $request, array $fields): Clas
334
372
->getNode ();
335
373
}
336
374
375
+ private function getSecurityHeaders (Operation $ operation , Specification $ specification ): array
376
+ {
377
+ $ headers = [];
378
+
379
+ foreach ($ this ->securityStrategies as $ securityStrategy ) {
380
+ /** @var SecurityStrategyInterface $securityStrategy */
381
+ $ headers += $ securityStrategy ->getSecurityHeaders ($ operation , $ specification );
382
+ }
383
+
384
+ return $ headers ;
385
+ }
386
+
337
387
private function generateParametersFromFields (array $ fields ): FuncCall
338
388
{
339
389
$ filterCallbackBody = $ this ->builder ->return (
0 commit comments