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
84 changes: 21 additions & 63 deletions src/Generator/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,7 @@ public static function generate(Configuration $configuration, string $pathPrefix
$factory = new BuilderFactory();
$stmt = $factory->namespace(trim($configuration->namespace->source, '\\'));

$class = $factory->class('Client')->implement(new Node\Name('ClientInterface'))->makeFinal()->addStmt(
$factory->property('authentication')->setType('\\' . AuthenticationInterface::class)->makeReadonly()->makePrivate(),
)->addStmt(
$factory->property('browser')->setType('\\' . Browser::class)->makeReadonly()->makePrivate(),
)->addStmt(
$factory->property('requestSchemaValidator')->setType('\League\OpenAPIValidation\Schema\SchemaValidator')->makeReadonly()->makePrivate(),
)->addStmt(
$factory->property('responseSchemaValidator')->setType('\League\OpenAPIValidation\Schema\SchemaValidator')->makeReadonly()->makePrivate(),
);
$class = $factory->class('Client')->implement(new Node\Name('ClientInterface'))->makeFinal();

if ($configuration->entryPoints->call) {
$class->addStmt(
Expand All @@ -88,20 +80,10 @@ public static function generate(Configuration $configuration, string $pathPrefix
}

$class->addStmt(
$factory->property('hydrators')->setType('Hydrators')->makeReadonly()->makePrivate(),
)->addStmt(
$factory->property('routers')->setType('Routers')->makeReadonly()->makePrivate(),
)->addStmt(
$factory->method('__construct')->makePublic()->addParam(
(new Param('authentication'))->setType('\\' . AuthenticationInterface::class),
)->addStmt(
new Node\Expr\Assign(
new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'authentication',
),
new Node\Expr\Variable('authentication'),
),
)->addParam(
(new Param('browser'))->setType('\\' . Browser::class),
)->addStmt((static function (Representation\Client $client): Node\Expr {
Expand All @@ -119,24 +101,21 @@ public static function generate(Configuration $configuration, string $pathPrefix
);
}

return new Node\Expr\Assign(new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'browser',
), new Node\Expr\MethodCall(
$assignExpr,
'withFollowRedirects',
[
new Arg(
new Node\Expr\ConstFetch(new Node\Name('false')),
),
],
));
return new Node\Expr\Assign(
new Node\Expr\Variable('browser'),
new Node\Expr\MethodCall(
$assignExpr,
'withFollowRedirects',
[
new Arg(
new Node\Expr\ConstFetch(new Node\Name('false')),
),
],
),
);
})($client))->addStmt(
new Node\Expr\Assign(
new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'requestSchemaValidator',
),
new Node\Expr\Variable('requestSchemaValidator'),
new Node\Expr\New_(
new Node\Name('\League\OpenAPIValidation\Schema\SchemaValidator'),
[
Expand All @@ -149,10 +128,7 @@ public static function generate(Configuration $configuration, string $pathPrefix
),
)->addStmt(
new Node\Expr\Assign(
new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'responseSchemaValidator',
),
new Node\Expr\Variable('responseSchemaValidator'),
new Node\Expr\New_(
new Node\Name('\League\OpenAPIValidation\Schema\SchemaValidator'),
[
Expand All @@ -165,10 +141,7 @@ public static function generate(Configuration $configuration, string $pathPrefix
),
)->addStmt(
new Node\Expr\Assign(
new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'hydrators',
),
new Node\Expr\Variable('hydrators'),
new Node\Expr\New_(
new Node\Name('Hydrators'),
[],
Expand All @@ -185,50 +158,35 @@ public static function generate(Configuration $configuration, string $pathPrefix
new Node\Name('Operations'),
[
new Arg(
new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'browser',
),
new Node\Expr\Variable('browser'),
false,
false,
[],
new Node\Identifier('browser'),
),
new Arg(
new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'authentication',
),
new Node\Expr\Variable('authentication'),
false,
false,
[],
new Node\Identifier('authentication'),
),
new Arg(
new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'requestSchemaValidator',
),
new Node\Expr\Variable('requestSchemaValidator'),
false,
false,
[],
new Node\Identifier('requestSchemaValidator'),
),
new Arg(
new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'responseSchemaValidator',
),
new Node\Expr\Variable('responseSchemaValidator'),
false,
false,
[],
new Node\Identifier('responseSchemaValidator'),
),
new Arg(
new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'hydrators',
),
new Node\Expr\Variable('hydrators'),
false,
false,
[],
Expand Down
2 changes: 1 addition & 1 deletion tests/app/etc/openapi-client-generator.state
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@
},
{
"name": ".\/tests\/app\/src\/\/Client.php",
"hash": "6ff122e2242b2a6e4485e102494cb16e"
"hash": "ee4a2025a9354a33e9bb90bf8bc01f4b"
},
{
"name": ".\/tests\/app\/src\/\/Router\/Get\/Pets.php",
Expand Down
16 changes: 5 additions & 11 deletions tests/app/src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,16 @@
use ApiClients\Contracts;
final class Client implements ClientInterface
{
private readonly \ApiClients\Contracts\HTTP\Headers\AuthenticationInterface $authentication;
private readonly \React\Http\Browser $browser;
private readonly \League\OpenAPIValidation\Schema\SchemaValidator $requestSchemaValidator;
private readonly \League\OpenAPIValidation\Schema\SchemaValidator $responseSchemaValidator;
private array $router = array();
private readonly OperationsInterface $operations;
private readonly Hydrators $hydrators;
private readonly Routers $routers;
public function __construct(\ApiClients\Contracts\HTTP\Headers\AuthenticationInterface $authentication, \React\Http\Browser $browser)
{
$this->authentication = $authentication;
$this->browser = $browser->withBase('http://petstore.swagger.io/v1')->withFollowRedirects(false);
$this->requestSchemaValidator = new \League\OpenAPIValidation\Schema\SchemaValidator(\League\OpenAPIValidation\Schema\SchemaValidator::VALIDATE_AS_REQUEST);
$this->responseSchemaValidator = new \League\OpenAPIValidation\Schema\SchemaValidator(\League\OpenAPIValidation\Schema\SchemaValidator::VALIDATE_AS_RESPONSE);
$this->hydrators = new Hydrators();
$this->operations = new Operations(browser: $this->browser, authentication: $this->authentication, requestSchemaValidator: $this->requestSchemaValidator, responseSchemaValidator: $this->responseSchemaValidator, hydrators: $this->hydrators);
$browser = $browser->withBase('http://petstore.swagger.io/v1')->withFollowRedirects(false);
$requestSchemaValidator = new \League\OpenAPIValidation\Schema\SchemaValidator(\League\OpenAPIValidation\Schema\SchemaValidator::VALIDATE_AS_REQUEST);
$responseSchemaValidator = new \League\OpenAPIValidation\Schema\SchemaValidator(\League\OpenAPIValidation\Schema\SchemaValidator::VALIDATE_AS_RESPONSE);
$hydrators = new Hydrators();
$this->operations = new Operations(browser: $browser, authentication: $authentication, requestSchemaValidator: $requestSchemaValidator, responseSchemaValidator: $responseSchemaValidator, hydrators: $hydrators);
$this->routers = new Routers(browser: $this->browser, authentication: $this->authentication, requestSchemaValidator: $this->requestSchemaValidator, responseSchemaValidator: $this->responseSchemaValidator, hydrators: $this->hydrators);
}
// phpcs:disable
Expand Down