-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGenerator.php
More file actions
128 lines (109 loc) · 4.42 KB
/
Copy pathGenerator.php
File metadata and controls
128 lines (109 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
namespace ApiClients\Tools\OpenApiClientGenerator;
use ApiClients\Tools\OpenApiClientGenerator\Generator\Operation;
use ApiClients\Tools\OpenApiClientGenerator\Generator\Path;
use ApiClients\Tools\OpenApiClientGenerator\Generator\Schema;
use cebe\openapi\Reader;
use cebe\openapi\spec\OpenApi;
use Jawira\CaseConverter\Convert;
use PhpParser\PrettyPrinter\Standard;
final class Generator
{
private OpenApi $spec;
public function __construct(string $specUrl)
{
/** @var OpenApi spec */
$this->spec = Reader::readFromYamlFile($specUrl);
}
public function generate(string $namespace, string $destinationPath)
{
$namespace = $this->cleanUpNamespace($namespace);
$codePrinter = new Standard();
foreach ($this->all($namespace) as $file) {
$fileName = $destinationPath . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, substr($file->fqcn(), strlen($namespace)));
@mkdir(dirname($fileName), 0744, true);
file_put_contents($fileName . '.php', $codePrinter->prettyPrintFile([$file->contents()]) . PHP_EOL);
}
}
private function className(string $className): string
{
return str_replace(['{', '}', '-', '$', '_'], ['Cb', 'Rcb', 'Dash', '_', '\\'], (new Convert($className))->toPascal());
}
private function cleanUpNamespace(string $namespace): string
{
$namespace = str_replace('/', '\\', $namespace);
$namespace = str_replace('\\\\', '\\', $namespace);
return $namespace;
}
/**
* @param string $namespace
* @param string $destinationPath
* @return iterable<File>
*/
private function all(string $namespace): iterable
{
if (count($this->spec->components->schemas ?? []) > 0) {
$schemaClassNameMap = [];
foreach ($this->spec->components->schemas as $name => $schema) {
$schemaClassName = $this->className($name);
if (strlen($schemaClassName) === 0) {
continue;
}
$schemaClassNameMap[spl_object_hash($schema)] = $schemaClassName;
}
foreach ($this->spec->components->schemas as $name => $schema) {
$schemaClassName = $schemaClassNameMap[spl_object_hash($schema)];
if (strlen($schemaClassName) === 0) {
continue;
}
yield from Schema::generate(
$name,
$this->dirname($namespace . 'Schema/' . $schemaClassName),
$this->basename($namespace . 'Schema/' . $schemaClassName),
$schema,
$schemaClassNameMap,
$namespace . 'Schema'
);
}
}
if (count($this->spec->paths ?? []) > 0) {
foreach ($this->spec->paths as $path => $pathItem) {
$pathClassName = $this->className($path);
if (strlen($pathClassName) === 0) {
continue;
}
yield from Path::generate(
$path,
$this->dirname($namespace . 'Path/' . $pathClassName),
$namespace,
$this->basename($namespace . 'Path/' . $pathClassName),
$pathItem
);
foreach ($pathItem->getOperations() as $method => $operation) {
$operationClassName = $this->className((new Convert($operation->operationId))->fromTrain()->toPascal());
$operations[$method] = $operationClassName;
if (strlen($operationClassName) === 0) {
continue;
}
yield from Operation::generate(
$path,
$method,
$this->dirname($namespace . 'Operation/' . $operationClassName),
$this->basename($namespace . 'Operation/' . $operationClassName),
$operation
);
}
}
}
}
private function dirname(string $fqcn): string
{
$fqcn = str_replace('\\', '/', $fqcn);
return $this->cleanUpNamespace(dirname($fqcn));
}
private function basename(string $fqcn): string
{
$fqcn = str_replace('\\', '/', $fqcn);
return $this->cleanUpNamespace(basename($fqcn));
}
}