1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace WyriHaximus\ApiClient\Tools; |
5
|
|
|
|
6
|
|
|
use Doctrine\Common\Inflector\Inflector; |
7
|
|
|
use Exception; |
8
|
|
|
use League\CLImate\CLImate; |
9
|
|
|
use PhpParser\Builder\Method; |
10
|
|
|
use PhpParser\Builder\Property; |
11
|
|
|
use PhpParser\BuilderFactory; |
12
|
|
|
use PhpParser\PrettyPrinter; |
13
|
|
|
use PhpParser\Node; |
14
|
|
|
use Symfony\Component\Yaml\Yaml; |
15
|
|
|
|
16
|
|
|
class ResourceGenerator |
17
|
|
|
{ |
18
|
|
|
protected $climate; |
19
|
|
|
|
20
|
2 |
|
public function __construct(CLImate $climate) |
21
|
|
|
{ |
22
|
2 |
|
$this->climate = $climate; |
23
|
|
|
|
24
|
2 |
|
$this->setUpArguments(); |
25
|
2 |
|
} |
26
|
|
|
|
27
|
2 |
|
protected function setUpArguments() |
28
|
|
|
{ |
29
|
2 |
|
$this->climate->arguments->add([ |
30
|
|
|
'definition' => [ |
31
|
|
|
'description' => 'YAML definition file', |
32
|
|
|
'required' => true, |
33
|
2 |
|
], |
34
|
|
|
'path' => [ |
35
|
|
|
'description' => 'Path to the resource directory', |
36
|
|
|
'required' => true, |
37
|
|
|
], |
38
|
|
|
'sync' => [ |
39
|
|
|
'prefix' => 's', |
40
|
|
|
'longPrefix' => 'sync', |
41
|
|
|
'defaultValue' => true, |
42
|
|
|
'noValue' => false, |
43
|
|
|
'description' => 'Don\'t generate Sync resource', |
44
|
|
|
'castTo' => 'bool', |
45
|
|
|
], |
46
|
|
|
'async' => [ |
47
|
|
|
'prefix' => 'as', |
48
|
|
|
'longPrefix' => 'async', |
49
|
|
|
'defaultValue' => true, |
50
|
|
|
'noValue' => false, |
51
|
|
|
'description' => 'Don\'t generate Async resource', |
52
|
|
|
'castTo' => 'bool', |
53
|
|
|
], |
54
|
|
|
]); |
55
|
2 |
|
} |
56
|
|
|
|
57
|
1 |
|
public function run() |
58
|
|
|
{ |
59
|
1 |
|
$yaml = $this->readYaml($this->climate->arguments->get('definition')); |
60
|
1 |
|
$this->save( |
61
|
1 |
|
$this->climate->arguments->get('path') . |
62
|
1 |
|
DIRECTORY_SEPARATOR, |
63
|
1 |
|
$yaml['class'] . |
64
|
1 |
|
'.php', |
65
|
1 |
|
$this->createBaseClass($yaml) |
66
|
|
|
); |
67
|
1 |
|
$this->save( |
68
|
1 |
|
$this->climate->arguments->get('path') . |
69
|
1 |
|
DIRECTORY_SEPARATOR, |
70
|
1 |
|
$yaml['class'] . |
71
|
1 |
|
'Interface.php', |
72
|
1 |
|
$this->createInterface($yaml) |
73
|
|
|
); |
74
|
1 |
|
$this->save( |
75
|
1 |
|
$this->climate->arguments->get('path') . |
76
|
1 |
|
DIRECTORY_SEPARATOR . |
77
|
1 |
|
'Async' . |
78
|
1 |
|
DIRECTORY_SEPARATOR, |
79
|
1 |
|
$yaml['class'] . |
80
|
1 |
|
'.php', |
81
|
1 |
|
$this->createExtendingClass('Async', $yaml) |
82
|
|
|
); |
83
|
1 |
|
$this->save( |
84
|
1 |
|
$this->climate->arguments->get('path') . |
85
|
1 |
|
DIRECTORY_SEPARATOR . |
86
|
1 |
|
'Sync' . |
87
|
1 |
|
DIRECTORY_SEPARATOR, |
88
|
1 |
|
$yaml['class'] . |
89
|
1 |
|
'.php', |
90
|
1 |
|
$this->createExtendingClass('Sync', $yaml) |
91
|
|
|
); |
92
|
1 |
|
} |
93
|
|
|
|
94
|
1 |
|
protected function readYaml(string $filename): array |
95
|
|
|
{ |
96
|
1 |
|
return Yaml::parse(file_get_contents($filename)); |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
protected function createBaseClass(array $yaml) |
100
|
|
|
{ |
101
|
1 |
|
$factory = new BuilderFactory; |
102
|
|
|
|
103
|
1 |
|
$class = $factory->class($yaml['class']) |
104
|
1 |
|
->implement($yaml['class'] . 'Interface') |
105
|
1 |
|
->makeAbstract(); |
106
|
1 |
|
$class->addStmt( |
107
|
1 |
|
new Node\Stmt\TraitUse([ |
108
|
1 |
|
new Node\Name('TransportAwareTrait') |
109
|
|
|
]) |
110
|
|
|
); |
111
|
|
|
|
112
|
1 |
|
foreach ($yaml['properties'] as $name => $details) { |
113
|
1 |
|
$type = $details; |
114
|
1 |
|
if (is_array($details)) { |
115
|
1 |
|
$type = $details['type']; |
116
|
|
|
} |
117
|
1 |
|
$class->addStmt($this->createProperty($factory, $type, $name, $details)); |
118
|
1 |
|
$class->addStmt($this->createMethod($factory, $type, $name, $details)); |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
$node = $factory->namespace($yaml['namespace']) |
122
|
1 |
|
->addStmt($factory->use('WyriHaximus\ApiClient\Resource\TransportAwareTrait')) |
123
|
1 |
|
->addStmt($class) |
124
|
|
|
|
125
|
1 |
|
->getNode() |
126
|
|
|
; |
127
|
|
|
|
128
|
1 |
|
$prettyPrinter = new PrettyPrinter\Standard(); |
129
|
1 |
|
return $prettyPrinter->prettyPrintFile([ |
130
|
1 |
|
$node |
131
|
1 |
|
]) . PHP_EOL; |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
protected function createInterface(array $yaml) |
135
|
|
|
{ |
136
|
1 |
|
$factory = new BuilderFactory; |
137
|
|
|
|
138
|
1 |
|
$class = $factory->interface($yaml['class'] . 'Interface') |
139
|
1 |
|
->extend('ResourceInterface'); |
140
|
|
|
|
141
|
1 |
|
foreach ($yaml['properties'] as $name => $details) { |
142
|
1 |
|
$type = $details; |
143
|
1 |
|
if (is_array($details)) { |
144
|
1 |
|
$type = $details['type']; |
145
|
|
|
} |
146
|
1 |
|
$class->addStmt($this->createMethod($factory, $type, $name, $details)); |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
$node = $factory->namespace($yaml['namespace']) |
150
|
1 |
|
->addStmt($factory->use('WyriHaximus\ApiClient\Resource\ResourceInterface')) |
151
|
1 |
|
->addStmt($class) |
152
|
1 |
|
->getNode() |
153
|
|
|
; |
154
|
|
|
|
155
|
1 |
|
$prettyPrinter = new PrettyPrinter\Standard(); |
156
|
1 |
|
return $prettyPrinter->prettyPrintFile([ |
157
|
1 |
|
$node |
158
|
1 |
|
]) . PHP_EOL; |
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
protected function createProperty(BuilderFactory $factory, string $type, string $name, $details): Property |
162
|
|
|
{ |
163
|
1 |
|
$property = $factory->property($name) |
164
|
1 |
|
->makeProtected() |
165
|
1 |
|
->setDocComment('/** |
166
|
1 |
|
* @var ' . $type . ' |
167
|
1 |
|
*/'); |
168
|
1 |
|
if (isset($details['default'])) { |
169
|
1 |
|
$property->setDefault($details['default']); |
170
|
|
|
} |
171
|
|
|
|
172
|
1 |
|
return $property; |
173
|
|
|
} |
174
|
|
|
|
175
|
1 |
|
protected function createMethod(BuilderFactory $factory, string $type, string $name, $details): Method |
|
|
|
|
176
|
|
|
{ |
177
|
1 |
|
return $factory->method(Inflector::camelize($name)) |
178
|
1 |
|
->makePublic() |
179
|
1 |
|
->setReturnType($type) |
180
|
1 |
|
->setDocComment('/** |
181
|
1 |
|
* @return ' . $type . ' |
182
|
1 |
|
*/') |
183
|
1 |
|
->addStmt( |
184
|
1 |
|
new Node\Stmt\Return_( |
185
|
1 |
|
new Node\Expr\PropertyFetch( |
186
|
1 |
|
new Node\Expr\Variable('this'), |
187
|
|
|
$name |
188
|
|
|
) |
189
|
|
|
) |
190
|
|
|
); |
191
|
|
|
} |
192
|
|
|
|
193
|
1 |
|
protected function createExtendingClass(string $type, array $yaml) |
194
|
|
|
{ |
195
|
1 |
|
$factory = new BuilderFactory; |
196
|
|
|
|
197
|
1 |
|
$class = $factory->class($yaml['class']) |
198
|
1 |
|
->extend('Base' . $yaml['class']); |
199
|
|
|
|
200
|
1 |
|
$class->addStmt($factory->method('refresh') |
201
|
1 |
|
->makePublic() |
202
|
1 |
|
->setReturnType($yaml['class']) |
203
|
1 |
|
->addStmt( |
204
|
1 |
|
new Node\Stmt\Return_( |
205
|
1 |
|
new Node\Expr\MethodCall( |
206
|
1 |
|
new Node\Expr\Variable('this'), |
207
|
1 |
|
'wait', |
208
|
|
|
[ |
|
|
|
|
209
|
1 |
|
new Node\Expr\MethodCall( |
210
|
1 |
|
new Node\Expr\Variable('this'), |
211
|
1 |
|
'callAsync', |
212
|
|
|
[ |
|
|
|
|
213
|
1 |
|
new Node\Scalar\String_('refresh'), |
214
|
|
|
] |
215
|
|
|
), |
216
|
|
|
] |
217
|
|
|
) |
218
|
|
|
) |
219
|
|
|
)); |
220
|
|
|
|
221
|
1 |
|
$node = $factory->namespace($yaml['namespace'] . '\\' . $type) |
222
|
1 |
|
->addStmt($factory->use($yaml['namespace'] . '\\' . $yaml['class'])->as('Base' . $yaml['class'])) |
223
|
1 |
|
->addStmt($class) |
224
|
|
|
|
225
|
1 |
|
->getNode() |
226
|
|
|
; |
227
|
|
|
|
228
|
1 |
|
$prettyPrinter = new PrettyPrinter\Standard(); |
229
|
1 |
|
return $prettyPrinter->prettyPrintFile([ |
230
|
1 |
|
$node |
231
|
1 |
|
]) . PHP_EOL; |
232
|
|
|
} |
233
|
|
|
|
234
|
1 |
|
protected function save(string $directory, string $fileName, string $fileContents) |
235
|
|
|
{ |
236
|
1 |
|
if (file_exists($directory . $fileName)) { |
237
|
|
|
return; |
238
|
|
|
} |
239
|
|
|
|
240
|
1 |
|
if (!file_exists($directory)) { |
241
|
1 |
|
mkdir($directory, 0777, true); |
242
|
|
|
} |
243
|
|
|
|
244
|
1 |
|
if (!file_exists($directory)) { |
245
|
|
|
throw new Exception('Unable to create: ' . $directory); |
246
|
|
|
} |
247
|
|
|
|
248
|
1 |
|
file_put_contents($directory . $fileName, $fileContents); |
249
|
1 |
|
} |
250
|
|
|
} |
251
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.