|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yoghi\Bundle\MaddaBundle\Generator; |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the MADDA project. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Stefano Tamagnini <> |
|
9
|
|
|
* |
|
10
|
|
|
* This source file is subject to the GPLv3 license that is bundled |
|
11
|
|
|
* with this source code in the file LICENSE. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
use League\Flysystem\Adapter\Local; |
|
15
|
|
|
use Nette\PhpGenerator\Method; |
|
16
|
|
|
use Nette\PhpGenerator\PhpFile; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Stefano Tamagnini <> |
|
20
|
|
|
*/ |
|
21
|
|
|
class ClassGenerator extends AbstractFileGenerator |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* [$currentClass description]. |
|
25
|
|
|
* |
|
26
|
|
|
* @var \Nette\PhpGenerator\ClassType |
|
27
|
|
|
*/ |
|
28
|
|
|
private $currentClass; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct($namespace, $className, $document = 'Generated Class') |
|
31
|
|
|
{ |
|
32
|
|
|
$this->currentFile = new PhpFile(); |
|
33
|
|
|
$this->currentClass = $this->currentFile->addClass($namespace.'\\'.ucfirst($className)); |
|
34
|
|
|
$this->currentClass->addComment($document); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Aggiungere il costruttore. |
|
39
|
|
|
* |
|
40
|
|
|
* @return Nette\PhpGenerator\Method |
|
41
|
|
|
*/ |
|
42
|
|
|
private function addConstructor() |
|
43
|
|
|
{ |
|
44
|
|
|
$this->info('Aggiungo costruttore', ['class' => $this->currentClass->getName()]); |
|
45
|
|
|
$mc = $this->currentClass->addMethod('__construct'); |
|
|
|
|
|
|
46
|
|
|
$mc->setStatic(false); |
|
47
|
|
|
$mc->setVisibility('public'); |
|
48
|
|
|
$mc->addComment('costruttore'); |
|
49
|
|
|
$mc->setFinal(true); |
|
50
|
|
|
|
|
51
|
|
|
return $mc; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $comment |
|
56
|
|
|
* @param boolean $createGetter |
|
57
|
|
|
*/ |
|
58
|
|
|
private function addSingleton($comment, $createGetter) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->info('Aggiungo supporto singleton', [ |
|
61
|
|
|
'class' => $this->currentClass->getName(), |
|
62
|
|
|
]); |
|
63
|
|
|
$fullClassName = $this->currentClass->getNamespace()->getName().'\\'.$this->currentClass->getName(); |
|
64
|
|
|
if ($createGetter) { |
|
65
|
|
|
$mSingleton = $this->currentClass->addMethod('getInstance'); |
|
66
|
|
|
$mSingleton->setStatic(true); |
|
67
|
|
|
$mSingleton->setVisibility('public'); |
|
68
|
|
|
$mSingleton->addComment('Singleton NO THREAD SAFE!'); |
|
69
|
|
|
$mSingleton->addComment('@return '.$fullClassName.'|null'); |
|
70
|
|
|
$mSingleton->setFinal(true); |
|
71
|
|
|
$body = 'if ( is_null(self::$instance) ) {'; |
|
72
|
|
|
$body .= ' self::$instance = new '.$this->currentClass->getName().'();'; |
|
73
|
|
|
$body .= '}'; |
|
74
|
|
|
$body .= 'return self::$instance;'; |
|
75
|
|
|
$mSingleton->setBody($body); |
|
76
|
|
|
} |
|
77
|
|
|
$field = $this->currentClass->addProperty('instance'); |
|
78
|
|
|
$field->setVisibility('protected'); |
|
79
|
|
|
$field->setStatic(true); |
|
80
|
|
|
$field->addComment($comment)->addComment('@var '.$fullClassName); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param boolean $isConcrete |
|
85
|
|
|
*/ |
|
86
|
|
|
private function addGetter($fieldName, $fieldClassFull, $isStatic, $isConcrete) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->info('Aggiungo getter', [ |
|
89
|
|
|
'class' => $this->currentClass->getName(), |
|
90
|
|
|
'field' => $fieldName, |
|
91
|
|
|
'type' => $fieldClassFull, |
|
92
|
|
|
'static' => $isStatic, |
|
93
|
|
|
'concrete' => $isConcrete, |
|
94
|
|
|
]); |
|
95
|
|
|
|
|
96
|
|
|
/** $methodGetter @var \Nette\PhpGenerator\Method */ |
|
97
|
|
|
$methodGetter = $this->currentClass->addMethod('get'.ucfirst($fieldName)); |
|
98
|
|
|
$methodGetter->setStatic($isStatic); |
|
99
|
|
|
$methodGetter->addComment('@return '.$fieldClassFull); |
|
100
|
|
|
if ($isConcrete) { |
|
101
|
|
|
$methodGetter->setFinal(true); |
|
102
|
|
|
if ($isStatic) { |
|
103
|
|
|
$methodGetter->setBody('return self::$?;', [$fieldName]); |
|
104
|
|
|
} else { |
|
105
|
|
|
$methodGetter->setBody('return $this->?;', [$fieldName]); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
private function addTrait($trait, $typesReference) |
|
111
|
|
|
{ |
|
112
|
|
|
$traitFull = $trait; |
|
113
|
|
|
if (array_key_exists($trait, $typesReference)) { |
|
114
|
|
|
$traitNamespace = $typesReference[$trait]; |
|
115
|
|
|
$traitFull = $traitNamespace.'\\'.$trait; |
|
116
|
|
|
} |
|
117
|
|
|
$this->currentClass->getNamespace()->addUse($traitFull); |
|
118
|
|
|
$this->currentClass->addTrait($traitFull); |
|
119
|
|
|
$this->info('Add trait', [ |
|
120
|
|
|
'class' => $this->currentClass->getName(), |
|
121
|
|
|
'trait' => $traitFull, |
|
122
|
|
|
]); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param boolean $isConcrete |
|
127
|
|
|
*/ |
|
128
|
|
|
private function addSetter($fieldName, $fieldClassFull, $isStatic, $isConcrete) |
|
129
|
|
|
{ |
|
130
|
|
|
$this->info('Aggiungo setter', [ |
|
131
|
|
|
'class' => $this->currentClass->getName(), |
|
132
|
|
|
'field' => $fieldName, |
|
133
|
|
|
'type' => $fieldClassFull, |
|
134
|
|
|
'static' => $isStatic, |
|
135
|
|
|
'concrete' => $isConcrete, |
|
136
|
|
|
]); |
|
137
|
|
|
|
|
138
|
|
|
/** $methodSetter @var \Nette\PhpGenerator\Method */ |
|
139
|
|
|
$methodSetter = $this->currentClass->addMethod('set'.ucfirst($fieldName)); |
|
140
|
|
|
$methodSetter->setStatic($isStatic); |
|
141
|
|
|
$methodSetter->addComment('@var '.$fieldName.' '.$fieldClassFull); |
|
142
|
|
|
$methodSetter->addParameter($fieldName)->setTypeHint($fieldClassFull); |
|
143
|
|
|
|
|
144
|
|
|
if ($isConcrete) { |
|
145
|
|
|
$methodSetter->setFinal(true); |
|
146
|
|
|
if ($isStatic) { |
|
147
|
|
|
$methodSetter->setBody('self::$? = $?;', [$fieldName, $fieldName]); |
|
148
|
|
|
} else { |
|
149
|
|
|
$methodSetter->setBody('$this->? = $?;', [$fieldName, $fieldName]); |
|
150
|
|
|
} |
|
151
|
|
|
$methodSetter->addParameter($fieldName)->setTypeHint($fieldClassFull); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
private function addParseString() |
|
156
|
|
|
{ |
|
157
|
|
|
$fieldClassFull = $this->currentClass->getNamespace()->getName().'\\'.$this->currentClass->getName(); |
|
158
|
|
|
|
|
159
|
|
|
$this->info('Aggiungo parseString', [ |
|
160
|
|
|
'class' => $this->currentClass->getName(), |
|
161
|
|
|
]); |
|
162
|
|
|
|
|
163
|
|
|
/** $methodParseString @var \Nette\PhpGenerator\Method */ |
|
164
|
|
|
$methodParseString = $this->currentClass->addMethod('parseString'); |
|
165
|
|
|
$methodParseString->setFinal(true); |
|
166
|
|
|
$methodParseString->setStatic(true); |
|
167
|
|
|
$methodParseString->addComment('@return '.$fieldClassFull.'|null'); |
|
168
|
|
|
$methodParseString->addParameter('parseString'); |
|
169
|
|
|
$body = '$className = \''.$fieldClassFull.'\'.\'\\\\\'.$parseString;'."\n"; |
|
170
|
|
|
$body .= 'if (class_exists($className)) {'; |
|
171
|
|
|
$body .= "\t".'$enumClass = $className::instance();'; |
|
172
|
|
|
$body .= "\t".''; |
|
173
|
|
|
$body .= "\t".'return $enumClass;'; |
|
174
|
|
|
$body .= '}'; |
|
175
|
|
|
//$methodParseString->setBody('self::$? = $?;', [$field_name, $field_name]); |
|
|
|
|
|
|
176
|
|
|
$methodParseString->setBody($body); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* [generateClassType description]. |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $properties elementi possibili 'fields', 'extend', 'implements' |
|
183
|
|
|
* @param array $typesReference [description] |
|
184
|
|
|
* @param array $typesDescription [description] |
|
185
|
|
|
* @param ClassConfig $config [description] |
|
186
|
|
|
*/ |
|
187
|
|
|
public function generateClassType(array $properties, $typesReference, $typesDescription, ClassConfig $config) |
|
188
|
|
|
{ |
|
189
|
|
|
$phpNamespace = $this->currentClass->getNamespace(); |
|
190
|
|
|
if ($config->isInterface) { |
|
191
|
|
|
$this->info('Passo a interfaccia', [$this->currentClass->getName()]); |
|
192
|
|
|
$docs = $this->currentClass->getComment(); |
|
193
|
|
|
$this->currentClass = $this->currentFile->addInterface($phpNamespace->getName().'\\'.ucfirst($this->currentClass->getName())); |
|
194
|
|
|
$this->currentClass->setComment($docs); |
|
195
|
|
|
$this->info('Check haveConstructor, in caso metto a false', [$config->haveConstructor]); |
|
196
|
|
|
$config->haveConstructor = false; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
$this->info('Generate', ['class' => $this->currentClass->getName(), 'namespace' => $phpNamespace->getName(), 'comment' => $this->currentClass->getComment(), 'properties' => $properties]); |
|
200
|
|
|
|
|
201
|
|
|
// extend class |
|
202
|
|
View Code Duplication |
if (array_key_exists('extend', $properties)) { |
|
|
|
|
|
|
203
|
|
|
$extendClassName = $properties['extend']; |
|
204
|
|
|
$this->info('Aggiungo extend', [ |
|
205
|
|
|
'class' => $this->currentClass->getName(), |
|
206
|
|
|
'extend' => $extendClassName, |
|
207
|
|
|
]); |
|
208
|
|
|
$this->currentClass->setExtends($extendClassName); |
|
209
|
|
|
$this->currentClass->getNamespace()->addUse($extendClassName); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
// implements class |
|
213
|
|
|
if (array_key_exists('implements', $properties)) { |
|
214
|
|
|
$implementsList = []; |
|
215
|
|
|
if (!is_array($properties['implements'])) { |
|
216
|
|
|
$implementsList[] = $properties['implements']; |
|
217
|
|
|
} else { |
|
218
|
|
|
$implementsList = array_merge($implementsList, $properties['implements']); |
|
219
|
|
|
} |
|
220
|
|
|
$this->currentClass->setImplements($implementsList); |
|
221
|
|
|
foreach ($implementsList as $implementUse) { |
|
222
|
|
|
$this->info('Aggiungo implement', [ |
|
223
|
|
|
'class' => $this->currentClass->getName(), |
|
224
|
|
|
'implements' => $implementUse, |
|
225
|
|
|
]); |
|
226
|
|
|
$this->currentClass->getNamespace()->addUse($implementUse); |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
// traits |
|
231
|
|
|
if (array_key_exists('traits', $properties)) { |
|
232
|
|
|
if (is_array($properties['traits'])) { |
|
233
|
|
|
foreach ($properties['traits'] as $trait) { |
|
234
|
|
|
$this->addTrait($trait, $typesReference); |
|
235
|
|
|
} |
|
236
|
|
|
} else { |
|
237
|
|
|
$traitObject = $properties['traits']; |
|
238
|
|
|
$this->addTrait($traitObject, $typesReference); |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
if ($config->isFinalClass) { |
|
243
|
|
|
$this->currentClass->setFinal(true); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
$first = true; |
|
247
|
|
|
if (array_key_exists('fields', $properties)) { |
|
248
|
|
|
/** @var $methodConstructor \Nette\PhpGenerator\Method */ |
|
249
|
|
|
$methodConstructor = null; |
|
250
|
|
|
if ($config->haveConstructor) { |
|
251
|
|
|
$methodConstructor = $this->addConstructor(); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
$body = ''; |
|
255
|
|
|
|
|
256
|
|
|
foreach ($properties['fields'] as $name => $fieldProperties) { |
|
257
|
|
|
$isStatic = false; |
|
258
|
|
|
$isAutoinizialize = false; |
|
259
|
|
|
$defaultValue = null; |
|
260
|
|
|
if (array_key_exists('static', $fieldProperties)) { |
|
261
|
|
|
$isStatic = $fieldProperties['static']; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
if (array_key_exists('autoinizialize', $fieldProperties)) { |
|
265
|
|
|
$isAutoinizialize = boolval($fieldProperties['autoinizialize']); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
if (array_key_exists('default', $fieldProperties)) { |
|
269
|
|
|
$defaultValue = $fieldProperties['default']; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
if (!$isAutoinizialize) { |
|
273
|
|
|
if (null != $defaultValue) { |
|
274
|
|
|
//TODO: usare "primitive type per determinare il corretto IF" |
|
|
|
|
|
|
275
|
|
|
//FARE UN TEST PER I BOOLEAN |
|
276
|
|
|
//@see https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ |
|
277
|
|
|
$body .= 'if ( empty($'.$name.') ) { '."\n"; |
|
278
|
|
|
if ($isStatic) { |
|
279
|
|
|
$body .= ' self::$'; |
|
280
|
|
|
} else { |
|
281
|
|
|
$body .= ' $this->'; |
|
282
|
|
|
} |
|
283
|
|
|
$body .= $name.' = '.$defaultValue.';'."\n"; |
|
284
|
|
|
$body .= '} else {'; |
|
285
|
|
|
if ($isStatic) { |
|
286
|
|
|
$body .= ' self::$'; |
|
287
|
|
|
} else { |
|
288
|
|
|
$body .= ' $this->'; |
|
289
|
|
|
} |
|
290
|
|
|
$body .= $name.' = $'.$name.';'."\n"; |
|
291
|
|
|
$body .= '}'."\n"; |
|
292
|
|
|
} else { |
|
293
|
|
|
if (!$isStatic) { |
|
294
|
|
|
$body .= ' $this->'.$name.' = $'.$name.';'."\n"; |
|
295
|
|
|
} |
|
296
|
|
|
} |
|
297
|
|
|
} else { |
|
298
|
|
|
if (!empty($defaultValue) || is_int($defaultValue)) { |
|
299
|
|
|
if (substr(rtrim($defaultValue), -1) == ';') { |
|
300
|
|
|
$this->error('autoinizialize for '.$name.' on class '.$this->currentClass->getName().' have default with ";" please remove!'); |
|
301
|
|
|
$defaultValue = substr($defaultValue, 0, strlen($defaultValue) - 1); |
|
302
|
|
|
} |
|
303
|
|
|
if (!$isStatic) { |
|
304
|
|
|
if ($isAutoinizialize) { |
|
305
|
|
|
$body .= '// autoinizialize'."\n"; |
|
306
|
|
|
$body .= '$this->'.$name.' = '.$defaultValue.';'."\n"; |
|
307
|
|
|
} else { |
|
308
|
|
|
if ($defaultValue) { |
|
309
|
|
|
$body .= 'if ( !is_null($'.$name.') ) {'."\n"; |
|
310
|
|
|
$body .= ' $this->'.$name.' = $'.$name.';'."\n"; |
|
311
|
|
|
$body .= '} else {'."\n"; |
|
312
|
|
|
$body .= ' $this->'.$name.' = '.$defaultValue.';'."\n"; |
|
313
|
|
|
$body .= '}'."\n"; |
|
314
|
|
|
// $body .= '$this->'.$name.' = '.$defaultValue.';'."\n"; |
|
|
|
|
|
|
315
|
|
|
} else { |
|
316
|
|
|
$body .= 'if ( is_null($'.$name.') ) {'."\n"; |
|
317
|
|
|
$body .= ' $this->'.$name.' = '.$defaultValue.';'."\n"; |
|
318
|
|
|
$body .= '}'."\n"; |
|
319
|
|
|
} |
|
320
|
|
|
} |
|
321
|
|
|
} |
|
322
|
|
|
} else { |
|
323
|
|
|
$this->error('autoinizialize for '.$name.' not defined on element '.$this->currentClass->getName()); |
|
324
|
|
|
$this->errors[] = 'autoinizialize for '.$name.' not defined on element '.$this->currentClass->getName(); |
|
325
|
|
|
} |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
$fieldClassFull = ''; |
|
|
|
|
|
|
329
|
|
|
if (array_key_exists('class', $fieldProperties)) { |
|
330
|
|
|
$fieldClassName = ucfirst($fieldProperties['class']); |
|
331
|
|
|
|
|
332
|
|
|
if (array_key_exists($fieldClassName, $typesReference)) { |
|
333
|
|
|
$fieldNamespace = $typesReference[$fieldClassName]; |
|
334
|
|
|
$fieldClassFull = $fieldNamespace.'\\'.$fieldClassName; |
|
335
|
|
|
$this->info('Trovato field namespace tra le reference', [ |
|
336
|
|
|
'class' => $this->currentClass->getName(), |
|
337
|
|
|
'field' => $fieldClassName, |
|
338
|
|
|
'className' => $fieldClassFull, |
|
339
|
|
|
]); |
|
340
|
|
|
} else { |
|
341
|
|
|
//FIXME: strpos is better |
|
|
|
|
|
|
342
|
|
|
if ($fieldClassName[0] == '\\') { |
|
343
|
|
|
//Class: \DateTime |
|
344
|
|
|
$fieldClassFull = $fieldClassName; |
|
345
|
|
|
} else { |
|
346
|
|
|
$fieldClassFull = $phpNamespace->getName().'\\'.$fieldClassName; |
|
347
|
|
|
$this->info('Uso class for field same namespace', [ |
|
348
|
|
|
'class' => $this->currentClass->getName(), |
|
349
|
|
|
'field' => $fieldClassName, |
|
350
|
|
|
'className' => $fieldClassFull, |
|
351
|
|
|
]); |
|
352
|
|
|
} |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
if ($config->haveConstructor && !$isStatic) { |
|
356
|
|
|
$parameter = null; |
|
|
|
|
|
|
357
|
|
|
if (!$isAutoinizialize) { |
|
358
|
|
|
$this->info('Aggiungo parametro al costruttore', [ |
|
359
|
|
|
'class' => $this->currentClass->getName(), |
|
360
|
|
|
'parameter' => $name, |
|
361
|
|
|
'className' => $fieldClassFull, |
|
362
|
|
|
'default' => $defaultValue, |
|
363
|
|
|
'autoinizialize' => $isAutoinizialize, |
|
364
|
|
|
]); |
|
365
|
|
|
if (!$first) { |
|
366
|
|
|
$parameter = $methodConstructor->addParameter($name, null); //solo i primitivi hanno un default, gli altri null come object |
|
367
|
|
|
$parameter->setTypeHint($fieldClassFull); |
|
368
|
|
|
} else { |
|
369
|
|
|
$parameter = $methodConstructor->addParameter($name); |
|
370
|
|
|
$parameter->setTypeHint($fieldClassFull); |
|
371
|
|
|
} |
|
372
|
|
|
} else { |
|
373
|
|
|
$this->info('Skip parametro al costruttore -> autoinizialize true', [ |
|
374
|
|
|
'class' => $this->currentClass->getName(), |
|
375
|
|
|
'parameter' => $name, |
|
376
|
|
|
'className' => $fieldClassFull, |
|
377
|
|
|
'default' => $defaultValue, |
|
378
|
|
|
'autoinizialize' => $isAutoinizialize, |
|
379
|
|
|
]); |
|
380
|
|
|
} |
|
381
|
|
|
} |
|
382
|
|
|
|
|
383
|
|
View Code Duplication |
if (array_key_exists($fieldClassName, $typesReference)) { |
|
|
|
|
|
|
384
|
|
|
$this->info('Add field type class with namespace', [ |
|
385
|
|
|
'class' => $this->currentClass->getName(), |
|
386
|
|
|
'field' => $fieldClassName, |
|
387
|
|
|
'className' => $fieldClassFull, |
|
388
|
|
|
]); |
|
389
|
|
|
$this->currentClass->getNamespace()->addUse($fieldClassFull); |
|
390
|
|
|
} |
|
391
|
|
|
} else { |
|
392
|
|
|
//tipo primitivo |
|
393
|
|
|
$fieldClassName = $fieldProperties['primitive']; |
|
394
|
|
|
$fieldNamespace = null; |
|
|
|
|
|
|
395
|
|
|
$fieldClassFull = $fieldProperties['primitive']; |
|
396
|
|
|
if ($config->haveConstructor && !$isStatic) { |
|
397
|
|
|
//FIXME: se sono in php7 ho anche gli altri elementi primitivi |
|
|
|
|
|
|
398
|
|
|
//@see: http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration |
|
399
|
|
|
|
|
400
|
|
|
$parameter = null; |
|
|
|
|
|
|
401
|
|
|
|
|
402
|
|
|
if (!$isAutoinizialize) { |
|
403
|
|
|
if (is_null($defaultValue)) { |
|
404
|
|
|
$this->info('Aggiungo parametro al costruttore', [ |
|
405
|
|
|
'class' => $this->currentClass->getName(), |
|
406
|
|
|
'parameter' => $name, |
|
407
|
|
|
'className' => $fieldClassFull, |
|
408
|
|
|
'default' => $defaultValue, |
|
409
|
|
|
'autoinizialize' => $isAutoinizialize, |
|
410
|
|
|
]); |
|
411
|
|
|
|
|
412
|
|
|
//PHP7 ONLY |
|
413
|
|
|
// if ($fieldClassFull == 'int') { |
|
|
|
|
|
|
414
|
|
|
// $parameter->setTypeHint('int'); |
|
|
|
|
|
|
415
|
|
|
// } |
|
416
|
|
|
|
|
417
|
|
|
if (!$first) { |
|
418
|
|
|
$parameter = $methodConstructor->addParameter($name, null); |
|
419
|
|
|
} else { |
|
420
|
|
|
$parameter = $methodConstructor->addParameter($name); |
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
|
|
if ($fieldClassFull == 'array') { |
|
424
|
|
|
$parameter->setTypeHint('array'); |
|
425
|
|
|
} else { |
|
426
|
|
|
if ($defaultValue != null) { |
|
427
|
|
|
/* @var $parameter \Nette\PhpGenerator\Parameter */ |
|
428
|
|
|
$parameter->setDefaultValue(''.$defaultValue); |
|
429
|
|
|
} |
|
430
|
|
|
} |
|
431
|
|
|
} |
|
432
|
|
|
} |
|
433
|
|
|
} |
|
434
|
|
|
} |
|
435
|
|
|
|
|
436
|
|
|
$this->info('Check autoinizialize field', [ |
|
437
|
|
|
'class' => $this->currentClass->getName(), |
|
438
|
|
|
'field' => $name, |
|
439
|
|
|
'autoinizialize' => $isAutoinizialize, |
|
440
|
|
|
'default' => $defaultValue, |
|
441
|
|
|
]); |
|
442
|
|
|
|
|
443
|
|
|
$comment = 'no description available'; |
|
444
|
|
|
if (array_key_exists('description', $fieldProperties)) { |
|
445
|
|
|
$comment = $fieldProperties['description']; |
|
446
|
|
|
} else { |
|
447
|
|
|
if (!is_null($typesDescription) && array_key_exists($fieldClassName, $typesDescription)) { |
|
448
|
|
|
$comment = $typesDescription[$fieldClassName]; |
|
449
|
|
|
} |
|
450
|
|
|
} |
|
451
|
|
|
|
|
452
|
|
|
if (!$config->isInterface) { |
|
453
|
|
|
/** $field @var \Nette\PhpGenerator\Property */ |
|
454
|
|
|
$field = $this->currentClass->addProperty($name); |
|
455
|
|
|
$field->setStatic($isStatic); |
|
456
|
|
|
if ($config->isEnum) { |
|
457
|
|
|
$field->setVisibility('protected'); |
|
458
|
|
|
} else { |
|
459
|
|
|
$field->setVisibility('private'); |
|
460
|
|
|
} |
|
461
|
|
|
$field->addComment($comment)->addComment('@var '.$fieldClassFull); |
|
462
|
|
|
} |
|
463
|
|
|
|
|
464
|
|
|
$createSetter = $config->haveSetter; |
|
465
|
|
|
if (array_key_exists('setter', $fieldProperties)) { |
|
466
|
|
|
$createSetter = $fieldProperties['setter']; |
|
467
|
|
|
} |
|
468
|
|
|
|
|
469
|
|
|
$createGetter = $config->haveGetter; |
|
470
|
|
|
if (array_key_exists('getter', $fieldProperties)) { |
|
471
|
|
|
$createGetter = $fieldProperties['getter']; |
|
472
|
|
|
} |
|
473
|
|
|
|
|
474
|
|
|
if ($config->isInterface) { |
|
475
|
|
|
if ($createGetter) { |
|
476
|
|
|
$this->addGetter($name, $fieldClassFull, $isStatic, false); |
|
477
|
|
|
} |
|
478
|
|
|
|
|
479
|
|
|
if ($createSetter) { |
|
480
|
|
|
$this->addSetter($name, $fieldClassFull, $isStatic, false); |
|
481
|
|
|
} |
|
482
|
|
|
} else { |
|
483
|
|
|
if ($createGetter) { |
|
484
|
|
|
$this->addGetter($name, $fieldClassFull, $isStatic, true); |
|
485
|
|
|
} |
|
486
|
|
|
|
|
487
|
|
|
if ($createSetter) { |
|
488
|
|
|
$this->addSetter($name, $fieldClassFull, $isStatic, true); |
|
489
|
|
|
} |
|
490
|
|
|
} |
|
491
|
|
|
if (!$isAutoinizialize) { |
|
492
|
|
|
$first = false; |
|
493
|
|
|
} |
|
494
|
|
|
} |
|
495
|
|
|
if ($config->haveConstructor) { |
|
496
|
|
|
$methodConstructor->setBody($body, []); |
|
497
|
|
|
} |
|
498
|
|
|
} //end fields |
|
499
|
|
|
|
|
500
|
|
|
if (array_key_exists('methods', $properties)) { |
|
501
|
|
|
$body = ''; |
|
|
|
|
|
|
502
|
|
|
|
|
503
|
|
|
foreach ($properties['methods'] as $methodName => $methodsProperties) { |
|
504
|
|
|
$this->info('Aggiungo method', [ |
|
505
|
|
|
'class' => $this->currentClass->getName(), |
|
506
|
|
|
'methodName' => $methodName, |
|
507
|
|
|
'methodProp' => $methodsProperties, |
|
508
|
|
|
]); |
|
509
|
|
|
|
|
510
|
|
|
/** $newMethodCall @var \Nette\PhpGenerator\Method */ |
|
511
|
|
|
$newMethodCall = $this->currentClass->addMethod($methodName); |
|
512
|
|
|
$newMethodCall->setFinal(true); |
|
513
|
|
|
|
|
514
|
|
|
$newMethodCall->setStatic(false); |
|
515
|
|
|
if (array_key_exists('static', $methodsProperties)) { |
|
516
|
|
|
$newMethodCall->setStatic($methodsProperties['static']); |
|
517
|
|
|
} |
|
518
|
|
|
|
|
519
|
|
|
if (array_key_exists('description', $methodsProperties)) { |
|
520
|
|
|
$newMethodCall->setVisibility($methodsProperties['visibility']); |
|
521
|
|
|
} else { |
|
522
|
|
|
$newMethodCall->setVisibility('public'); |
|
523
|
|
|
} |
|
524
|
|
|
|
|
525
|
|
|
if (array_key_exists('description', $methodsProperties)) { |
|
526
|
|
|
$newMethodCall->addComment($methodsProperties['description']); |
|
527
|
|
|
} else { |
|
528
|
|
|
$returnType = 'void'; |
|
|
|
|
|
|
529
|
|
|
if (array_key_exists('@return', $methodsProperties)) { |
|
530
|
|
|
$returnType = $methodsProperties['@return']; |
|
531
|
|
|
//TODO: .'|null' va messo in quale condizione? |
|
|
|
|
|
|
532
|
|
|
$newMethodCall->addComment('@return '.$returnType); |
|
533
|
|
|
} else { |
|
|
|
|
|
|
534
|
|
|
//NOPE |
|
535
|
|
|
} |
|
536
|
|
|
} |
|
537
|
|
|
|
|
538
|
|
|
if (array_key_exists('params', $methodsProperties)) { |
|
539
|
|
|
foreach ($methodsProperties['params'] as $paramName => $paramProp) { |
|
540
|
|
|
if (array_key_exists('class', $paramProp)) { |
|
541
|
|
|
$newMethodCall->addParameter($paramName)->setTypeHint($paramProp['class']); |
|
542
|
|
|
} |
|
543
|
|
|
if (array_key_exists('primitive', $paramProp)) { |
|
544
|
|
|
$newMethodCall->addParameter($paramName); |
|
545
|
|
|
} |
|
546
|
|
|
} |
|
547
|
|
|
} |
|
548
|
|
|
$body = ' // FIMXE: da implementare '; |
|
549
|
|
|
if (array_key_exists('body', $methodsProperties)) { |
|
550
|
|
|
$body = $methodsProperties['body']; |
|
551
|
|
|
} |
|
552
|
|
|
$newMethodCall->setBody($body); |
|
553
|
|
|
} |
|
554
|
|
|
} |
|
555
|
|
|
|
|
556
|
|
|
if ($config->isEnum) { |
|
557
|
|
|
$this->currentClass->setAbstract(true); |
|
558
|
|
|
$this->addSingleton('Singleton instance for enum', false); |
|
559
|
|
|
$this->addParseString(); |
|
560
|
|
|
} |
|
561
|
|
|
|
|
562
|
|
|
if ($config->isSingleton) { |
|
563
|
|
|
$this->addSingleton('Singleton instance', true); |
|
564
|
|
|
} |
|
565
|
|
|
} |
|
566
|
|
|
|
|
567
|
|
|
public function createFileOnDir(Local $adapter) |
|
568
|
|
|
{ |
|
569
|
|
|
$outFile = str_replace('\\', '/', $this->currentClass->getNamespace()->getName().'\\'.$this->currentClass->getName()).'.php'; |
|
570
|
|
|
$this->_createFileOnDir($adapter, $outFile); |
|
571
|
|
|
} |
|
572
|
|
|
} |
|
573
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.