Skip to content

Commit 1537f7d

Browse files
authored
Merge pull request #134 from php-api-clients/introduce-internal-configuration-objects
Introduce internal configuration objects
2 parents 9fb8279 + 2f422d2 commit 1537f7d

File tree

6 files changed

+70
-18
lines changed

6 files changed

+70
-18
lines changed

bin/openapi-client-generator.source

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/usr/bin/php
22
<?php declare(strict_types=1);
33

4+
use ApiClients\Tools\OpenApiClientGenerator\Configuration;
45
use ApiClients\Tools\OpenApiClientGenerator\Generator;
6+
use EventSauce\ObjectHydrator\ObjectMapperUsingReflection;
57
use Symfony\Component\Yaml\Yaml;
68

79
(function(): void {
@@ -15,9 +17,9 @@ use Symfony\Component\Yaml\Yaml;
1517
/**
1618
* Create and boot up the application
1719
*/
18-
exit((function (string $configuration): int {
19-
$yaml = Yaml::parseFile($configuration);
20-
(new Generator($yaml['spec']))->generate($yaml['namespace'] . '\\', dirname($configuration) . DIRECTORY_SEPARATOR . $yaml['destination'], $yaml['schemas'] ?? [], $yaml['voter'] ?? []);
20+
exit((function (string $configurationFile): int {
21+
$configuration = (new ObjectMapperUsingReflection())->hydrateObject(Configuration::class, Yaml::parseFile($configurationFile));
22+
(new Generator($configuration->spec))->generate($configuration->namespace . '\\', dirname($configurationFile) . DIRECTORY_SEPARATOR . $configuration->destination, $configuration);
2123

2224
return 0;
2325
})($configuration));

src/Configuration.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace ApiClients\Tools\OpenApiClientGenerator;
4+
5+
use ApiClients\Tools\OpenApiClientGenerator\Configuration\Voter;
6+
use ApiClients\Tools\OpenApiClientGenerator\Configuration\Schemas;
7+
8+
final readonly class Configuration
9+
{
10+
public function __construct(
11+
public string $spec,
12+
public string $namespace,
13+
public string $destination,
14+
public ?Schemas $schemas,
15+
public ?Voter $voter,
16+
) {
17+
}
18+
}

src/Configuration/Schemas.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace ApiClients\Tools\OpenApiClientGenerator\Configuration;
4+
5+
use EventSauce\ObjectHydrator\MapFrom;
6+
7+
final readonly class Schemas
8+
{
9+
public function __construct(
10+
#[MapFrom('allowDuplication')]
11+
public bool $allowDuplication,
12+
) {
13+
}
14+
}

src/Configuration/Voter.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace ApiClients\Tools\OpenApiClientGenerator\Configuration;
4+
5+
use ApiClients\Tools\OpenApiClientGenerator\Contract\Voter as VoterContract;
6+
use EventSauce\ObjectHydrator\MapFrom;
7+
8+
final readonly class Voter
9+
{
10+
/**
11+
* @param array<class-string<VoterContract\ListOperation>>|null $listOperation
12+
* @param array<class-string<VoterContract\StreamOperation>>|null $streamOperation
13+
*/
14+
public function __construct(
15+
#[MapFrom('listOperation')]
16+
public ?array $listOperation,
17+
#[MapFrom('streamOperation')]
18+
public ?array $streamOperation,
19+
) {
20+
}
21+
}

src/Gatherer/Path.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@
22

33
namespace ApiClients\Tools\OpenApiClientGenerator\Gatherer;
44

5-
use ApiClients\Tools\OpenApiClientGenerator\Contract\Voter\StreamOperation;
5+
use ApiClients\Tools\OpenApiClientGenerator\Configuration\Voter;
66
use ApiClients\Tools\OpenApiClientGenerator\Utils;
77
use ApiClients\Tools\OpenApiClientGenerator\Registry\Schema as SchemaRegistry;
88
use cebe\openapi\spec\PathItem;
99

1010
final class Path
1111
{
12-
/**
13-
* @param array{streamOperation: array<StreamOperation>} $voters
14-
*/
1512
public static function gather(
1613
string $className,
1714
string $path,
1815
PathItem $pathItem,
1916
SchemaRegistry $schemaRegistry,
20-
array $voters,
17+
?Voter $voters,
2118
): \ApiClients\Tools\OpenApiClientGenerator\Representation\Path {
2219
$className = Utils::fixKeyword($className);
2320
$operations = [];
@@ -38,9 +35,9 @@ public static function gather(
3835
$schemaRegistry,
3936
);
4037

41-
if (array_key_exists('listOperation', $voters)) {
38+
if ($voters !== null && is_array($voters->listOperation)) {
4239
$shouldStream = false;
43-
foreach ($voters['listOperation'] as $voter) {
40+
foreach ($voters->listOperation as $voter) {
4441
if ($voter::list($opp)) {
4542
$shouldStream = true;
4643
break;
@@ -65,9 +62,9 @@ public static function gather(
6562
}
6663
}
6764

68-
if (array_key_exists('streamOperation', $voters)) {
65+
if ($voters !== null && is_array($voters->streamOperation)) {
6966
$shouldStream = false;
70-
foreach ($voters['streamOperation'] as $voter) {
67+
foreach ($voters->streamOperation as $voter) {
7168
if ($voter::stream($opp)) {
7269
$shouldStream = true;
7370
break;

src/Generator.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ public function __construct(string $specUrl)
3030
$this->spec = Reader::readFromYamlFile($specUrl);
3131
}
3232

33-
public function generate(string $namespace, string $destinationPath, array $schemas, array $voters)
33+
public function generate(string $namespace, string $destinationPath, Configuration $configuration)
3434
{
3535
$existingFiles = iterator_to_array(Files::listExistingFiles($destinationPath . DIRECTORY_SEPARATOR));
3636
$namespace = Utils::cleanUpNamespace($namespace);
3737
$codePrinter = new Standard();
3838

39-
foreach ($this->all($namespace, $destinationPath . DIRECTORY_SEPARATOR, $schemas, $voters) as $file) {
39+
foreach ($this->all($namespace, $destinationPath . DIRECTORY_SEPARATOR, $configuration) as $file) {
4040
$fileName = $destinationPath . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, substr($file->fqcn, strlen($namespace))) . '.php';
4141
if ($file->contents instanceof Node\Stmt\Namespace_) {
4242
array_unshift($file->contents->stmts, ...[
@@ -102,11 +102,11 @@ public function generate(string $namespace, string $destinationPath, array $sche
102102
/**
103103
* @return iterable<File>
104104
*/
105-
private function all(string $namespace, string $rootPath, array $schemas, array $voters): iterable
105+
private function all(string $namespace, string $rootPath, Configuration $configuration): iterable
106106
{
107107
$schemaRegistry = new SchemaRegistry();
108-
if (array_key_exists('allowDuplication', $schemas)) {
109-
$schemaRegistry->setAllowDuplicatedSchemas($schemas['allowDuplication']);
108+
if ($configuration->schemas !== null && $configuration->schemas->allowDuplication !== null) {
109+
$schemaRegistry->setAllowDuplicatedSchemas($configuration->schemas->allowDuplication);
110110
}
111111
$schemas = [];
112112
$throwableSchemaRegistry = new ThrowableSchema();
@@ -141,7 +141,7 @@ private function all(string $namespace, string $rootPath, array $schemas, array
141141
continue;
142142
}
143143

144-
$paths[] = \ApiClients\Tools\OpenApiClientGenerator\Gatherer\Path::gather($pathClassName, $path, $pathItem, $schemaRegistry, $voters);
144+
$paths[] = \ApiClients\Tools\OpenApiClientGenerator\Gatherer\Path::gather($pathClassName, $path, $pathItem, $schemaRegistry, $configuration->voter);
145145

146146
}
147147
}

0 commit comments

Comments
 (0)