|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace RoundingWell\Schematic; |
|
5
|
|
|
|
|
6
|
|
|
use PhpParser\BuilderFactory; |
|
7
|
|
|
use PhpParser\Node\Name; |
|
8
|
|
|
use PhpParser\Node\Stmt\Class_ as Cls; |
|
9
|
|
|
use PhpParser\Node\Stmt\Namespace_ as Ns; |
|
10
|
|
|
use PhpParser\PrettyPrinterAbstract; |
|
11
|
|
|
use PhpParser\PrettyPrinter\Standard as StandardPrinter; |
|
12
|
|
|
use RoundingWell\Schematic\Schema\ObjectSchema; |
|
13
|
|
|
|
|
14
|
|
|
class Generator |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var BuilderFactory |
|
18
|
|
|
*/ |
|
19
|
|
|
private $builder; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var PrettyPrinterAbstract |
|
23
|
|
|
*/ |
|
24
|
|
|
private $printer; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var System |
|
28
|
|
|
*/ |
|
29
|
|
|
private $system; |
|
30
|
|
|
|
|
31
|
2 |
|
public function __construct( |
|
32
|
|
|
BuilderFactory $factory = null, |
|
33
|
|
|
PrettyPrinterAbstract $printer = null, |
|
34
|
|
|
System $system = null |
|
35
|
|
|
) { |
|
36
|
2 |
|
$this->factory = $factory ?: $this->defaultBuilder(); |
|
|
|
|
|
|
37
|
2 |
|
$this->printer = $printer ?: $this->defaultPrinter(); |
|
38
|
2 |
|
$this->system = $system ?: $this->defaultSystem(); |
|
39
|
2 |
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
public function generate(ObjectSchema $schema, string $className, string $baseClass = ''): array |
|
42
|
|
|
{ |
|
43
|
1 |
|
$classes = []; |
|
44
|
|
|
|
|
45
|
1 |
|
$className = new Name($className); |
|
46
|
|
|
// Start the class AST definition |
|
47
|
1 |
|
$namespace = $this->factory->namespace($className->slice(0, -1)->toString()); |
|
48
|
1 |
|
$class = $this->factory->class($className->getLast()); |
|
49
|
|
|
|
|
50
|
1 |
|
if ($baseClass) { |
|
51
|
1 |
|
$baseClassName = new Name($baseClass); |
|
52
|
|
|
// Import the base class with a "use" statement |
|
53
|
1 |
|
$namespace->addStmt($this->factory->use($baseClassName)); |
|
54
|
|
|
// Make the class extend the base class |
|
55
|
1 |
|
$class->extend($baseClassName->getLast()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
foreach ($schema->properties() as $name => $property) { |
|
59
|
1 |
|
$typeHint = $property->phpType(); |
|
60
|
|
|
|
|
61
|
1 |
|
if ($property->isObject()) { |
|
62
|
|
|
// Create a new class for this property |
|
63
|
1 |
|
$nextClass = Name::concat($className, ucfirst($name)); |
|
64
|
1 |
|
$typeHint = '\\' . $nextClass->toString(); |
|
65
|
1 |
|
$classes = array_merge($classes, $this->generate( |
|
66
|
1 |
|
$property, |
|
|
|
|
|
|
67
|
1 |
|
$nextClass->toString(), |
|
68
|
1 |
|
$baseClass |
|
69
|
|
|
)); |
|
70
|
1 |
|
} elseif ($property->isArray() && $property->items()->isObject()) { |
|
|
|
|
|
|
71
|
|
|
// Create a new class for this array of properties |
|
72
|
1 |
|
$nextClass = Name::concat($className, ucfirst(singular($name))); |
|
73
|
1 |
|
$typeHint = '\\' . $nextClass->toString() . '[]'; |
|
74
|
1 |
|
$classes = array_merge($classes, $this->generate( |
|
75
|
1 |
|
$property->items(), |
|
|
|
|
|
|
76
|
1 |
|
$nextClass->toString(), |
|
77
|
1 |
|
$baseClass |
|
78
|
|
|
)); |
|
79
|
1 |
|
} elseif (!$schema->isRequired($name) && !$property->isNull()) { |
|
80
|
1 |
|
$typeHint = "$typeHint|null"; |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// Add a property declaration to the class |
|
84
|
1 |
|
$class->addStmt( |
|
85
|
1 |
|
$this->factory->property($name) |
|
86
|
1 |
|
->makePublic() |
|
87
|
1 |
|
->setDocComment("/**\n * @var $typeHint\n */") |
|
|
|
|
|
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// Add the class declaration to the namespace |
|
92
|
1 |
|
$namespace->addStmt($class); |
|
93
|
|
|
|
|
94
|
1 |
|
$classes[$className->toString()] = $namespace->getNode(); |
|
95
|
|
|
|
|
96
|
1 |
|
return $classes; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param Ns[] $classes |
|
101
|
|
|
* @return string[] |
|
102
|
|
|
*/ |
|
103
|
1 |
|
public function write(array $classes, string $rootDirectory, string $rootNamespace = ''): array |
|
104
|
|
|
{ |
|
105
|
1 |
|
$rootDirectory = rtrim($rootDirectory, '/'); |
|
106
|
|
|
|
|
107
|
1 |
|
return array_map( |
|
108
|
1 |
|
function (Ns $node) use ($rootDirectory, $rootNamespace): string { |
|
109
|
|
|
// Remove the root (PSR-4) namespace and convert to a path |
|
110
|
1 |
|
$directory = str_replace($rootNamespace, '', $node->name->toString()); |
|
111
|
1 |
|
$directory = trim(str_replace('\\', '/', $directory), '/'); |
|
112
|
1 |
|
$directory = rtrim("$rootDirectory/$directory", '/'); |
|
|
|
|
|
|
113
|
|
|
// Grab the class name from AST |
|
114
|
1 |
|
$class = $this->classNode($node->stmts)->name; |
|
115
|
|
|
|
|
116
|
1 |
|
$path = "$directory/$class.php"; |
|
|
|
|
|
|
117
|
1 |
|
$code = $this->printer->prettyPrintFile([$node]); |
|
118
|
|
|
|
|
119
|
1 |
|
$this->system->writeFile($path, $code); |
|
120
|
|
|
|
|
121
|
1 |
|
return $path; |
|
122
|
1 |
|
}, |
|
123
|
1 |
|
$classes |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
2 |
|
protected function defaultBuilder(): BuilderFactory |
|
128
|
|
|
{ |
|
129
|
2 |
|
return new BuilderFactory(); |
|
130
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
2 |
|
protected function defaultPrinter(): PrettyPrinterAbstract |
|
134
|
|
|
{ |
|
135
|
2 |
|
return new StandardPrinter([ |
|
136
|
2 |
|
'shortArraySyntax' => true, |
|
137
|
|
|
]); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
1 |
|
protected function defaultSystem(): System |
|
141
|
|
|
{ |
|
142
|
1 |
|
return new System(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
1 |
|
private function classNode(array $stmts): Cls |
|
146
|
|
|
{ |
|
147
|
1 |
|
foreach ($stmts as $stmt) { |
|
148
|
1 |
|
if ($stmt instanceof Cls) { |
|
149
|
1 |
|
return $stmt; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
// @codeCoverageIgnoreStart |
|
154
|
|
|
throw new \InvalidArgumentException( |
|
155
|
|
|
'Cannot find class node in statements' |
|
156
|
|
|
); |
|
157
|
|
|
// @codeCoverageIgnoreEnd |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: