-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGenerator.php
More file actions
103 lines (94 loc) · 4.4 KB
/
Copy pathGenerator.php
File metadata and controls
103 lines (94 loc) · 4.4 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
<?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();
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;
}
@mkdir(dirname($destinationPath . '/Schema/' . $schemaClassName), 0777, true);
file_put_contents($destinationPath . '/Schema/' . $schemaClassName . '.php', $codePrinter->prettyPrintFile([
Schema::generate(
$name,
$this->cleanUpNamespace($namespace . dirname('Schema/' . $schemaClassName)),
strrev(explode('/', strrev($schemaClassName))[0]),
$schema,
$schemaClassNameMap
),
]) . PHP_EOL);
}
}
if (count($this->spec->paths) > 0) {
foreach ($this->spec->paths as $path => $pathItem) {
$pathClassName = $this->className($path);
if (strlen($pathClassName) === 0) {
continue;
}
@mkdir(dirname($destinationPath . '/Path/' . $pathClassName), 0777, true);
file_put_contents($destinationPath . '/Path/' . $pathClassName . '.php', $codePrinter->prettyPrintFile([
Path::generate(
$path,
$this->cleanUpNamespace($namespace . dirname('Path/' . $pathClassName)),
$namespace,
strrev(explode('/', strrev($pathClassName))[0]),
$pathItem
),
]) . PHP_EOL);
foreach ($pathItem->getOperations() as $method => $operation) {
$operationClassName = $this->className((new Convert($operation->operationId))->fromTrain()->toPascal());
$operations[$method] = $operationClassName;
if (strlen($operationClassName) === 0) {
continue;
}
@mkdir(dirname($destinationPath . '/Operation/' . $operationClassName), 0777, true);
file_put_contents($destinationPath . '/Operation/' . $operationClassName . '.php', $codePrinter->prettyPrintFile([
Operation::generate(
$path,
$method,
$this->cleanUpNamespace($namespace . dirname('Operation/' . $operationClassName)),
strrev(explode('/', strrev($operationClassName))[0]),
$operation
),
]) . 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;
}
}