|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Leaditin\Code\Generator; |
|
4
|
|
|
|
|
5
|
|
|
use Leaditin\Code\DocBlock; |
|
6
|
|
|
use Leaditin\Code\Import; |
|
7
|
|
|
use Leaditin\Code\Member\Constant; |
|
8
|
|
|
use Leaditin\Code\Member\Method; |
|
9
|
|
|
use Leaditin\Code\Member\Property; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @package Leaditin\Code |
|
13
|
|
|
* @author Igor Vuckovic <[email protected]> |
|
14
|
|
|
* @license MIT |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class ClassAwareGenerator extends Generator |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var ConstantGenerator |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $constantGenerator; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var DocBlockGenerator |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $docBlockGenerator; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var ImportGenerator |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $importGenerator; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var MethodGenerator |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $methodGenerator; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var PropertyGenerator |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $propertyGenerator; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $name; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var null|string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $namespace; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var null|string |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $extends; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var null|DocBlock |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $docBlock; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var Constant[] |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $constants = []; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var Property[] |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $properties = []; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @var Method[] |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $methods = []; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @var Import[] |
|
80
|
|
|
*/ |
|
81
|
|
|
protected $imports = []; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param ConstantGenerator $constantGenerator |
|
85
|
|
|
* @param DocBlockGenerator $docBlockGenerator |
|
86
|
|
|
* @param ImportGenerator $importGenerator |
|
87
|
|
|
* @param MethodGenerator $methodGenerator |
|
88
|
|
|
* @param PropertyGenerator $propertyGenerator |
|
89
|
|
|
*/ |
|
90
|
25 |
|
public function __construct( |
|
91
|
|
|
ConstantGenerator $constantGenerator, |
|
92
|
|
|
DocBlockGenerator $docBlockGenerator, |
|
93
|
|
|
ImportGenerator $importGenerator, |
|
94
|
|
|
MethodGenerator $methodGenerator, |
|
95
|
|
|
PropertyGenerator $propertyGenerator |
|
96
|
|
|
) { |
|
97
|
25 |
|
$this->constantGenerator = $constantGenerator; |
|
98
|
25 |
|
$this->docBlockGenerator = $docBlockGenerator; |
|
99
|
25 |
|
$this->importGenerator = $importGenerator; |
|
100
|
25 |
|
$this->methodGenerator = $methodGenerator; |
|
101
|
25 |
|
$this->propertyGenerator = $propertyGenerator; |
|
102
|
25 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param string $name |
|
106
|
|
|
* |
|
107
|
|
|
* @return static |
|
108
|
|
|
*/ |
|
109
|
18 |
|
public function setName(string $name): self |
|
110
|
|
|
{ |
|
111
|
18 |
|
$this->name = $name; |
|
112
|
|
|
|
|
113
|
18 |
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param null|string $namespace |
|
118
|
|
|
* |
|
119
|
|
|
* @return static |
|
120
|
|
|
*/ |
|
121
|
9 |
|
public function setNamespace(?string $namespace): self |
|
122
|
|
|
{ |
|
123
|
9 |
|
$this->namespace = $namespace; |
|
124
|
|
|
|
|
125
|
9 |
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param null|string $extends |
|
130
|
|
|
* |
|
131
|
|
|
* @return static |
|
132
|
|
|
*/ |
|
133
|
9 |
|
public function setExtends(?string $extends): self |
|
134
|
|
|
{ |
|
135
|
9 |
|
$this->extends = $extends; |
|
136
|
|
|
|
|
137
|
9 |
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param null|DocBlock $docBlock |
|
142
|
|
|
* |
|
143
|
|
|
* @return static |
|
144
|
|
|
*/ |
|
145
|
4 |
|
public function setDocBlock(?DocBlock $docBlock): self |
|
146
|
|
|
{ |
|
147
|
4 |
|
$this->docBlock = $docBlock; |
|
148
|
|
|
|
|
149
|
4 |
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param Constant $constant |
|
154
|
|
|
* |
|
155
|
|
|
* @return static |
|
156
|
|
|
*/ |
|
157
|
6 |
|
public function addConstant(Constant $constant): self |
|
158
|
|
|
{ |
|
159
|
6 |
|
$this->constants[$constant->name()] = $constant; |
|
160
|
|
|
|
|
161
|
6 |
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param Property $property |
|
166
|
|
|
* |
|
167
|
|
|
* @return static |
|
168
|
|
|
*/ |
|
169
|
5 |
|
public function addProperty(Property $property): self |
|
170
|
|
|
{ |
|
171
|
5 |
|
$this->properties[$property->name()] = $property; |
|
172
|
|
|
|
|
173
|
5 |
|
return $this; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @param Method $method |
|
178
|
|
|
* |
|
179
|
|
|
* @return static |
|
180
|
|
|
*/ |
|
181
|
9 |
|
public function addMethod(Method $method): self |
|
182
|
|
|
{ |
|
183
|
9 |
|
$method->setScope($this->scope()); |
|
184
|
9 |
|
$this->methods[$method->name()] = $method; |
|
185
|
|
|
|
|
186
|
9 |
|
return $this; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
2 |
|
public function addImport(Import $import): self |
|
190
|
|
|
{ |
|
191
|
2 |
|
$this->imports[$import->fullyQualifiedClassName()] = $import; |
|
192
|
|
|
|
|
193
|
2 |
|
return $this; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @return string |
|
198
|
|
|
*/ |
|
199
|
17 |
|
public function generate(): string |
|
200
|
|
|
{ |
|
201
|
17 |
|
return $this->generateHead() |
|
202
|
17 |
|
. $this->generateConstants() |
|
203
|
17 |
|
. $this->generateProperties() |
|
204
|
17 |
|
. $this->generateMethods() |
|
205
|
17 |
|
. $this->generateFoot(); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @return string |
|
210
|
|
|
*/ |
|
211
|
|
|
abstract protected function scope(): string; |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @return string |
|
215
|
|
|
*/ |
|
216
|
|
|
abstract protected function generateScope(): string; |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @return string |
|
220
|
|
|
*/ |
|
221
|
17 |
|
protected function generateHead(): string |
|
222
|
|
|
{ |
|
223
|
17 |
|
$output = $this->generateLine('<?php', null, 1); |
|
224
|
17 |
|
$output .= $this->generateNamespace(); |
|
225
|
17 |
|
$output .= $this->generateImport(); |
|
226
|
17 |
|
$output .= $this->generateDocBlock(); |
|
227
|
17 |
|
$output .= $this->generateScope(); |
|
228
|
|
|
|
|
229
|
17 |
|
return $output; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* @return string |
|
234
|
|
|
*/ |
|
235
|
17 |
|
protected function generateNamespace(): string |
|
236
|
|
|
{ |
|
237
|
17 |
|
if ($this->namespace === null) { |
|
238
|
9 |
|
return ''; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
8 |
|
return $this->generateLine('namespace ' . trim($this->namespace) . ';', null, 1); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @return string |
|
246
|
|
|
*/ |
|
247
|
17 |
|
protected function generateImport(): string |
|
248
|
|
|
{ |
|
249
|
17 |
|
if (count($this->imports) === 0) { |
|
250
|
16 |
|
return ''; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
1 |
|
$output = ''; |
|
254
|
|
|
|
|
255
|
1 |
|
foreach ($this->imports as $import) { |
|
256
|
1 |
|
$output .= $this->generateLine($this->importGenerator->generate($import)); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
1 |
|
$output .= $this->generateLine(''); |
|
260
|
|
|
|
|
261
|
1 |
|
return $output; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @return string |
|
266
|
|
|
*/ |
|
267
|
17 |
|
protected function generateDocBlock(): string |
|
268
|
|
|
{ |
|
269
|
17 |
|
if ($this->docBlock === null) { |
|
270
|
14 |
|
return ''; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
3 |
|
return $this->generateLine($this->docBlockGenerator->generate($this->docBlock)); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* @return string |
|
278
|
|
|
*/ |
|
279
|
17 |
|
protected function generateInheritance(): string |
|
280
|
|
|
{ |
|
281
|
17 |
|
if ($this->extends === null) { |
|
282
|
9 |
|
return ''; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
8 |
|
return ' extends ' . trim($this->extends); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* @return string |
|
290
|
|
|
*/ |
|
291
|
17 |
|
protected function generateFoot(): string |
|
292
|
|
|
{ |
|
293
|
17 |
|
return $this->generateLine('}'); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* @return string |
|
298
|
|
|
*/ |
|
299
|
17 |
|
protected function generateConstants(): string |
|
300
|
|
|
{ |
|
301
|
17 |
|
$lines = []; |
|
302
|
|
|
|
|
303
|
17 |
|
foreach ($this->constants as $constant) { |
|
304
|
5 |
|
$lines[] = $this->generateLine($this->constantGenerator->generate($constant)); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
17 |
|
return implode('', $lines); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* @return string |
|
312
|
|
|
*/ |
|
313
|
13 |
|
protected function generateProperties(): string |
|
314
|
|
|
{ |
|
315
|
13 |
|
$output = ''; |
|
316
|
|
|
|
|
317
|
13 |
|
if ($this->constants !== [] && $this->properties !== []) { |
|
318
|
1 |
|
$output .= $this->generateLine('', 0); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
13 |
|
$lines = []; |
|
322
|
13 |
|
foreach ($this->properties as $property) { |
|
323
|
4 |
|
$lines[] = $this->generateLine($this->propertyGenerator->generate($property)); |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
13 |
|
$output .= implode(static::END_OF_LINE, $lines); |
|
327
|
|
|
|
|
328
|
13 |
|
return $output; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* @return string |
|
333
|
|
|
*/ |
|
334
|
17 |
|
protected function generateMethods(): string |
|
335
|
|
|
{ |
|
336
|
17 |
|
$output = ''; |
|
337
|
|
|
|
|
338
|
17 |
|
if ($this->methods !== [] && ($this->constants !== [] || $this->properties !== [])) { |
|
339
|
4 |
|
$output .= $this->generateLine('', 0); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
17 |
|
$lines = []; |
|
343
|
17 |
|
foreach ($this->methods as $method) { |
|
344
|
8 |
|
$lines[] = $this->generateLine($this->methodGenerator->generate($method)); |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
17 |
|
$output .= implode(static::END_OF_LINE, $lines); |
|
348
|
|
|
|
|
349
|
17 |
|
return $output; |
|
350
|
|
|
} |
|
351
|
|
|
} |
|
352
|
|
|
|