2
2
3
3
namespace ApiClients \Tools \OpenApiClientGenerator \Generator ;
4
4
5
+ use ApiClients \Client \Github \Schema \WebhookLabelEdited \Changes \Name ;
5
6
use ApiClients \Contracts \HTTP \Headers \AuthenticationInterface ;
6
7
use ApiClients \Contracts \OpenAPI \WebHooksInterface ;
7
8
use ApiClients \Tools \OpenApiClientGenerator \File ;
27
28
use RingCentral \Psr7 \Request ;
28
29
use Rx \Observable ;
29
30
use Twig \Node \Expression \Binary \AndBinary ;
31
+ use Twig \Node \Expression \Binary \OrBinary ;
30
32
31
33
final class Client
32
34
{
@@ -209,18 +211,93 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
209
211
} else {
210
212
$ operationPath = explode ('/ ' , $ operation ->path );
211
213
}
214
+ $ operationPathCount = count ($ operationPath );
212
215
213
216
if (!array_key_exists ($ operation ->method , $ sortedOperations )) {
214
- $ sortedOperations [$ operation ->method ] = [
217
+ $ sortedOperations [$ operation ->method ] = [];
218
+ }
219
+ if (!array_key_exists ($ operationPathCount , $ sortedOperations [$ operation ->method ])) {
220
+ $ sortedOperations [$ operation ->method ][$ operationPathCount ] = [
215
221
'operations ' => [],
216
222
'paths ' => [],
217
223
];
218
224
}
219
225
220
- $ sortedOperations [$ operation ->method ] = self ::traverseOperationPaths ($ sortedOperations [$ operation ->method ], $ operationPath , $ operation , $ path );
226
+ $ sortedOperations [$ operation ->method ][$ operationPathCount ] = self ::traverseOperationPaths ($ sortedOperations [$ operation ->method ][$ operationPathCount ], $ operationPath , $ operation , $ path );
227
+ }
228
+ }
229
+
230
+
231
+ // new Node\Stmt\Switch_(
232
+ // new Node\Expr\Variable('method'),
233
+ // iterator_to_array((function (array $sortedOperations) use ($factory): iterable {
234
+ // foreach ($sortedOperations as $method => $operation) {
235
+ // yield new Node\Stmt\Case_(
236
+ // new Node\Scalar\String_($method),
237
+ // [
238
+ // ...self::traverseOperations($operation['operations'], $operation['paths'], 0),
239
+ // new Node\Stmt\Break_(),
240
+ // ],
241
+ // );
242
+ // }
243
+ // })($sortedOperations))
244
+ // )
245
+
246
+ $ operationsIfs = [];
247
+ foreach ($ sortedOperations as $ method => $ ops ) {
248
+ $ opsTmts = [];
249
+ foreach ($ ops as $ chunkCount => $ moar ) {
250
+ $ opsTmts [] = [
251
+ new Node \Expr \BinaryOp \Identical (
252
+ new Node \Expr \Variable ('pathChunksCount ' ),
253
+ new Node \Scalar \LNumber ($ chunkCount ),
254
+ ),
255
+ self ::traverseOperations ($ moar ['operations ' ], $ moar ['paths ' ], 0 ),
256
+ ];
221
257
}
258
+ $ operationsIfs [] = [
259
+ new Node \Expr \BinaryOp \Identical (
260
+ new Node \Expr \Variable ('method ' ),
261
+ new Node \Scalar \String_ ($ method ),
262
+ ),
263
+ (static function (array $ opsTmts ): array {
264
+ $ first = array_shift ($ opsTmts );
265
+ $ elseIfs = [];
266
+
267
+ foreach ($ opsTmts as $ opsTmt ) {
268
+ $ elseIfs [] = new Node \Stmt \ElseIf_ (...$ opsTmt );
269
+ }
270
+
271
+ return [
272
+ new Node \Stmt \If_ (
273
+ $ first [0 ],
274
+ [
275
+ 'stmts ' => $ first [1 ],
276
+ 'elseifs ' => $ elseIfs ,
277
+ ],
278
+ )
279
+ ];
280
+ })($ opsTmts ),
281
+ ];
222
282
}
223
283
284
+ $ firstOperationsIfs = array_shift ($ operationsIfs );
285
+ $ operationsIf = new Node \Stmt \If_ (
286
+ $ firstOperationsIfs [0 ],
287
+ [
288
+ 'stmts ' => $ firstOperationsIfs [1 ],
289
+ 'elseifs ' => (static function (array $ operationsIfs ): array {
290
+ $ elseIfs = [];
291
+
292
+ foreach ($ operationsIfs as $ operationsIf ) {
293
+ $ elseIfs [] = new Node \Stmt \ElseIf_ (...$ operationsIf );
294
+ }
295
+
296
+ return $ elseIfs ;
297
+ })($ operationsIfs ),
298
+ ],
299
+ );
300
+
224
301
$ class ->addStmt (
225
302
$ factory ->method ('callAsync ' )->makePublic ()->setDocComment (
226
303
new Doc (implode (PHP_EOL , [
@@ -244,6 +321,90 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
244
321
' */ ' ,
245
322
]))
246
323
)->addParam ((new Param ('call ' ))->setType ('string ' ))->addParam ((new Param ('params ' ))->setType ('array ' )->setDefault ([]))->addStmt (
324
+ new Node \Expr \Assign (
325
+ new Node \Expr \Variable ('resolvedUrlPassed ' ),
326
+ new Node \Expr \ConstFetch (
327
+ new Node \Name ('false ' ),
328
+ )
329
+ )
330
+ )->addStmt (
331
+ new Node \Stmt \If_ (
332
+ new Node \Expr \BinaryOp \NotIdentical (
333
+ new Node \Expr \FuncCall (
334
+ new Node \Name ('strpos ' ),
335
+ [
336
+ new Arg (
337
+ new Node \Expr \Variable ('call ' ),
338
+ ),
339
+ new Arg (
340
+ new Node \Scalar \String_ ($ client ->baseUrl ),
341
+ ),
342
+ ],
343
+ ),
344
+ new Node \Scalar \LNumber (0 ),
345
+ ),
346
+ [
347
+ 'stmts ' => [
348
+ new Node \Stmt \Expression (
349
+ new Node \Expr \Assign (
350
+ new Node \Expr \Variable ('call ' ),
351
+ new Node \Expr \FuncCall (
352
+ new Node \Name ('substr ' ),
353
+ [
354
+ new Arg (
355
+ new Node \Expr \Variable ('call ' ),
356
+ ),
357
+ new Arg (
358
+ new Node \Scalar \LNumber (strlen ($ client ->baseUrl )),
359
+ ),
360
+ ],
361
+ ),
362
+ ),
363
+ ),
364
+ ],
365
+ ],
366
+ )
367
+ )->addStmt (
368
+ new Node \Stmt \If_ (
369
+ new Node \Expr \BinaryOp \Identical (
370
+ new Node \Expr \FuncCall (
371
+ new Node \Name ('strpos ' ),
372
+ [
373
+ new Arg (
374
+ new Node \Expr \Variable ('call ' ),
375
+ ),
376
+ new Arg (
377
+ new Node \Scalar \String_ (' ' ),
378
+ ),
379
+ ],
380
+ ),
381
+ new Node \Expr \ConstFetch (
382
+ new Node \Name ('false ' ),
383
+ ),
384
+ ),
385
+ [
386
+ 'stmts ' => [
387
+ new Node \Stmt \Expression (
388
+ new Node \Expr \Assign (
389
+ new Node \Expr \Variable ('call ' ),
390
+ new Node \Expr \BinaryOp \Concat (
391
+ new Node \Scalar \String_ ('GET ' ),
392
+ new Node \Expr \Variable ('call ' ),
393
+ ),
394
+ ),
395
+ ),
396
+ new Node \Stmt \Expression (
397
+ new Node \Expr \Assign (
398
+ new Node \Expr \Variable ('resolvedUrlPassed ' ),
399
+ new Node \Expr \ConstFetch (
400
+ new Node \Name ('true ' ),
401
+ )
402
+ ),
403
+ ),
404
+ ],
405
+ ],
406
+ )
407
+ )->addStmt (
247
408
new Node \Expr \Assign (
248
409
new Node \Expr \Array_ ([
249
410
new Node \Expr \ArrayItem (
@@ -282,20 +443,19 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
282
443
],
283
444
)
284
445
)
285
- )->addStmt (new Node \Stmt \Switch_ (
286
- new Node \Expr \Variable ('method ' ),
287
- iterator_to_array ((function (array $ sortedOperations ) use ($ factory ): iterable {
288
- foreach ($ sortedOperations as $ method => $ operation ) {
289
- yield new Node \Stmt \Case_ (
290
- new Node \Scalar \String_ ($ method ),
291
- [
292
- ...self ::traverseOperations ($ operation ['operations ' ], $ operation ['paths ' ], 0 ),
293
- new Node \Stmt \Break_ (),
294
- ],
295
- );
296
- }
297
- })($ sortedOperations ))
298
- ))->addStmt (
446
+ )->addStmt (
447
+ new Node \Expr \Assign (
448
+ new Node \Expr \Variable ('pathChunksCount ' ),
449
+ new Node \Expr \FuncCall (
450
+ new Node \Name ('count ' ),
451
+ [
452
+ new Arg (
453
+ new Node \Expr \Variable ('pathChunks ' ),
454
+ ),
455
+ ],
456
+ )
457
+ )
458
+ )->addStmt ($ operationsIf )->addStmt (
299
459
new Node \Stmt \Throw_ (
300
460
new Node \Expr \New_ (
301
461
new Node \Name ('\InvalidArgumentException ' )
@@ -342,48 +502,69 @@ private static function traverseOperationPaths(array $operations, array &$operat
342
502
343
503
private static function traverseOperations (array $ operations , array $ paths , int $ level ): array
344
504
{
505
+ $ nonArgumentPathChunks = [];
506
+ foreach ($ paths as $ pathChunk => $ _ ) {
507
+ if (strpos ($ pathChunk , '{ ' ) === 0 ) {
508
+ continue ;
509
+ }
510
+
511
+ $ nonArgumentPathChunks [] = new Node \Expr \ArrayItem (new Node \Scalar \String_ ($ pathChunk ));
512
+ }
513
+
345
514
$ ifs = [];
346
515
foreach ($ operations as $ operation ) {
347
516
$ ifs [] = [
348
517
new Node \Expr \BinaryOp \Equal (
349
- new Node \Scalar \String_ ($ operation ['operation ' ]->method . ' ' . $ operation ['operation ' ]->path ),
350
518
new Node \Expr \Variable ('call ' ),
519
+ new Node \Scalar \String_ ($ operation ['operation ' ]->method . ' ' . $ operation ['operation ' ]->path ),
351
520
),
352
- [
353
- 'stmts ' => static ::callOperation (...$ operation ),
354
- ]
521
+ static ::callOperation (...$ operation ),
355
522
];
356
523
}
357
524
foreach ($ paths as $ pathChunk => $ path ) {
525
+ $ baseCondition = new Node \Expr \BinaryOp \Equal (
526
+ new Node \Expr \ArrayDimFetch (
527
+ new Node \Expr \Variable ('pathChunks ' ),
528
+ new Node \Scalar \LNumber ($ level ),
529
+ ),
530
+ new Node \Scalar \String_ ($ pathChunk ),
531
+ );
358
532
$ ifs [] = [
359
- new Node \Expr \BinaryOp \BooleanAnd (
360
- new Node \ Expr \ BinaryOp \ Equal (
361
- new Node \Expr \FuncCall (
362
- new Node \Name ( ' array_key_exists ' ),
363
- [
364
- new Arg (
365
- new Node \Scalar \ LNumber ( $ level ),
366
- ),
367
- new Arg (
368
- new Node \ Expr \ Variable ( ' pathChunks ' ),
533
+ (!( strpos ( $ pathChunk , ' { ' ) === 0 )) ? $ baseCondition : new Node \Expr \BinaryOp \BooleanOr (
534
+ $ baseCondition ,
535
+ new Node \Expr \BooleanNot (
536
+ new Node \Expr \ BooleanNot (
537
+ new Node \ Expr \ BinaryOp \ BooleanAnd (
538
+ new Node \ Expr \ BinaryOp \ Equal (
539
+ new Node \Expr \ Variable ( ' resolvedUrlPassed ' ),
540
+ new Node \ Expr \ ConstFetch (
541
+ new Node \ Name ( ' true ' )
542
+ ),
369
543
),
370
- ],
371
- ),
372
- new Node \Expr \ConstFetch (
373
- new Node \Name ('true ' ),
374
- ),
375
- ),
376
- new Node \Expr \BinaryOp \Equal (
377
- new Node \Scalar \String_ ($ pathChunk ),
378
- new Node \Expr \ArrayDimFetch (
379
- new Node \Expr \Variable ('pathChunks ' ),
380
- new Node \Scalar \LNumber ($ level ),
544
+ new Node \Expr \BinaryOp \NotIdentical (
545
+ new Node \Expr \FuncCall (
546
+ new Node \Name ('in_array ' ),
547
+ [
548
+ new Arg (
549
+ new Node \Expr \ArrayDimFetch (
550
+ new Node \Expr \Variable ('pathChunks ' ),
551
+ new Node \Scalar \LNumber ($ level ),
552
+ ),
553
+ ),
554
+ new Arg (
555
+ new Node \Expr \Array_ ($ nonArgumentPathChunks ),
556
+ ),
557
+ ],
558
+ ),
559
+ new Node \Expr \ConstFetch (
560
+ new Node \Name ('true ' ),
561
+ ),
562
+ )
563
+ ),
381
564
),
382
565
),
383
566
),
384
- [
385
- 'stmts ' => self ::traverseOperations ($ path ['operations ' ], $ path ['paths ' ], $ level + 1 ),
386
- ],
567
+ self ::traverseOperations ($ path ['operations ' ], $ path ['paths ' ], $ level + 1 ),
387
568
];
388
569
}
389
570
@@ -394,13 +575,13 @@ private static function traverseOperations(array $operations, array $paths, int
394
575
$ elfseIfs = [];
395
576
$ baseIf = array_shift ($ ifs );
396
577
foreach ($ ifs as $ if ) {
397
- $ elfseIfs [] = new Node \Stmt \ElseIf_ ($ if [0 ], $ if [1 ][ ' stmts ' ] );
578
+ $ elfseIfs [] = new Node \Stmt \ElseIf_ ($ if [0 ], $ if [1 ]);
398
579
}
399
580
400
581
return [new Node \Stmt \If_ (
401
582
$ baseIf [0 ],
402
583
[
403
- 'stmts ' => $ baseIf [1 ][ ' stmts ' ] ,
584
+ 'stmts ' => $ baseIf [1 ],
404
585
'elseifs ' => $ elfseIfs ,
405
586
],
406
587
)];
0 commit comments