Skip to content

Commit 07afa2b

Browse files
authored
Merge pull request #22 from php-api-clients/use-generators-for-file-creation
Use generators for file creation
2 parents 12c7dc1 + 425a09c commit 07afa2b

File tree

6 files changed

+113
-178
lines changed

6 files changed

+113
-178
lines changed

src/File.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace ApiClients\Tools\OpenApiClientGenerator;
4+
5+
use ApiClients\Tools\OpenApiClientGenerator\Generator\Operation;
6+
use ApiClients\Tools\OpenApiClientGenerator\Generator\Path;
7+
use ApiClients\Tools\OpenApiClientGenerator\Generator\Schema;
8+
use cebe\openapi\Reader;
9+
use cebe\openapi\spec\OpenApi;
10+
use Jawira\CaseConverter\Convert;
11+
use PhpParser\Node;
12+
use PhpParser\PrettyPrinter\Standard;
13+
14+
final class File
15+
{
16+
private string $fqcn;
17+
private Node $contents;
18+
19+
public function __construct(string $path, Node $contents)
20+
{
21+
$this->fqcn = $path;
22+
$this->contents = $contents;
23+
}
24+
25+
public function fqcn(): string
26+
{
27+
return $this->fqcn;
28+
}
29+
30+
public function contents(): Node
31+
{
32+
return $this->contents;
33+
}
34+
}

src/Generator.php

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,34 @@ public function generate(string $namespace, string $destinationPath)
2424
{
2525
$namespace = $this->cleanUpNamespace($namespace);
2626
$codePrinter = new Standard();
27+
28+
foreach ($this->all($namespace) as $file) {
29+
$fileName = $destinationPath . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, substr($file->fqcn(), strlen($namespace)));
30+
@mkdir(dirname($fileName), 0744, true);
31+
file_put_contents($fileName . '.php', $codePrinter->prettyPrintFile([$file->contents()]) . PHP_EOL);
32+
}
33+
}
34+
35+
private function className(string $className): string
36+
{
37+
return str_replace(['{', '}', '-', '$'], ['Cb', 'Rcb', 'Dash', '_'], (new Convert($className))->toPascal());
38+
}
39+
40+
private function cleanUpNamespace(string $namespace): string
41+
{
42+
$namespace = str_replace('/', '\\', $namespace);
43+
$namespace = str_replace('\\\\', '\\', $namespace);
44+
45+
return $namespace;
46+
}
47+
48+
/**
49+
* @param string $namespace
50+
* @param string $destinationPath
51+
* @return iterable<File>
52+
*/
53+
private function all(string $namespace): iterable
54+
{
2755
if (count($this->spec->components->schemas ?? []) > 0) {
2856
$schemaClassNameMap = [];
2957
foreach ($this->spec->components->schemas as $name => $schema) {
@@ -38,16 +66,14 @@ public function generate(string $namespace, string $destinationPath)
3866
if (strlen($schemaClassName) === 0) {
3967
continue;
4068
}
41-
@mkdir(dirname($destinationPath . '/Schema/' . $schemaClassName), 0777, true);
42-
file_put_contents($destinationPath . '/Schema/' . $schemaClassName . '.php', $codePrinter->prettyPrintFile([
43-
Schema::generate(
44-
$name,
45-
$this->cleanUpNamespace($namespace . dirname('Schema/' . $schemaClassName)),
46-
strrev(explode('/', strrev($schemaClassName))[0]),
47-
$schema,
48-
$schemaClassNameMap
49-
),
50-
]) . PHP_EOL);
69+
70+
yield from Schema::generate(
71+
$name,
72+
$this->cleanUpNamespace($namespace . dirname('Schema/' . $schemaClassName)),
73+
strrev(explode('/', strrev($schemaClassName))[0]),
74+
$schema,
75+
$schemaClassNameMap
76+
);
5177
}
5278
}
5379

@@ -57,47 +83,31 @@ public function generate(string $namespace, string $destinationPath)
5783
if (strlen($pathClassName) === 0) {
5884
continue;
5985
}
60-
@mkdir(dirname($destinationPath . '/Path/' . $pathClassName), 0777, true);
61-
file_put_contents($destinationPath . '/Path/' . $pathClassName . '.php', $codePrinter->prettyPrintFile([
62-
Path::generate(
63-
$path,
64-
$this->cleanUpNamespace($namespace . dirname('Path/' . $pathClassName)),
65-
$namespace,
66-
strrev(explode('/', strrev($pathClassName))[0]),
67-
$pathItem
68-
),
69-
]) . PHP_EOL);
86+
87+
yield from Path::generate(
88+
$path,
89+
$this->cleanUpNamespace($namespace . dirname('Path/' . $pathClassName)),
90+
$namespace,
91+
strrev(explode('/', strrev($pathClassName))[0]),
92+
$pathItem
93+
);
94+
7095
foreach ($pathItem->getOperations() as $method => $operation) {
7196
$operationClassName = $this->className((new Convert($operation->operationId))->fromTrain()->toPascal());
7297
$operations[$method] = $operationClassName;
7398
if (strlen($operationClassName) === 0) {
7499
continue;
75100
}
76-
@mkdir(dirname($destinationPath . '/Operation/' . $operationClassName), 0777, true);
77-
file_put_contents($destinationPath . '/Operation/' . $operationClassName . '.php', $codePrinter->prettyPrintFile([
78-
Operation::generate(
79-
$path,
80-
$method,
81-
$this->cleanUpNamespace($namespace . dirname('Operation/' . $operationClassName)),
82-
strrev(explode('/', strrev($operationClassName))[0]),
83-
$operation
84-
),
85-
]) . PHP_EOL);
101+
102+
yield from Operation::generate(
103+
$path,
104+
$method,
105+
$this->cleanUpNamespace($namespace . dirname('Operation/' . $operationClassName)),
106+
strrev(explode('/', strrev($operationClassName))[0]),
107+
$operation
108+
);
86109
}
87110
}
88111
}
89112
}
90-
91-
private function className(string $className): string
92-
{
93-
return str_replace(['{', '}', '-', '$'], ['Cb', 'Rcb', 'Dash', '_'], (new Convert($className))->toPascal());
94-
}
95-
96-
private function cleanUpNamespace(string $namespace): string
97-
{
98-
$namespace = str_replace('/', '\\', $namespace);
99-
$namespace = str_replace('\\\\', '\\', $namespace);
100-
101-
return $namespace;
102-
}
103113
}

src/Generator/Operation.php

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

33
namespace ApiClients\Tools\OpenApiClientGenerator\Generator;
44

5+
use ApiClients\Tools\OpenApiClientGenerator\File;
56
use cebe\openapi\spec\Operation as OpenAPiOperation;
67
use PhpParser\Builder\Param;
78
use PhpParser\BuilderFactory;
@@ -12,7 +13,15 @@
1213

1314
final class Operation
1415
{
15-
public static function generate(string $path, string $method, string $namespace, string $className, OpenAPiOperation $operation): Node
16+
/**
17+
* @param string $path
18+
* @param string $method
19+
* @param string $namespace
20+
* @param string $className
21+
* @param OpenAPiOperation $operation
22+
* @return iterable<Node>
23+
*/
24+
public static function generate(string $path, string $method, string $namespace, string $className, OpenAPiOperation $operation): iterable
1625
{
1726
$factory = new BuilderFactory();
1827
$stmt = $factory->namespace($namespace);
@@ -126,6 +135,6 @@ public static function generate(string $path, string $method, string $namespace,
126135
$factory->method('validateResponse')
127136
);
128137

129-
return $stmt->addStmt($class)->getNode();
138+
yield new File($namespace . '\\' . $className, $stmt->addStmt($class)->getNode());
130139
}
131140
}

src/Generator/Operations.php

Lines changed: 0 additions & 129 deletions
This file was deleted.

src/Generator/Path.php

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

33
namespace ApiClients\Tools\OpenApiClientGenerator\Generator;
44

5+
use ApiClients\Tools\OpenApiClientGenerator\File;
56
use cebe\openapi\spec\Operation as OpenAPiOperation;
67
use cebe\openapi\spec\PathItem;
78
use Jawira\CaseConverter\Convert;
@@ -14,7 +15,16 @@
1415

1516
final class Path
1617
{
17-
public static function generate(string $path, string $namespace, string $baseNamespace, string $className, PathItem $pathItem): Node
18+
/**
19+
* @param string $path
20+
* @param string $namespace
21+
* @param string $baseNamespace
22+
* @param string $className
23+
* @param PathItem $pathItem
24+
* @return iterable<Node>
25+
* @throws \Jawira\CaseConverter\CaseConverterException
26+
*/
27+
public static function generate(string $path, string $namespace, string $baseNamespace, string $className, PathItem $pathItem): iterable
1828
{
1929
$factory = new BuilderFactory();
2030
$stmt = $factory->namespace($namespace);
@@ -57,6 +67,6 @@ public static function generate(string $path, string $namespace, string $baseNam
5767
$class->addStmt($method);
5868
}
5969

60-
return $stmt->addStmt($class)->getNode();
70+
yield new File($namespace . '\\' . $className, $stmt->addStmt($class)->getNode());
6171
}
6272
}

src/Generator/Schema.php

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

33
namespace ApiClients\Tools\OpenApiClientGenerator\Generator;
44

5+
use ApiClients\Tools\OpenApiClientGenerator\File;
56
use cebe\openapi\spec\Schema as OpenAPiSchema;
67
use PhpParser\Builder\Param;
78
use PhpParser\BuilderFactory;
@@ -19,7 +20,7 @@ final class Schema
1920
* @param OpenAPiSchema $schema
2021
* @return iterable<Node>
2122
*/
22-
public static function generate(string $name, string $namespace, string $className, OpenAPiSchema $schema, array $schemaClassNameMap): Node
23+
public static function generate(string $name, string $namespace, string $className, OpenAPiSchema $schema, array $schemaClassNameMap): iterable
2324
{
2425
$factory = new BuilderFactory();
2526
$stmt = $factory->namespace($namespace);
@@ -121,6 +122,6 @@ public static function generate(string $name, string $namespace, string $classNa
121122
$class->addStmt($propertyStmt)->addStmt($method);
122123
}
123124

124-
return $stmt->addStmt($class)->getNode();
125+
yield new File($namespace . '\\' . $className, $stmt->addStmt($class)->getNode());
125126
}
126127
}

0 commit comments

Comments
 (0)