Skip to content

Commit

Permalink
Support passing full URL into call method
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus committed Mar 4, 2023
1 parent d297d16 commit a8fe798
Showing 1 changed file with 227 additions and 46 deletions.
273 changes: 227 additions & 46 deletions src/Generator/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ApiClients\Tools\OpenApiClientGenerator\Generator;

use ApiClients\Client\Github\Schema\WebhookLabelEdited\Changes\Name;
use ApiClients\Contracts\HTTP\Headers\AuthenticationInterface;
use ApiClients\Contracts\OpenAPI\WebHooksInterface;
use ApiClients\Tools\OpenApiClientGenerator\File;
Expand All @@ -27,6 +28,7 @@
use RingCentral\Psr7\Request;
use Rx\Observable;
use Twig\Node\Expression\Binary\AndBinary;
use Twig\Node\Expression\Binary\OrBinary;

final class Client
{
Expand Down Expand Up @@ -209,18 +211,93 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
} else {
$operationPath = explode('/', $operation->path);
}
$operationPathCount = count($operationPath);

if (!array_key_exists($operation->method, $sortedOperations)) {
$sortedOperations[$operation->method] = [
$sortedOperations[$operation->method] = [];
}
if (!array_key_exists($operationPathCount, $sortedOperations[$operation->method])) {
$sortedOperations[$operation->method][$operationPathCount] = [
'operations' => [],
'paths' => [],
];
}

$sortedOperations[$operation->method] = self::traverseOperationPaths($sortedOperations[$operation->method], $operationPath, $operation, $path);
$sortedOperations[$operation->method][$operationPathCount] = self::traverseOperationPaths($sortedOperations[$operation->method][$operationPathCount], $operationPath, $operation, $path);
}
}


// new Node\Stmt\Switch_(
// new Node\Expr\Variable('method'),
// iterator_to_array((function (array $sortedOperations) use ($factory): iterable {
// foreach ($sortedOperations as $method => $operation) {
// yield new Node\Stmt\Case_(
// new Node\Scalar\String_($method),
// [
// ...self::traverseOperations($operation['operations'], $operation['paths'], 0),
// new Node\Stmt\Break_(),
// ],
// );
// }
// })($sortedOperations))
// )

$operationsIfs = [];
foreach ($sortedOperations as $method => $ops) {
$opsTmts = [];
foreach ($ops as $chunkCount => $moar) {
$opsTmts[] = [
new Node\Expr\BinaryOp\Identical(
new Node\Expr\Variable('pathChunksCount'),
new Node\Scalar\LNumber($chunkCount),
),
self::traverseOperations($moar['operations'], $moar['paths'], 0),
];
}
$operationsIfs[] = [
new Node\Expr\BinaryOp\Identical(
new Node\Expr\Variable('method'),
new Node\Scalar\String_($method),
),
(static function (array $opsTmts): array {
$first = array_shift($opsTmts);
$elseIfs = [];

foreach ($opsTmts as $opsTmt) {
$elseIfs[] = new Node\Stmt\ElseIf_(...$opsTmt);
}

return [
new Node\Stmt\If_(
$first[0],
[
'stmts' => $first[1],
'elseifs' => $elseIfs,
],
)
];
})($opsTmts),
];
}

$firstOperationsIfs = array_shift($operationsIfs);
$operationsIf = new Node\Stmt\If_(
$firstOperationsIfs[0],
[
'stmts' => $firstOperationsIfs[1],
'elseifs' => (static function (array $operationsIfs): array {
$elseIfs = [];

foreach ($operationsIfs as $operationsIf) {
$elseIfs[] = new Node\Stmt\ElseIf_(...$operationsIf);
}

return $elseIfs;
})($operationsIfs),
],
);

$class->addStmt(
$factory->method('callAsync')->makePublic()->setDocComment(
new Doc(implode(PHP_EOL, [
Expand All @@ -244,6 +321,90 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
' */',
]))
)->addParam((new Param('call'))->setType('string'))->addParam((new Param('params'))->setType('array')->setDefault([]))->addStmt(
new Node\Expr\Assign(
new Node\Expr\Variable('resolvedUrlPassed'),
new Node\Expr\ConstFetch(
new Node\Name('false'),
)
)
)->addStmt(
new Node\Stmt\If_(
new Node\Expr\BinaryOp\NotIdentical(
new Node\Expr\FuncCall(
new Node\Name('strpos'),
[
new Arg(
new Node\Expr\Variable('call'),
),
new Arg(
new Node\Scalar\String_($client->baseUrl),
),
],
),
new Node\Scalar\LNumber(0),
),
[
'stmts' => [
new Node\Stmt\Expression(
new Node\Expr\Assign(
new Node\Expr\Variable('call'),
new Node\Expr\FuncCall(
new Node\Name('substr'),
[
new Arg(
new Node\Expr\Variable('call'),
),
new Arg(
new Node\Scalar\LNumber(strlen($client->baseUrl)),
),
],
),
),
),
],
],
)
)->addStmt(
new Node\Stmt\If_(
new Node\Expr\BinaryOp\Identical(
new Node\Expr\FuncCall(
new Node\Name('strpos'),
[
new Arg(
new Node\Expr\Variable('call'),
),
new Arg(
new Node\Scalar\String_(' '),
),
],
),
new Node\Expr\ConstFetch(
new Node\Name('false'),
),
),
[
'stmts' => [
new Node\Stmt\Expression(
new Node\Expr\Assign(
new Node\Expr\Variable('call'),
new Node\Expr\BinaryOp\Concat(
new Node\Scalar\String_('GET '),
new Node\Expr\Variable('call'),
),
),
),
new Node\Stmt\Expression(
new Node\Expr\Assign(
new Node\Expr\Variable('resolvedUrlPassed'),
new Node\Expr\ConstFetch(
new Node\Name('true'),
)
),
),
],
],
)
)->addStmt(
new Node\Expr\Assign(
new Node\Expr\Array_([
new Node\Expr\ArrayItem(
Expand Down Expand Up @@ -282,20 +443,19 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
],
)
)
)->addStmt(new Node\Stmt\Switch_(
new Node\Expr\Variable('method'),
iterator_to_array((function (array $sortedOperations) use ($factory): iterable {
foreach ($sortedOperations as $method => $operation) {
yield new Node\Stmt\Case_(
new Node\Scalar\String_($method),
[
...self::traverseOperations($operation['operations'], $operation['paths'], 0),
new Node\Stmt\Break_(),
],
);
}
})($sortedOperations))
))->addStmt(
)->addStmt(
new Node\Expr\Assign(
new Node\Expr\Variable('pathChunksCount'),
new Node\Expr\FuncCall(
new Node\Name('count'),
[
new Arg(
new Node\Expr\Variable('pathChunks'),
),
],
)
)
)->addStmt($operationsIf)->addStmt(
new Node\Stmt\Throw_(
new Node\Expr\New_(
new Node\Name('\InvalidArgumentException')
Expand Down Expand Up @@ -342,48 +502,69 @@ private static function traverseOperationPaths(array $operations, array &$operat

private static function traverseOperations(array $operations, array $paths, int $level): array
{
$nonArgumentPathChunks = [];
foreach ($paths as $pathChunk => $_) {
if (strpos($pathChunk, '{') === 0) {
continue;
}

$nonArgumentPathChunks[] = new Node\Expr\ArrayItem(new Node\Scalar\String_($pathChunk));
}

$ifs = [];
foreach ($operations as $operation) {
$ifs[] = [
new Node\Expr\BinaryOp\Equal(
new Node\Scalar\String_($operation['operation']->method . ' ' . $operation['operation']->path),
new Node\Expr\Variable('call'),
new Node\Scalar\String_($operation['operation']->method . ' ' . $operation['operation']->path),
),
[
'stmts' => static::callOperation(...$operation),
]
static::callOperation(...$operation),
];
}
foreach ($paths as $pathChunk => $path) {
$baseCondition = new Node\Expr\BinaryOp\Equal(
new Node\Expr\ArrayDimFetch(
new Node\Expr\Variable('pathChunks'),
new Node\Scalar\LNumber($level),
),
new Node\Scalar\String_($pathChunk),
);
$ifs[] = [
new Node\Expr\BinaryOp\BooleanAnd(
new Node\Expr\BinaryOp\Equal(
new Node\Expr\FuncCall(
new Node\Name('array_key_exists'),
[
new Arg(
new Node\Scalar\LNumber($level),
),
new Arg(
new Node\Expr\Variable('pathChunks'),
(!(strpos($pathChunk, '{') === 0)) ? $baseCondition : new Node\Expr\BinaryOp\BooleanOr(
$baseCondition,
new Node\Expr\BooleanNot(
new Node\Expr\BooleanNot(
new Node\Expr\BinaryOp\BooleanAnd(
new Node\Expr\BinaryOp\Equal(
new Node\Expr\Variable('resolvedUrlPassed'),
new Node\Expr\ConstFetch(
new Node\Name('true')
),
),
],
),
new Node\Expr\ConstFetch(
new Node\Name('true'),
),
),
new Node\Expr\BinaryOp\Equal(
new Node\Scalar\String_($pathChunk),
new Node\Expr\ArrayDimFetch(
new Node\Expr\Variable('pathChunks'),
new Node\Scalar\LNumber($level),
new Node\Expr\BinaryOp\NotIdentical(
new Node\Expr\FuncCall(
new Node\Name('in_array'),
[
new Arg(
new Node\Expr\ArrayDimFetch(
new Node\Expr\Variable('pathChunks'),
new Node\Scalar\LNumber($level),
),
),
new Arg(
new Node\Expr\Array_($nonArgumentPathChunks),
),
],
),
new Node\Expr\ConstFetch(
new Node\Name('true'),
),
)
),
),
),
),
[
'stmts' => self::traverseOperations($path['operations'], $path['paths'], $level + 1),
],
self::traverseOperations($path['operations'], $path['paths'], $level + 1),
];
}

Expand All @@ -394,13 +575,13 @@ private static function traverseOperations(array $operations, array $paths, int
$elfseIfs = [];
$baseIf = array_shift($ifs);
foreach ($ifs as $if) {
$elfseIfs[] = new Node\Stmt\ElseIf_($if[0], $if[1]['stmts']);
$elfseIfs[] = new Node\Stmt\ElseIf_($if[0], $if[1]);
}

return [new Node\Stmt\If_(
$baseIf[0],
[
'stmts' => $baseIf[1]['stmts'],
'stmts' => $baseIf[1],
'elseifs' => $elfseIfs,
],
)];
Expand Down

0 comments on commit a8fe798

Please sign in to comment.