Skip to content

Commit a8fe798

Browse files
committed
Support passing full URL into call method
1 parent d297d16 commit a8fe798

File tree

1 file changed

+227
-46
lines changed

1 file changed

+227
-46
lines changed

src/Generator/Client.php

+227-46
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ApiClients\Tools\OpenApiClientGenerator\Generator;
44

5+
use ApiClients\Client\Github\Schema\WebhookLabelEdited\Changes\Name;
56
use ApiClients\Contracts\HTTP\Headers\AuthenticationInterface;
67
use ApiClients\Contracts\OpenAPI\WebHooksInterface;
78
use ApiClients\Tools\OpenApiClientGenerator\File;
@@ -27,6 +28,7 @@
2728
use RingCentral\Psr7\Request;
2829
use Rx\Observable;
2930
use Twig\Node\Expression\Binary\AndBinary;
31+
use Twig\Node\Expression\Binary\OrBinary;
3032

3133
final class Client
3234
{
@@ -209,18 +211,93 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
209211
} else {
210212
$operationPath = explode('/', $operation->path);
211213
}
214+
$operationPathCount = count($operationPath);
212215

213216
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] = [
215221
'operations' => [],
216222
'paths' => [],
217223
];
218224
}
219225

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+
];
221257
}
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+
];
222282
}
223283

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+
224301
$class->addStmt(
225302
$factory->method('callAsync')->makePublic()->setDocComment(
226303
new Doc(implode(PHP_EOL, [
@@ -244,6 +321,90 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
244321
' */',
245322
]))
246323
)->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(
247408
new Node\Expr\Assign(
248409
new Node\Expr\Array_([
249410
new Node\Expr\ArrayItem(
@@ -282,20 +443,19 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
282443
],
283444
)
284445
)
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(
299459
new Node\Stmt\Throw_(
300460
new Node\Expr\New_(
301461
new Node\Name('\InvalidArgumentException')
@@ -342,48 +502,69 @@ private static function traverseOperationPaths(array $operations, array &$operat
342502

343503
private static function traverseOperations(array $operations, array $paths, int $level): array
344504
{
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+
345514
$ifs = [];
346515
foreach ($operations as $operation) {
347516
$ifs[] = [
348517
new Node\Expr\BinaryOp\Equal(
349-
new Node\Scalar\String_($operation['operation']->method . ' ' . $operation['operation']->path),
350518
new Node\Expr\Variable('call'),
519+
new Node\Scalar\String_($operation['operation']->method . ' ' . $operation['operation']->path),
351520
),
352-
[
353-
'stmts' => static::callOperation(...$operation),
354-
]
521+
static::callOperation(...$operation),
355522
];
356523
}
357524
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+
);
358532
$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+
),
369543
),
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+
),
381564
),
382565
),
383566
),
384-
[
385-
'stmts' => self::traverseOperations($path['operations'], $path['paths'], $level + 1),
386-
],
567+
self::traverseOperations($path['operations'], $path['paths'], $level + 1),
387568
];
388569
}
389570

@@ -394,13 +575,13 @@ private static function traverseOperations(array $operations, array $paths, int
394575
$elfseIfs = [];
395576
$baseIf = array_shift($ifs);
396577
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]);
398579
}
399580

400581
return [new Node\Stmt\If_(
401582
$baseIf[0],
402583
[
403-
'stmts' => $baseIf[1]['stmts'],
584+
'stmts' => $baseIf[1],
404585
'elseifs' => $elfseIfs,
405586
],
406587
)];

0 commit comments

Comments
 (0)