Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 56 additions & 44 deletions src/Generator/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,64 @@ public static function generate(string $namespace, array $clients, SchemaRegistr
}

$class->addStmt(
$factory->method('callAsync')->makePublic()->setReturnType(
new Node\Name('\\' . PromiseInterface::class)
)->setDocComment(
$factory->method('call')->makePublic()->setDocComment(
new Doc(implode(PHP_EOL, [
'/**',
' * @return \\' . PromiseInterface::class . '<' . implode('|', array_unique($callReturnTypes)) . '>',
' * @return ' . (function (array $operationCalls): string {
$count = count($operationCalls);
$lastItem = $count - 1;
$left = '';
$right = '';
for ($i = 0; $i < $count; $i++) {
if ($i !== $lastItem) {
$left .= '($call is ' . $operationCalls[$i]['className'] . '::OPERATION_MATCH ? ' . implode('|', array_unique($operationCalls[$i]['returnType'])) . ' : ';
} else {
$left .= implode('|', array_unique($operationCalls[$i]['returnType']));
}
$right .= ')';
}
return $left . $right;
})($operationCalls),
' */',
]))
)->addParam((new Param('call'))->setType('string'))->addParam((new Param('params'))->setType('array')->setDefault([]))->addStmt(new Node\Stmt\Return_(
new Node\Expr\FuncCall(
new Node\Name('\React\Async\await'),
[
new Node\Arg(
new Node\Expr\MethodCall(
new Node\Expr\Variable('this'),
new Node\Name('callAsync'),
[
new Node\Arg(new Node\Expr\Variable('call')),
new Node\Arg(new Node\Expr\Variable('params')),
]
)
),
],
)
))
);

$class->addStmt(
$factory->method('callAsync')->makePublic()->setDocComment(
new Doc(implode(PHP_EOL, [
'/**',
' * @return ' . (function (array $operationCalls): string {
$count = count($operationCalls);
$lastItem = $count - 1;
$left = '';
$right = '';
for ($i = 0; $i < $count; $i++) {
if ($i !== $lastItem) {
$left .= '($call is ' . $operationCalls[$i]['className'] . '::OPERATION_MATCH ? \\' . PromiseInterface::class . '<' . implode('|', array_unique($operationCalls[$i]['returnType'])) . '> : ';
} else {
$left .= '\\' . PromiseInterface::class . '<' . implode('|', array_unique($operationCalls[$i]['returnType'])) . '>';
}
$right .= ')';
}
return $left . $right;
})($operationCalls),
' */',
]))
)->addParam((new Param('call'))->setType('string'))->addParam((new Param('params'))->setType('array')->setDefault([]))->addStmt(new Node\Stmt\Switch_(
Expand Down Expand Up @@ -341,46 +393,6 @@ public static function generate(string $namespace, array $clients, SchemaRegistr
))
);

$class->addStmt(
$factory->method('call')->makePublic()->setDocComment(
new Doc(implode(PHP_EOL, [
'/**',
' * @return ' . (function (array $operationCalls): string {
$count = count($operationCalls);
$lastItem = $count - 1;
$left = '';
$right = '';
for ($i = 0; $i < $count; $i++) {
if ($i !== $lastItem) {
$left .= '($call is ' . $operationCalls[$i]['className'] . '::OPERATION_MATCH ? ' . implode('|', array_unique($operationCalls[$i]['returnType'])) . ' : ';
} else {
$left .= implode('|', array_unique($operationCalls[$i]['returnType']));
}
$right .= ')';
}
return $left . $right;
})($operationCalls),
' */',
]))
)->setReturnType(implode('|', array_unique($rawCallReturnTypes)))->addParam((new Param('call'))->setType('string'))->addParam((new Param('params'))->setType('array')->setDefault([]))->addStmt(new Node\Stmt\Return_(
new Node\Expr\FuncCall(
new Node\Name('\React\Async\await'),
[
new Node\Arg(
new Node\Expr\MethodCall(
new Node\Expr\Variable('this'),
new Node\Name('callAsync'),
[
new Node\Arg(new Node\Expr\Variable('call')),
new Node\Arg(new Node\Expr\Variable('params')),
]
)
),
],
)
))
);

yield new File($namespace . '\\' . 'Client', $stmt->addStmt($class)->getNode());
}
}
24 changes: 17 additions & 7 deletions src/Generator/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ public static function generate(string $namespace, array $clients, SchemaRegistr
}

$class->addStmt(
$factory->method('call')->makePublic()->setReturnType(
new Node\Name(implode('|', array_unique($rawCallReturnTypes)))
)->setDocComment(
$factory->method('call')->makePublic()->setDocComment(
new Doc(implode(PHP_EOL, [
'/**',
' * @return ' . (function (array $operationCalls): string {
Expand All @@ -100,12 +98,24 @@ public static function generate(string $namespace, array $clients, SchemaRegistr
);

$class->addStmt(
$factory->method('callAsync')->makePublic()->setReturnType(
new Node\Name('\\' . PromiseInterface::class)
)->setDocComment(
$factory->method('callAsync')->makePublic()->setDocComment(
new Doc(implode(PHP_EOL, [
'/**',
' * @return \\' . PromiseInterface::class . '<' . implode('|', array_unique($callReturnTypes)) . '>',
' * @return ' . (function (array $operationCalls): string {
$count = count($operationCalls);
$lastItem = $count - 1;
$left = '';
$right = '';
for ($i = 0; $i < $count; $i++) {
if ($i !== $lastItem) {
$left .= '($call is ' . $operationCalls[$i]['className'] . '::OPERATION_MATCH ? \\' . PromiseInterface::class . '<' . implode('|', array_unique($operationCalls[$i]['returnType'])) . '> : ';
} else {
$left .= '\\' . PromiseInterface::class . '<' . implode('|', array_unique($operationCalls[$i]['returnType'])) . '>';
}
$right .= ')';
}
return $left . $right;
})($operationCalls),
' */',
]))
)->addParam((new Param('call'))->setType('string'))->addParam((new Param('params'))->setType('array')->setDefault([]))
Expand Down