|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace ApiClients\Tools\ResourceGenerator; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use PhpParser\Node; |
|
7
|
|
|
use PhpParser\PrettyPrinter; |
|
8
|
|
|
|
|
9
|
|
|
class ResourceGenerator |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var callable |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $out = 'ApiClients\Tools\ResourceGenerator\outln'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $configuration; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var FileGeneratorInterface[] |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $generators = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $pathSrc; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $pathTests; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var Fixer |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $fixer; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $fixers; |
|
45
|
|
|
|
|
46
|
1 |
|
public function __construct(array $configuration, callable $out = null) |
|
47
|
|
|
{ |
|
48
|
1 |
|
$this->configuration = $configuration; |
|
49
|
1 |
|
$this->generators = $this->configuration['file_generators']; |
|
50
|
|
|
|
|
51
|
1 |
|
if (is_callable($out)) { |
|
52
|
1 |
|
$this->out = $out; |
|
53
|
|
|
} |
|
54
|
1 |
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
public function run() |
|
57
|
|
|
{ |
|
58
|
1 |
|
foreach ($this->configuration['files'] as $definition) { |
|
59
|
1 |
|
$this->out('-----'); |
|
60
|
1 |
|
$this->out('- Definition: ' . $definition['class']); |
|
61
|
1 |
|
$definition = $this->applyAnnotationsToDefinition($definition); |
|
62
|
1 |
|
$this->generateFromDefinition($definition); |
|
63
|
1 |
|
$this->out('-----'); |
|
64
|
|
|
} |
|
65
|
1 |
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
protected function applyAnnotationsToDefinition(array $definition): array |
|
68
|
|
|
{ |
|
69
|
1 |
|
foreach ($definition['properties'] as $property => $properties) { |
|
70
|
1 |
|
if (!isset($properties['annotations'])) { |
|
71
|
1 |
|
continue; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
foreach ($properties['annotations'] as $annotation => $input) { |
|
75
|
1 |
|
if (!isset($this->configuration['annotation_handlers'][$annotation])) { |
|
76
|
|
|
continue; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
if (!is_subclass_of( |
|
|
|
|
|
|
80
|
1 |
|
$this->configuration['annotation_handlers'][$annotation], |
|
81
|
1 |
|
AnnotationHandlerInterface::class |
|
82
|
|
|
)) { |
|
83
|
|
|
continue; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
$definition = forward_static_call_array( |
|
87
|
|
|
[ |
|
88
|
1 |
|
$this->configuration['annotation_handlers'][$annotation], |
|
89
|
1 |
|
'handle', |
|
90
|
|
|
], |
|
91
|
|
|
[ |
|
92
|
1 |
|
$property, |
|
93
|
1 |
|
$definition, |
|
94
|
1 |
|
$input, |
|
95
|
|
|
] |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
return $definition; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param array $file |
|
105
|
|
|
* @throws Exception |
|
106
|
|
|
*/ |
|
107
|
1 |
|
protected function generateFromDefinition(array $file) |
|
108
|
|
|
{ |
|
109
|
1 |
|
$config = $this->configuration + $file; |
|
110
|
1 |
|
unset($config['files']); |
|
111
|
|
|
|
|
112
|
1 |
|
foreach ($this->generators as $generatorClass) { |
|
113
|
|
|
/** @var FileGeneratorInterface $generator */ |
|
114
|
1 |
|
$generator = new $generatorClass($config); |
|
115
|
1 |
|
$fileName = $generator->getFilename(); |
|
116
|
1 |
|
$this->out('----'); |
|
117
|
1 |
|
$this->out('-- Generator: ' . $generatorClass); |
|
118
|
1 |
|
$this->out('-- File: ' . $fileName); |
|
119
|
1 |
|
$this->out('---'); |
|
120
|
1 |
|
$this->out('-- Generating'); |
|
121
|
1 |
|
$node = $generator->generate(); |
|
122
|
1 |
|
$this->out('-- Printing'); |
|
123
|
1 |
|
$code = $this->printCode($node); |
|
124
|
1 |
|
$this->out('-- Saving file'); |
|
125
|
1 |
|
$continue = $this->save($fileName, $code); |
|
126
|
1 |
|
if (!$continue) { |
|
127
|
|
|
continue; |
|
128
|
|
|
} |
|
129
|
1 |
|
$this->out('-- Applying code standards'); |
|
130
|
1 |
|
$this->applyPsr2($fileName); |
|
131
|
1 |
|
$this->out('----'); |
|
132
|
|
|
} |
|
133
|
1 |
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param Node $node |
|
137
|
|
|
* @return string |
|
138
|
|
|
*/ |
|
139
|
1 |
|
protected function printCode(Node $node): string |
|
140
|
|
|
{ |
|
141
|
1 |
|
$prettyPrinter = new PrettyPrinter\Standard(); |
|
142
|
|
|
|
|
143
|
1 |
|
return $prettyPrinter->prettyPrintFile([ |
|
144
|
1 |
|
$node, |
|
145
|
1 |
|
]) . PHP_EOL; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param string $fileName |
|
150
|
|
|
* @param string $fileContents |
|
151
|
|
|
* @throws Exception |
|
152
|
|
|
* @return bool |
|
153
|
|
|
*/ |
|
154
|
1 |
|
protected function save(string $fileName, string $fileContents) |
|
155
|
|
|
{ |
|
156
|
1 |
|
$fileName = $this->configuration['root'] . $fileName; |
|
157
|
|
|
|
|
158
|
1 |
|
if (file_exists($fileName)) { |
|
159
|
|
|
$this->out('-- Exists'); |
|
160
|
|
|
|
|
161
|
|
|
return false; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
1 |
|
$directory = dirname($fileName); |
|
165
|
1 |
|
if (!file_exists($directory)) { |
|
166
|
1 |
|
mkdir($directory, 0755, true); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
1 |
|
if (!file_exists($directory)) { |
|
170
|
|
|
throw new Exception('Unable to create: ' . $directory); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
1 |
|
file_put_contents($fileName, $fileContents); |
|
174
|
|
|
|
|
175
|
|
|
do { |
|
176
|
1 |
|
usleep(500); |
|
177
|
1 |
|
} while (!file_exists($fileName)); |
|
178
|
|
|
|
|
179
|
1 |
|
return true; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param string $fileName |
|
184
|
|
|
*/ |
|
185
|
1 |
|
protected function applyPsr2($fileName) |
|
186
|
|
|
{ |
|
187
|
1 |
|
$fileName = $this->configuration['root'] . $fileName; |
|
188
|
|
|
|
|
189
|
|
|
$command = 'PHP_CS_FIXER_IGNORE_ENV=1 vendor/bin/php-cs-fixer fix ' . |
|
190
|
1 |
|
$fileName . |
|
191
|
1 |
|
' --config=' . |
|
192
|
1 |
|
dirname(__DIR__) . |
|
193
|
1 |
|
DIRECTORY_SEPARATOR . |
|
194
|
1 |
|
'.php_cs ' . |
|
195
|
1 |
|
' --allow-risky=yes -q -v --stop-on-violation --using-cache=no' . |
|
196
|
1 |
|
' 2>&1'; |
|
197
|
|
|
|
|
198
|
1 |
|
exec($command); |
|
199
|
1 |
|
} |
|
200
|
|
|
|
|
201
|
1 |
|
private function out(string $message) |
|
202
|
|
|
{ |
|
203
|
1 |
|
$out = $this->out; |
|
204
|
1 |
|
$out($message); |
|
205
|
1 |
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|