|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace ApiClients\Tools\ResourceGenerator; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use PhpParser\PrettyPrinter; |
|
7
|
|
|
use PhpParser\Node; |
|
8
|
|
|
use Symfony\CS\Config\Config; |
|
9
|
|
|
use Symfony\CS\ConfigAwareInterface; |
|
10
|
|
|
use Symfony\CS\ConfigInterface; |
|
11
|
|
|
use Symfony\CS\Fixer; |
|
12
|
|
|
use Symfony\CS\FixerInterface; |
|
13
|
|
|
|
|
14
|
|
|
class ResourceGenerator |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var callable |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $out = 'ApiClients\Tools\ResourceGenerator\outln'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $configuration; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var FileGeneratorInterface[] |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $generators = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $pathSrc; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $pathTests; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var Fixer |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $fixer; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $fixers; |
|
50
|
|
|
|
|
51
|
1 |
|
public function __construct(array $configuration, callable $out = null) |
|
52
|
|
|
{ |
|
53
|
1 |
|
$this->configuration = $configuration; |
|
54
|
1 |
|
$this->generators = $this->configuration['file_generators']; |
|
55
|
|
|
|
|
56
|
1 |
|
if (is_callable($out)) { |
|
57
|
1 |
|
$this->out = $out; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
$this->setUpFixers(); |
|
61
|
1 |
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
protected function setUpFixers() |
|
64
|
|
|
{ |
|
65
|
1 |
|
$this->fixer = new Fixer(); |
|
66
|
1 |
|
$this->fixer->registerCustomFixers([ |
|
67
|
1 |
|
new Fixer\Symfony\ExtraEmptyLinesFixer(), |
|
68
|
1 |
|
new Fixer\Symfony\SingleBlankLineBeforeNamespaceFixer(), |
|
69
|
1 |
|
new Fixer\PSR0\Psr0Fixer(), |
|
70
|
1 |
|
new Fixer\PSR1\EncodingFixer(), |
|
71
|
1 |
|
new Fixer\PSR1\ShortTagFixer(), |
|
72
|
1 |
|
new Fixer\PSR2\BracesFixer(), |
|
73
|
1 |
|
new Fixer\PSR2\ElseifFixer(), |
|
74
|
1 |
|
new Fixer\PSR2\EofEndingFixer(), |
|
75
|
1 |
|
new Fixer\PSR2\FunctionCallSpaceFixer(), |
|
76
|
1 |
|
new Fixer\PSR2\FunctionDeclarationFixer(), |
|
77
|
1 |
|
new Fixer\PSR2\IndentationFixer(), |
|
78
|
1 |
|
new Fixer\PSR2\LineAfterNamespaceFixer(), |
|
79
|
1 |
|
new Fixer\PSR2\LinefeedFixer(), |
|
80
|
1 |
|
new Fixer\PSR2\LowercaseConstantsFixer(), |
|
81
|
1 |
|
new Fixer\PSR2\LowercaseKeywordsFixer(), |
|
82
|
1 |
|
new Fixer\PSR2\MethodArgumentSpaceFixer(), |
|
83
|
1 |
|
new Fixer\PSR2\MultipleUseFixer(), |
|
84
|
1 |
|
new Fixer\PSR2\ParenthesisFixer(), |
|
85
|
1 |
|
new Fixer\PSR2\PhpClosingTagFixer(), |
|
86
|
1 |
|
new Fixer\PSR2\SingleLineAfterImportsFixer(), |
|
87
|
1 |
|
new Fixer\PSR2\TrailingSpacesFixer(), |
|
88
|
1 |
|
new Fixer\PSR2\VisibilityFixer(), |
|
89
|
1 |
|
new Fixer\Contrib\NewlineAfterOpenTagFixer(), |
|
90
|
1 |
|
new EmptyLineAboveDocblocksFixer(), |
|
91
|
|
|
]); |
|
92
|
1 |
|
$config = Config::create()-> |
|
93
|
1 |
|
fixers($this->fixer->getFixers()) |
|
94
|
|
|
; |
|
95
|
1 |
|
$this->fixer->addConfig($config); |
|
96
|
1 |
|
$this->fixers = $this->prepareFixers($config); |
|
97
|
1 |
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
public function run() |
|
100
|
|
|
{ |
|
101
|
1 |
|
foreach ($this->configuration['files'] as $definition) { |
|
102
|
1 |
|
$this->out('-----'); |
|
103
|
1 |
|
$this->out('- Definition: ' . $definition['class']); |
|
104
|
1 |
|
$definition = $this->applyAnnotationsToDefinition($definition); |
|
105
|
1 |
|
$this->generateFromDefinition($definition); |
|
106
|
1 |
|
$this->out('-----'); |
|
107
|
|
|
} |
|
108
|
1 |
|
} |
|
109
|
|
|
|
|
110
|
1 |
|
protected function applyAnnotationsToDefinition(array $definition): array |
|
111
|
|
|
{ |
|
112
|
1 |
|
foreach ($definition['properties'] as $property => $properties) { |
|
113
|
1 |
|
if (!isset($properties['annotations'])) { |
|
114
|
1 |
|
continue; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
1 |
|
foreach ($properties['annotations'] as $annotation => $input) { |
|
118
|
1 |
|
if (!isset($this->configuration['annotation_handlers'][$annotation])) { |
|
119
|
|
|
continue; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
1 |
|
if (!is_subclass_of( |
|
|
|
|
|
|
123
|
1 |
|
$this->configuration['annotation_handlers'][$annotation], |
|
124
|
1 |
|
AnnotationHandlerInterface::class |
|
125
|
|
|
)) { |
|
126
|
|
|
continue; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
1 |
|
$definition = forward_static_call_array( |
|
130
|
|
|
[ |
|
131
|
1 |
|
$this->configuration['annotation_handlers'][$annotation], |
|
132
|
1 |
|
'handle', |
|
133
|
|
|
], |
|
134
|
|
|
[ |
|
135
|
1 |
|
$property, |
|
136
|
1 |
|
$definition, |
|
137
|
1 |
|
$input, |
|
138
|
|
|
] |
|
139
|
|
|
); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
1 |
|
return $definition; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param array $file |
|
148
|
|
|
* @throws Exception |
|
149
|
|
|
*/ |
|
150
|
1 |
|
protected function generateFromDefinition(array $file) |
|
151
|
|
|
{ |
|
152
|
1 |
|
$config = $this->configuration + $file; |
|
153
|
1 |
|
unset($config['files']); |
|
154
|
|
|
|
|
155
|
1 |
|
foreach ($this->generators as $generatorClass) { |
|
156
|
|
|
/** @var FileGeneratorInterface $generator */ |
|
157
|
1 |
|
$generator = new $generatorClass($config); |
|
158
|
1 |
|
$fileName = $generator->getFilename(); |
|
159
|
1 |
|
$this->out('----'); |
|
160
|
1 |
|
$this->out('-- Generator: ' . $generatorClass); |
|
161
|
1 |
|
$this->out('-- File: ' . $fileName); |
|
162
|
1 |
|
$this->out('---'); |
|
163
|
1 |
|
$this->out('-- Generating'); |
|
164
|
1 |
|
$node = $generator->generate(); |
|
165
|
1 |
|
$this->out('-- Printing'); |
|
166
|
1 |
|
$code = $this->printCode($node); |
|
167
|
1 |
|
$this->out('-- Saving file'); |
|
168
|
1 |
|
$continue = $this->save($fileName, $code); |
|
169
|
1 |
|
if (!$continue) { |
|
170
|
|
|
continue; |
|
171
|
|
|
} |
|
172
|
1 |
|
$this->out('-- Applying code standards'); |
|
173
|
1 |
|
$this->applyPsr2($fileName); |
|
174
|
1 |
|
$this->out('----'); |
|
175
|
|
|
} |
|
176
|
1 |
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @param Node $node |
|
180
|
|
|
* @return string |
|
181
|
|
|
*/ |
|
182
|
1 |
|
protected function printCode(Node $node): string |
|
183
|
|
|
{ |
|
184
|
1 |
|
$prettyPrinter = new PrettyPrinter\Standard(); |
|
185
|
1 |
|
return $prettyPrinter->prettyPrintFile([ |
|
186
|
1 |
|
$node |
|
187
|
1 |
|
]) . PHP_EOL; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @param string $fileName |
|
192
|
|
|
* @param string $fileContents |
|
193
|
|
|
* @return bool |
|
194
|
|
|
* @throws Exception |
|
195
|
|
|
*/ |
|
196
|
1 |
|
protected function save(string $fileName, string $fileContents) |
|
197
|
|
|
{ |
|
198
|
1 |
|
$fileName = $this->configuration['root'] . $fileName; |
|
199
|
|
|
|
|
200
|
1 |
|
if (file_exists($fileName)) { |
|
201
|
|
|
$this->out('-- Exists'); |
|
202
|
|
|
return false; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
1 |
|
$directory = dirname($fileName); |
|
206
|
1 |
|
if (!file_exists($directory)) { |
|
207
|
1 |
|
mkdir($directory, 0755, true); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
1 |
|
if (!file_exists($directory)) { |
|
211
|
|
|
throw new Exception('Unable to create: ' . $directory); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
1 |
|
file_put_contents($fileName, $fileContents); |
|
215
|
|
|
|
|
216
|
|
|
do { |
|
217
|
1 |
|
usleep(500); |
|
218
|
1 |
|
} while (!file_exists($fileName)); |
|
219
|
|
|
|
|
220
|
1 |
|
return true; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @param string $fileName |
|
225
|
|
|
*/ |
|
226
|
1 |
|
protected function applyPsr2($fileName) |
|
227
|
|
|
{ |
|
228
|
1 |
|
$fileName = $this->configuration['root'] . $fileName; |
|
229
|
|
|
|
|
230
|
1 |
|
$file = new \SplFileInfo($fileName); |
|
231
|
1 |
|
$new = file_get_contents($file->getRealPath()); |
|
232
|
|
|
|
|
233
|
1 |
|
foreach ($this->fixers as $fixer) { |
|
234
|
1 |
|
if (!$fixer->supports($file)) { |
|
235
|
|
|
continue; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
1 |
|
$new = $fixer->fix($file, $new); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
1 |
|
file_put_contents( |
|
242
|
|
|
$fileName, |
|
243
|
|
|
str_replace( |
|
244
|
1 |
|
'<?php', |
|
245
|
1 |
|
'<?php declare(strict_types=1);', |
|
246
|
|
|
$new |
|
247
|
|
|
) |
|
248
|
|
|
); |
|
249
|
1 |
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* @param ConfigInterface $config |
|
253
|
|
|
* |
|
254
|
|
|
* @return FixerInterface[] |
|
255
|
|
|
*/ |
|
256
|
1 |
|
private function prepareFixers(ConfigInterface $config): array |
|
257
|
|
|
{ |
|
258
|
1 |
|
$fixers = $config->getFixers(); |
|
259
|
|
|
|
|
260
|
1 |
|
foreach ($fixers as $fixer) { |
|
261
|
1 |
|
if ($fixer instanceof ConfigAwareInterface) { |
|
262
|
1 |
|
$fixer->setConfig($config); |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
1 |
|
return $fixers; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
1 |
|
private function out(string $message) |
|
270
|
|
|
{ |
|
271
|
1 |
|
$out = $this->out; |
|
272
|
1 |
|
$out($message); |
|
273
|
1 |
|
} |
|
274
|
|
|
} |
|
275
|
|
|
|