|
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 Psr\Log\LoggerInterface; |
|
16
|
|
|
use Yoghi\Bundle\MaddaBundle\Model\Reader; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Stefano Tamagnini <> |
|
20
|
|
|
*/ |
|
21
|
|
|
class DDDGenerator |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* [$logger description]. |
|
25
|
|
|
* |
|
26
|
|
|
* @var \Psr\Log\LoggerInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $logger; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Reader model file. |
|
32
|
|
|
* |
|
33
|
|
|
* @var \Yoghi\Bundle\MaddaBundle\Model\Reader |
|
34
|
|
|
*/ |
|
35
|
|
|
private $rym; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Model classes: class -> namespace. |
|
39
|
|
|
* |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
private $modelClass; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Comments of model classes. |
|
46
|
|
|
* |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
private $modelComments; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Fields of model classes. |
|
53
|
|
|
* |
|
54
|
|
|
* @var array |
|
55
|
|
|
*/ |
|
56
|
|
|
private $fieldsClass; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Array process errors. |
|
60
|
|
|
* |
|
61
|
|
|
* @var array |
|
62
|
|
|
*/ |
|
63
|
|
|
private $errors; |
|
64
|
|
|
|
|
65
|
|
|
public function __construct() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->rym = new Reader(); |
|
68
|
|
|
$this->errors = []; |
|
69
|
|
|
$this->modelClass = []; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function setLogger(LoggerInterface $logger) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->logger = $logger; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* [analyze description]. |
|
79
|
|
|
* |
|
80
|
|
|
* @param [type] $fullPathFile [description] |
|
|
|
|
|
|
81
|
|
|
* |
|
82
|
|
|
* @return [type] [description] |
|
|
|
|
|
|
83
|
|
|
*/ |
|
84
|
|
|
public function analyze($fullPathFile) |
|
85
|
|
|
{ |
|
86
|
|
|
$this->rym->readYaml($fullPathFile); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param string $message |
|
91
|
|
|
*/ |
|
92
|
|
|
private function info($message, $context = []) |
|
93
|
|
|
{ |
|
94
|
|
|
if (!is_null($this->logger)) { |
|
95
|
|
|
$this->logger->info($message, $context); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param string $message |
|
101
|
|
|
*/ |
|
102
|
|
|
private function error($message, $context = []) |
|
103
|
|
|
{ |
|
104
|
|
|
if (!is_null($this->logger)) { |
|
105
|
|
|
$this->logger->error($message, $context); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* errori durante la generazione. |
|
111
|
|
|
* |
|
112
|
|
|
* @return array of string |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getErrors() |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->errors; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* [generate description]. |
|
121
|
|
|
* |
|
122
|
|
|
* @param Local $directoryOutput directory where write generated class |
|
123
|
|
|
*/ |
|
124
|
|
|
public function generate(Local $directoryOutput) |
|
125
|
|
|
{ |
|
126
|
|
|
//$is_constructor_enable = true; |
|
|
|
|
|
|
127
|
|
|
//$ddd_is_root_aggregate = false; |
|
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
$specListClasses = $this->rym->getClassesDefinition(); |
|
130
|
|
|
foreach ($specListClasses as $className => $properties) { |
|
|
|
|
|
|
131
|
|
|
$this->info('Generate class', ['class' => $className]); //, 'properties' => $properties |
|
|
|
|
|
|
132
|
|
|
if (!array_key_exists('ddd', $properties)) { |
|
133
|
|
|
$this->error('missing ddd section into yml for class', ['class' => $className]); |
|
134
|
|
|
$this->errors[] = 'missing ddd section into yml for class '.$className; |
|
135
|
|
|
$this->info('force '.$className.' to type class'); |
|
136
|
|
|
$properties['ddd'] = []; |
|
137
|
|
|
$properties['ddd']['type'] = 'class'; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$namespace = ''; |
|
141
|
|
|
if (array_key_exists('namespace', $properties)) { |
|
142
|
|
|
$namespace = $properties['namespace']; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$classComments = 'No comment found on ddd model'; |
|
146
|
|
|
if (array_key_exists('description', $properties)) { |
|
147
|
|
|
$classComments = $properties['description']; |
|
148
|
|
|
$this->info('Found description :'.$classComments); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
//FIXME: switch with $dddType as key |
|
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
$generated = false; |
|
154
|
|
|
$dddType = $properties['ddd']['type']; |
|
155
|
|
View Code Duplication |
if (in_array($dddType, ['interface'])) { |
|
|
|
|
|
|
156
|
|
|
$g = new ClassGenerator($namespace, $className, $classComments); |
|
157
|
|
|
$g->setLogger($this->logger); |
|
158
|
|
|
$config = new ClassConfig(); |
|
159
|
|
|
$config->isInterface = true; |
|
160
|
|
|
$g->generateClassType($properties, $this->modelClass, $this->modelComments, $config); |
|
161
|
|
|
$g->createFileOnDir($directoryOutput); |
|
162
|
|
|
$generated = true; //FIXME: use $g for determinate! -> take error from generator |
|
|
|
|
|
|
163
|
|
|
if ($generated) { |
|
164
|
|
|
$this->fieldsClass[$namespace.'\\'.$className] = $properties['fields']; //ONLY IF VALID!!! |
|
|
|
|
|
|
165
|
|
|
} |
|
166
|
|
|
// DOMANDA: perche' non passarle tutte?? |
|
167
|
|
|
// if (array_key_exists('fields', $properties)) { |
|
|
|
|
|
|
168
|
|
|
// $types_field[$className] = $properties['fields']; |
|
|
|
|
|
|
169
|
|
|
// } |
|
170
|
|
|
// $this->generateClassType($fileInterface, $interface, $properties, $types_reference, $types_description, false, true, true, false, false, $filesystem, $io); |
|
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
// NOTE: class aren't ddd type, we haven't section on ddd definition |
|
174
|
|
View Code Duplication |
if (in_array($dddType, ['class'])) { |
|
|
|
|
|
|
175
|
|
|
$g = new ClassGenerator($namespace, $className, $classComments); |
|
176
|
|
|
$g->setLogger($this->logger); |
|
177
|
|
|
$config = new ClassConfig(); |
|
178
|
|
|
$config->isInterface = false; |
|
179
|
|
|
$config->haveConstructor = true; |
|
180
|
|
|
$g->generateClassType($properties, $this->modelClass, $this->modelComments, $config); |
|
181
|
|
|
$g->createFileOnDir($directoryOutput); |
|
182
|
|
|
$generated = true; //FIXME: use $g for determinate! -> take error from generator |
|
|
|
|
|
|
183
|
|
|
if ($generated) { |
|
184
|
|
|
$this->fieldsClass[$namespace.'\\'.$className] = $properties['fields']; //ONLY IF VALID!!! |
|
|
|
|
|
|
185
|
|
|
} |
|
186
|
|
|
// DOMANDA: perche' non passarle tutte?? |
|
187
|
|
|
// if (array_key_exists('fields', $properties)) { |
|
|
|
|
|
|
188
|
|
|
// $types_field[$className] = $properties['fields']; |
|
|
|
|
|
|
189
|
|
|
// } |
|
190
|
|
|
// $this->generateClassType($fileInterface, $interface, $properties, $types_reference, $types_description, false, true, true, false, false, $filesystem, $io); |
|
|
|
|
|
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
if (in_array($dddType, ['events'])) { |
|
194
|
|
|
//FIXME: impossible! events exist in relation on aggregateRoot |
|
|
|
|
|
|
195
|
|
|
$this->error('events exist in relation on aggregateRoot', ['class' => $className]); |
|
196
|
|
|
$this->errors[] = 'events exist in relation on aggregateRoot, event class '.$className.' cannot exist!'; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
if (!$generated) { |
|
200
|
|
|
$dddDefinition = $this->rym->getDomainDefinitionAttributes($dddType); |
|
201
|
|
|
|
|
202
|
|
|
if (is_null($dddDefinition)) { |
|
203
|
|
|
$this->error('Missing ddd reference for : '.$dddType.' into '.$className, ['class' => $className]); |
|
204
|
|
|
$this->errors[] = 'Missing ddd reference for : '.$dddType.' into '.$className; |
|
205
|
|
|
} else { |
|
206
|
|
|
if (array_key_exists('package', $dddDefinition)) { |
|
207
|
|
|
$namespace = $dddDefinition['package']; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
if (empty($namespace)) { |
|
211
|
|
|
$this->error('Missing namespace', ['class' => $className]); |
|
212
|
|
|
$this->errors[] = 'Missing namespace for '.$className; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
$createGetter = false; |
|
216
|
|
|
$createSetter = false; |
|
217
|
|
|
if (array_key_exists('getter', $dddDefinition)) { |
|
218
|
|
|
$createGetter = $dddDefinition['getter']; |
|
219
|
|
|
} |
|
220
|
|
|
if (array_key_exists('setter', $dddDefinition)) { |
|
221
|
|
|
$createSetter = $dddDefinition['setter']; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
$isRootAggregate = $dddType == 'aggregate' && isset($properties['ddd']['root']) && boolval($properties['ddd']['root']) ? true : false; |
|
225
|
|
|
|
|
226
|
|
|
$this->info('Method required', ['class' => $className, 'getter' => $createGetter, 'setter' => $createSetter, 'aggregateRoot' => $isRootAggregate]); |
|
227
|
|
|
|
|
228
|
|
|
if (array_key_exists('extend', $dddDefinition)) { |
|
229
|
|
|
$dddExtendDefinition = $dddDefinition['extend']; |
|
230
|
|
|
if (!array_key_exists('extend', $properties)) { |
|
231
|
|
|
$properties['extend'] = $dddExtendDefinition; //No multi-inheritance |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
$dddReferenceFields = []; |
|
236
|
|
|
if (array_key_exists('fields', $dddDefinition)) { |
|
237
|
|
|
foreach ($dddDefinition['fields'] as $key => $value) { |
|
238
|
|
|
$dddReferenceFields[$key] = $value; |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
//TODO: gestire gli [] dentro la definizione del modello se serve... |
|
|
|
|
|
|
243
|
|
|
|
|
244
|
|
|
//TODO: aggiungere le validazioni |
|
|
|
|
|
|
245
|
|
|
// validationRule: |
|
246
|
|
|
// events: |
|
247
|
|
|
// create: |
|
248
|
|
|
// fields: [ id, sessione, tipologiaCampo] |
|
249
|
|
|
// delete: |
|
250
|
|
|
// fields: [ id ] |
|
251
|
|
|
// addDocument: |
|
252
|
|
|
// fields: [ id, documentoCorrelato ] |
|
253
|
|
|
|
|
254
|
|
|
if (array_key_exists('events', $properties)) { |
|
255
|
|
|
//genero altre classi per ogni evento! |
|
256
|
|
|
$eventsProperties = $this->rym->getDomainDefinitionAttributes('events'); |
|
257
|
|
|
$eventsNamespace = $eventsProperties['package']; |
|
258
|
|
|
$eventsImplement = ''; |
|
259
|
|
|
if (array_key_exists('implement', $eventsProperties)) { |
|
260
|
|
|
$eventsImplement = $eventsProperties['implement']; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
$eventsExtend = ''; |
|
264
|
|
|
if (array_key_exists('extend', $eventsProperties)) { |
|
265
|
|
|
$eventsExtend = $eventsProperties['extend']; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
if (!array_key_exists($eventsImplement, $this->modelClass)) { |
|
269
|
|
|
$this->error('Missing implement class '.$eventsImplement, ['class' => $className]); |
|
270
|
|
|
$this->errors[] = 'Missing implement '.$eventsImplement.' for '.$className; |
|
271
|
|
|
continue; |
|
272
|
|
|
} |
|
273
|
|
|
$namespaceImplementClass = $this->modelClass[$eventsImplement]; |
|
274
|
|
|
$eventsImplementFull = $namespaceImplementClass.'\\'.$eventsImplement; |
|
275
|
|
|
|
|
276
|
|
|
$eventsField = []; |
|
277
|
|
|
if (array_key_exists('fields', $eventsProperties)) { |
|
278
|
|
|
foreach ($eventsProperties['fields'] as $key => $value) { |
|
279
|
|
|
$eventsField[$key] = $value; |
|
280
|
|
|
} |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
//field's inheritance |
|
284
|
|
|
if (array_key_exists($eventsImplementFull, $this->fieldsClass)) { |
|
285
|
|
|
$fieldsImplementClass = $this->fieldsClass[$eventsImplementFull]; |
|
286
|
|
|
foreach ($fieldsImplementClass as $key => $value) { |
|
287
|
|
|
$eventsField[$key] = $value; |
|
288
|
|
|
} |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
$eventsToCreate = []; |
|
292
|
|
|
if (array_key_exists('events', $properties)) { |
|
293
|
|
|
$eventsToCreate = $properties['events']; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
if (array_key_exists('events', $dddDefinition)) { |
|
297
|
|
|
$eventsToCreate = array_merge($dddDefinition['events'], $eventsToCreate); |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
foreach ($eventsToCreate as $event) { |
|
301
|
|
|
$eventClassName = $className.str_replace('_', '', ucwords($event, '_')).'Event'; |
|
302
|
|
|
$eventClassComments = 'Event '.$event.' for Aggregate Root '.$className; |
|
303
|
|
|
|
|
304
|
|
|
$propertiesEventClass = []; |
|
305
|
|
|
if (!empty($eventsExtend)) { |
|
306
|
|
|
$propertiesEventClass['extend'] = $eventsExtend; |
|
307
|
|
|
} |
|
308
|
|
|
if (!empty($eventsImplement)) { |
|
309
|
|
|
$propertiesEventClass['implements'] = $eventsImplementFull; |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
$propertiesEventClass['fields'] = $eventsField; |
|
313
|
|
|
|
|
314
|
|
|
$this->info('Create Event', ['event' => $event, 'class' => $className, 'extend' => $eventsExtend, 'implement' => $eventsImplementFull, 'fields' => $eventsField]); |
|
315
|
|
|
|
|
316
|
|
|
$g = new ClassGenerator($eventsNamespace, $eventClassName, $eventClassComments); |
|
317
|
|
|
$g->setLogger($this->logger); |
|
318
|
|
|
$config = new ClassConfig(); |
|
319
|
|
|
$config->isInterface = false; |
|
320
|
|
|
$config->haveConstructor = true; |
|
321
|
|
|
$config->isFinalClass = true; //don't wnat cycle dependency |
|
322
|
|
|
$config->haveGetter = true; |
|
323
|
|
|
$config->haveSetter = false; |
|
324
|
|
|
$g->generateClassType($propertiesEventClass, $this->modelClass, $this->modelComments, $config); |
|
325
|
|
|
$g->createFileOnDir($directoryOutput); |
|
326
|
|
|
$generated = true; |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
if ($generated) { |
|
330
|
|
|
$this->fieldsClass[$namespace.'\\'.$className] = $eventsField; //ONLY IF VALID!!! |
|
|
|
|
|
|
331
|
|
|
} |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
if (array_key_exists('enum', $properties)) { |
|
335
|
|
|
$enumClassList = $properties['enum']; |
|
336
|
|
|
foreach ($enumClassList as $enumClassName) { |
|
337
|
|
|
$enumNamespace = $namespace.'\\'.$className; |
|
338
|
|
|
$propertiesEnumClass = [ |
|
339
|
|
|
'extend' => $namespace.'\\'.$className, |
|
340
|
|
|
]; |
|
341
|
|
|
$actionName = 'instance'; |
|
342
|
|
|
$propertiesEnumClass['methods'] = []; |
|
343
|
|
|
$propertiesEnumClass['methods'][$actionName] = []; |
|
344
|
|
|
$propertiesEnumClass['methods'][$actionName]['params'] = []; |
|
345
|
|
|
$propertiesEnumClass['methods'][$actionName]['static'] = true; |
|
346
|
|
|
$propertiesEnumClass['methods'][$actionName]['@return'] = $enumNamespace.'\\'.$enumClassName; |
|
347
|
|
|
$body = 'self::$instance = new '.$enumClassName.'();'; |
|
348
|
|
|
$body .= 'return self::$instance;'; |
|
349
|
|
|
$propertiesEnumClass['methods'][$actionName]['body'] = $body; |
|
350
|
|
|
|
|
351
|
|
|
//TODO: pensare se qui va bene cosi... potrebbe il ClassGenerator sapere come fare questo costruttore? |
|
|
|
|
|
|
352
|
|
|
$actionName = '__construct'; |
|
353
|
|
|
$propertiesEnumClass['methods'][$actionName] = []; |
|
354
|
|
|
$propertiesEnumClass['methods'][$actionName]['visibility'] = 'private'; |
|
355
|
|
|
$propertiesEnumClass['methods'][$actionName]['params'] = []; |
|
356
|
|
|
$propertiesEnumClass['methods'][$actionName]['static'] = false; |
|
357
|
|
|
$propertiesEnumClass['methods'][$actionName]['description'] = 'costruttore'; |
|
358
|
|
|
$body = '$this->name = \''.$enumClassName.'\';'; |
|
359
|
|
|
$propertiesEnumClass['methods'][$actionName]['body'] = $body; |
|
360
|
|
|
|
|
361
|
|
|
$enumClassComments = 'Child of '.$className.' '.$enumClassName; |
|
362
|
|
|
$g = new ClassGenerator($enumNamespace, $enumClassName, $enumClassComments); |
|
363
|
|
|
$g->setLogger($this->logger); |
|
364
|
|
|
$configEnum = new ClassConfig(); |
|
365
|
|
|
$configEnum->isInterface = false; |
|
366
|
|
|
$configEnum->haveConstructor = true; |
|
367
|
|
|
$configEnum->isFinalClass = true; //don't wnat cycle dependency |
|
368
|
|
|
$configEnum->haveGetter = true; |
|
369
|
|
|
$configEnum->haveSetter = false; |
|
370
|
|
|
$g->generateClassType($propertiesEnumClass, $this->modelClass, $this->modelComments, $configEnum); |
|
371
|
|
|
$g->createFileOnDir($directoryOutput); |
|
372
|
|
|
$generated = true; |
|
|
|
|
|
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
$properties['fields']['name'] = [ |
|
376
|
|
|
'primitive' => 'string', |
|
377
|
|
|
'description' => 'nome esplicativo della enum', |
|
378
|
|
|
'getter' => true, |
|
379
|
|
|
]; |
|
380
|
|
|
|
|
381
|
|
|
$config = new ClassConfig(); |
|
382
|
|
|
$config->isInterface = false; |
|
383
|
|
|
$config->haveConstructor = false; |
|
384
|
|
|
$config->isFinalClass = false; |
|
385
|
|
|
$config->isEnum = true; |
|
386
|
|
|
$config->haveGetter = $createGetter; |
|
387
|
|
|
$config->haveSetter = $createSetter; |
|
388
|
|
|
} else { |
|
389
|
|
|
$config = new ClassConfig(); |
|
390
|
|
|
$config->isInterface = false; |
|
391
|
|
|
$config->haveConstructor = true; |
|
392
|
|
|
$config->isFinalClass = true; //don't wnat cycle dependency |
|
393
|
|
|
$config->haveGetter = $createGetter; |
|
394
|
|
|
$config->haveSetter = $createSetter; |
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
|
|
//NORMAL GENERATION |
|
398
|
|
|
$g = new ClassGenerator($namespace, $className, $classComments); |
|
399
|
|
|
$g->setLogger($this->logger); |
|
400
|
|
|
$g->generateClassType($properties, $this->modelClass, $this->modelComments, $config); |
|
401
|
|
|
$g->createFileOnDir($directoryOutput); |
|
402
|
|
|
$generated = true; |
|
403
|
|
|
} |
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
|
|
if ($generated) { |
|
407
|
|
|
$this->modelClass[$className] = $namespace; |
|
408
|
|
|
$this->modelComments[$className] = $classComments; |
|
409
|
|
|
} |
|
410
|
|
|
} //end class generation |
|
411
|
|
|
} |
|
412
|
|
|
} |
|
413
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.