|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator; |
|
4
|
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Relations\GenerateRelationCodeForEntity; |
|
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
|
7
|
|
|
use gossi\codegen\model\PhpClass; |
|
8
|
|
|
use gossi\codegen\model\PhpInterface; |
|
9
|
|
|
use gossi\codegen\model\PhpTrait; |
|
10
|
|
|
use PhpParser\Error; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class RelationsGenerator |
|
14
|
|
|
* |
|
15
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator |
|
16
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
|
17
|
|
|
*/ |
|
18
|
|
|
class RelationsGenerator extends AbstractGenerator |
|
19
|
|
|
{ |
|
20
|
|
|
public const PREFIX_OWNING = 'Owning'; |
|
21
|
|
|
public const PREFIX_INVERSE = 'Inverse'; |
|
22
|
|
|
public const PREFIX_UNIDIRECTIONAL = 'Unidirectional'; |
|
23
|
|
|
public const PREFIX_REQUIRED = 'Required'; |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/******************************************************************************************************************* |
|
27
|
|
|
* OneToOne - One instance of the current Entity refers to One instance of the referred Entity. |
|
28
|
|
|
*/ |
|
29
|
|
|
public const INTERNAL_TYPE_ONE_TO_ONE = 'OneToOne'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @see codeTemplates/src/Entities/Traits/Relations/TemplateEntity/HasTemplateEntity/HasTemplateEntityOwningOneToOne.php |
|
33
|
|
|
*/ |
|
34
|
|
|
public const HAS_ONE_TO_ONE = self::PREFIX_OWNING . self::INTERNAL_TYPE_ONE_TO_ONE; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @see codeTemplates/src/Entities/Traits/Relations/TemplateEntity/HasRequiredTemplateEntity/HasRequiredTemplateEntityOwningOneToOne.php |
|
38
|
|
|
*/ |
|
39
|
|
|
public const HAS_REQUIRED_ONE_TO_ONE = self::PREFIX_REQUIRED . self::PREFIX_OWNING . self::INTERNAL_TYPE_ONE_TO_ONE; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @see codeTemplates/src/Entities/Traits/Relations/TemplateEntity/HasTemplateEntity/HasTemplateEntityInverseOneToOne.php |
|
43
|
|
|
*/ |
|
44
|
|
|
public const HAS_INVERSE_ONE_TO_ONE = self::PREFIX_INVERSE . self::INTERNAL_TYPE_ONE_TO_ONE; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @see codeTemplates/src/Entities/Traits/Relations/TemplateEntity/HasRequiredTemplateEntity/HasRequiredTemplateEntityInverseOneToOne.php |
|
48
|
|
|
*/ |
|
49
|
|
|
public const HAS_REQUIRED_INVERSE_ONE_TO_ONE = self::PREFIX_REQUIRED . |
|
50
|
|
|
self::PREFIX_INVERSE . |
|
51
|
|
|
self::INTERNAL_TYPE_ONE_TO_ONE; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @see codeTemplates/src/Entities/Traits/Relations/TemplateEntity/HasTemplateEntity/HasTemplateEntityUnidrectionalOneToOne.php |
|
55
|
|
|
*/ |
|
56
|
|
|
public const HAS_UNIDIRECTIONAL_ONE_TO_ONE = self::PREFIX_UNIDIRECTIONAL . self::INTERNAL_TYPE_ONE_TO_ONE; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @see codeTemplates/src/Entities/Traits/Relations/TemplateEntity/HasRequiredTemplateEntity/HasRequiredTemplateEntityUnidrectionalOneToOne.php |
|
60
|
|
|
*/ |
|
61
|
|
|
public const HAS_REQUIRED_UNIDIRECTIONAL_ONE_TO_ONE = self::PREFIX_REQUIRED . |
|
62
|
|
|
self::PREFIX_UNIDIRECTIONAL . |
|
63
|
|
|
self::INTERNAL_TYPE_ONE_TO_ONE; |
|
64
|
|
|
|
|
65
|
|
|
/******************************************************************************************************************* |
|
66
|
|
|
* OneToMany - One instance of the current Entity has Many instances (references) to the referred Entity. |
|
67
|
|
|
*/ |
|
68
|
|
|
public const INTERNAL_TYPE_ONE_TO_MANY = 'OneToMany'; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @see codeTemplates/src/Entities/Traits/Relations/TemplateEntity/HasTemplateEntities/HasTemplateEntitiesOneToMany.php |
|
72
|
|
|
*/ |
|
73
|
|
|
public const HAS_ONE_TO_MANY = self::INTERNAL_TYPE_ONE_TO_MANY; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @see codeTemplates/src/Entities/Traits/Relations/TemplateEntity/HasRequiredTemplateEntities/HasRequiredTemplateEntitiesOneToMany.php |
|
77
|
|
|
*/ |
|
78
|
|
|
public const HAS_REQUIRED_ONE_TO_MANY = self::PREFIX_REQUIRED . self::INTERNAL_TYPE_ONE_TO_MANY; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @see codeTemplates/src/Entities/Traits/Relations/TemplateEntity/HasTemplateEntities/HasTemplateEntitiesOneToMany.php |
|
82
|
|
|
*/ |
|
83
|
|
|
public const HAS_UNIDIRECTIONAL_ONE_TO_MANY = self::PREFIX_UNIDIRECTIONAL . self::INTERNAL_TYPE_ONE_TO_MANY; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @see codeTemplates/src/Entities/Traits/Relations/TemplateEntity/HasRequiredTemplateEntities/HasRequiredTemplateEntitiesOneToMany.php |
|
87
|
|
|
*/ |
|
88
|
|
|
public const HAS_REQUIRED_UNIDIRECTIONAL_ONE_TO_MANY = self::PREFIX_REQUIRED . |
|
89
|
|
|
self::PREFIX_UNIDIRECTIONAL . |
|
90
|
|
|
self::INTERNAL_TYPE_ONE_TO_MANY; |
|
91
|
|
|
|
|
92
|
|
|
/******************************************************************************************************************* |
|
93
|
|
|
* ManyToOne - Many instances of the current Entity refer to One instance of the referred Entity. |
|
94
|
|
|
*/ |
|
95
|
|
|
public const INTERNAL_TYPE_MANY_TO_ONE = 'ManyToOne'; |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @see codeTemplates/src/Entities/Traits/Relations/TemplateEntity/HasTemplateEntity/HasTemplateEntityManyToOne.php |
|
99
|
|
|
*/ |
|
100
|
|
|
public const HAS_MANY_TO_ONE = self::INTERNAL_TYPE_MANY_TO_ONE; |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @see codeTemplates/src/Entities/Traits/Relations/TemplateEntity/HasRequiredTemplateEntity/HasRequiredTemplateEntityManyToOne.php |
|
104
|
|
|
*/ |
|
105
|
|
|
public const HAS_REQUIRED_MANY_TO_ONE = self::PREFIX_REQUIRED . self::INTERNAL_TYPE_MANY_TO_ONE; |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @see codeTemplates/src/Entities/Traits/Relations/TemplateEntity/HasTemplateEntity/HasTemplateEntityManyToOne.php |
|
109
|
|
|
*/ |
|
110
|
|
|
public const HAS_UNIDIRECTIONAL_MANY_TO_ONE = self::PREFIX_UNIDIRECTIONAL . self::INTERNAL_TYPE_MANY_TO_ONE; |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @see codeTemplates/src/Entities/Traits/Relations/TemplateEntity/HasRequiredTemplateEntity/HasRequiredTemplateEntityManyToOne.php |
|
114
|
|
|
*/ |
|
115
|
|
|
public const HAS_REQUIRED_UNIDIRECTIONAL_MANY_TO_ONE = self::PREFIX_REQUIRED . |
|
116
|
|
|
self::PREFIX_UNIDIRECTIONAL . |
|
117
|
|
|
self::INTERNAL_TYPE_MANY_TO_ONE; |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
/******************************************************************************************************************* |
|
121
|
|
|
* ManyToMany - Many instances of the current Entity refer to Many instance of the referred Entity. |
|
122
|
|
|
*/ |
|
123
|
|
|
public const INTERNAL_TYPE_MANY_TO_MANY = 'ManyToMany'; |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @see codeTemplates/src/Entities/Traits/Relations/TemplateEntity/HasTemplateEntities/HasTemplateEntitiesOwningManyToMany.php |
|
127
|
|
|
*/ |
|
128
|
|
|
public const HAS_MANY_TO_MANY = self::PREFIX_OWNING . self::INTERNAL_TYPE_MANY_TO_MANY; |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @see codeTemplates/src/Entities/Traits/Relations/TemplateEntity/HasRequiredTemplateEntities/HasRequiredTemplateEntitiesOwningManyToMany.php |
|
132
|
|
|
*/ |
|
133
|
|
|
public const HAS_REQUIRED_MANY_TO_MANY = self::PREFIX_REQUIRED . |
|
134
|
|
|
self::PREFIX_OWNING . |
|
135
|
|
|
self::INTERNAL_TYPE_MANY_TO_MANY; |
|
136
|
|
|
/** |
|
137
|
|
|
* @see codeTemplates/src/Entities/Traits/Relations/TemplateEntity/HasTemplateEntities/HasTemplateEntitiesInverseManyToMany.php |
|
138
|
|
|
*/ |
|
139
|
|
|
public const HAS_INVERSE_MANY_TO_MANY = self::PREFIX_INVERSE . self::INTERNAL_TYPE_MANY_TO_MANY; |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @see codeTemplates/src/Entities/Traits/Relations/TemplateEntity/HasRequiredTemplateEntities/HasRequiredTemplateEntitiesInverseManyToMany.php |
|
143
|
|
|
*/ |
|
144
|
|
|
public const HAS_REQUIRED_INVERSE_MANY_TO_MANY = self::PREFIX_REQUIRED . |
|
145
|
|
|
self::PREFIX_INVERSE . |
|
146
|
|
|
self::INTERNAL_TYPE_MANY_TO_MANY; |
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* The full list of possible relation types |
|
151
|
|
|
*/ |
|
152
|
|
|
public const HAS_TYPES = [ |
|
153
|
|
|
self::HAS_ONE_TO_ONE, |
|
154
|
|
|
self::HAS_INVERSE_ONE_TO_ONE, |
|
155
|
|
|
self::HAS_UNIDIRECTIONAL_ONE_TO_ONE, |
|
156
|
|
|
self::HAS_ONE_TO_MANY, |
|
157
|
|
|
self::HAS_UNIDIRECTIONAL_ONE_TO_MANY, |
|
158
|
|
|
self::HAS_MANY_TO_ONE, |
|
159
|
|
|
self::HAS_UNIDIRECTIONAL_MANY_TO_ONE, |
|
160
|
|
|
self::HAS_MANY_TO_MANY, |
|
161
|
|
|
self::HAS_INVERSE_MANY_TO_MANY, |
|
162
|
|
|
|
|
163
|
|
|
self::HAS_REQUIRED_ONE_TO_ONE, |
|
164
|
|
|
self::HAS_REQUIRED_INVERSE_ONE_TO_ONE, |
|
165
|
|
|
self::HAS_REQUIRED_UNIDIRECTIONAL_ONE_TO_ONE, |
|
166
|
|
|
self::HAS_REQUIRED_ONE_TO_MANY, |
|
167
|
|
|
self::HAS_REQUIRED_UNIDIRECTIONAL_ONE_TO_MANY, |
|
168
|
|
|
self::HAS_REQUIRED_MANY_TO_ONE, |
|
169
|
|
|
self::HAS_REQUIRED_UNIDIRECTIONAL_MANY_TO_ONE, |
|
170
|
|
|
self::HAS_REQUIRED_MANY_TO_MANY, |
|
171
|
|
|
self::HAS_REQUIRED_INVERSE_MANY_TO_MANY, |
|
172
|
|
|
]; |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Of the full list, which ones will be automatically reciprocated in the generated code |
|
176
|
|
|
*/ |
|
177
|
|
|
public const HAS_TYPES_RECIPROCATED = [ |
|
178
|
|
|
self::HAS_ONE_TO_ONE, |
|
179
|
|
|
self::HAS_INVERSE_ONE_TO_ONE, |
|
180
|
|
|
self::HAS_ONE_TO_MANY, |
|
181
|
|
|
self::HAS_MANY_TO_ONE, |
|
182
|
|
|
self::HAS_MANY_TO_MANY, |
|
183
|
|
|
self::HAS_INVERSE_MANY_TO_MANY, |
|
184
|
|
|
|
|
185
|
|
|
self::HAS_REQUIRED_ONE_TO_ONE, |
|
186
|
|
|
self::HAS_REQUIRED_INVERSE_ONE_TO_ONE, |
|
187
|
|
|
self::HAS_REQUIRED_ONE_TO_MANY, |
|
188
|
|
|
self::HAS_REQUIRED_MANY_TO_ONE, |
|
189
|
|
|
self::HAS_REQUIRED_MANY_TO_MANY, |
|
190
|
|
|
self::HAS_REQUIRED_INVERSE_MANY_TO_MANY, |
|
191
|
|
|
]; |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
*Of the full list, which ones are unidirectional (i.e not reciprocated) |
|
195
|
|
|
*/ |
|
196
|
|
|
public const HAS_TYPES_UNIDIRECTIONAL = [ |
|
197
|
|
|
self::HAS_UNIDIRECTIONAL_MANY_TO_ONE, |
|
198
|
|
|
self::HAS_UNIDIRECTIONAL_ONE_TO_MANY, |
|
199
|
|
|
self::HAS_UNIDIRECTIONAL_ONE_TO_ONE, |
|
200
|
|
|
|
|
201
|
|
|
self::HAS_REQUIRED_UNIDIRECTIONAL_MANY_TO_ONE, |
|
202
|
|
|
self::HAS_REQUIRED_UNIDIRECTIONAL_ONE_TO_MANY, |
|
203
|
|
|
self::HAS_REQUIRED_UNIDIRECTIONAL_ONE_TO_ONE, |
|
204
|
|
|
]; |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Of the full list, which ones are a plural relationship, i.e they have multiple of the related entity |
|
208
|
|
|
*/ |
|
209
|
|
|
public const HAS_TYPES_PLURAL = [ |
|
210
|
|
|
self::HAS_MANY_TO_MANY, |
|
211
|
|
|
self::HAS_INVERSE_MANY_TO_MANY, |
|
212
|
|
|
self::HAS_ONE_TO_MANY, |
|
213
|
|
|
self::HAS_UNIDIRECTIONAL_ONE_TO_MANY, |
|
214
|
|
|
|
|
215
|
|
|
self::HAS_REQUIRED_MANY_TO_MANY, |
|
216
|
|
|
self::HAS_REQUIRED_INVERSE_MANY_TO_MANY, |
|
217
|
|
|
self::HAS_REQUIRED_ONE_TO_MANY, |
|
218
|
|
|
self::HAS_REQUIRED_UNIDIRECTIONAL_ONE_TO_MANY, |
|
219
|
|
|
]; |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Set a relationship from one Entity to Another Entity. |
|
223
|
|
|
* |
|
224
|
|
|
* Also used internally to set the reciprocal side. Uses an undocumented 4th bool parameter to kill recursion. |
|
225
|
|
|
* |
|
226
|
|
|
* @param string $owningEntityFqn |
|
227
|
|
|
* @param string $hasType |
|
228
|
|
|
* @param string $ownedEntityFqn |
|
229
|
|
|
* @param bool $requiredReciprocation |
|
230
|
|
|
* |
|
231
|
|
|
* You should never pass in this parameter, it is only used internally |
|
232
|
|
|
* @param bool $internalUseOnly |
|
233
|
|
|
* |
|
234
|
|
|
* @throws DoctrineStaticMetaException |
|
235
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
|
236
|
|
|
*/ |
|
237
|
2 |
|
public function setEntityHasRelationToEntity( |
|
238
|
|
|
string $owningEntityFqn, |
|
239
|
|
|
string $hasType, |
|
240
|
|
|
string $ownedEntityFqn, |
|
241
|
|
|
bool $requiredReciprocation = false, |
|
242
|
|
|
bool $internalUseOnly = true |
|
243
|
|
|
): void { |
|
244
|
2 |
|
$reciprocate = $internalUseOnly; |
|
245
|
|
|
try { |
|
246
|
2 |
|
$this->validateHasType($hasType); |
|
247
|
|
|
list( |
|
248
|
|
|
$owningTraitPath, |
|
249
|
|
|
$owningInterfacePath, |
|
250
|
|
|
$reciprocatingInterfacePath, |
|
251
|
2 |
|
) = $this->getPathsForOwningTraitsAndInterfaces( |
|
252
|
2 |
|
$hasType, |
|
253
|
2 |
|
$ownedEntityFqn |
|
254
|
|
|
); |
|
255
|
2 |
|
list($owningClass, , $owningClassSubDirs) = $this->parseFullyQualifiedName($owningEntityFqn); |
|
256
|
2 |
|
$owningClassPath = $this->pathHelper->getPathFromNameAndSubDirs( |
|
257
|
2 |
|
$this->pathToProjectRoot, |
|
258
|
2 |
|
$owningClass, |
|
259
|
2 |
|
$owningClassSubDirs |
|
260
|
|
|
); |
|
261
|
2 |
|
$this->useRelationTraitInClass($owningClassPath, $owningTraitPath); |
|
262
|
2 |
|
$this->useRelationInterfaceInEntityInterface($owningClassPath, $owningInterfacePath); |
|
263
|
2 |
|
if (\in_array($hasType, self::HAS_TYPES_RECIPROCATED, true)) { |
|
264
|
2 |
|
$this->useRelationInterfaceInEntityInterface($owningClassPath, $reciprocatingInterfacePath); |
|
265
|
|
|
} |
|
266
|
2 |
|
if (true === $reciprocate && \in_array($hasType, self::HAS_TYPES_RECIPROCATED, true)) { |
|
267
|
2 |
|
$inverseType = $this->getInverseHasType($hasType); |
|
268
|
2 |
|
$inverseType = $this->updateHasTypeForPossibleRequired($inverseType, $requiredReciprocation); |
|
269
|
2 |
|
$this->setEntityHasRelationToEntity( |
|
270
|
2 |
|
$ownedEntityFqn, |
|
271
|
2 |
|
$inverseType, |
|
272
|
2 |
|
$owningEntityFqn, |
|
273
|
|
|
/** |
|
274
|
|
|
* Setting required reciprocation to false, |
|
275
|
|
|
* actually it is irrelevant because reciprocation is disabled |
|
276
|
|
|
*/ |
|
277
|
2 |
|
false, |
|
278
|
2 |
|
false |
|
279
|
|
|
); |
|
280
|
|
|
} |
|
281
|
|
|
} catch (\Exception $e) { |
|
282
|
|
|
throw new DoctrineStaticMetaException( |
|
283
|
|
|
'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
|
284
|
|
|
$e->getCode(), |
|
285
|
|
|
$e |
|
286
|
|
|
); |
|
287
|
|
|
} |
|
288
|
2 |
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* @param string $hasType |
|
292
|
|
|
* |
|
293
|
|
|
* @throws \InvalidArgumentException |
|
294
|
|
|
*/ |
|
295
|
2 |
|
protected function validateHasType(string $hasType): void |
|
296
|
|
|
{ |
|
297
|
2 |
|
if (!\in_array($hasType, static::HAS_TYPES, true)) { |
|
298
|
|
|
throw new \InvalidArgumentException( |
|
299
|
|
|
'Invalid $hasType ' . $hasType . ', must be one of: ' |
|
300
|
|
|
. \print_r(static::HAS_TYPES, true) |
|
301
|
|
|
); |
|
302
|
|
|
} |
|
303
|
2 |
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* Get the absolute paths for the owning traits and interfaces for the specified relation type |
|
307
|
|
|
* Will ensure that the files exists |
|
308
|
|
|
* |
|
309
|
|
|
* @param string $hasType |
|
310
|
|
|
* @param string $ownedEntityFqn |
|
311
|
|
|
* |
|
312
|
|
|
* @return array [ |
|
313
|
|
|
* $owningTraitPath, |
|
314
|
|
|
* $owningInterfacePath, |
|
315
|
|
|
* $reciprocatingInterfacePath |
|
316
|
|
|
* ] |
|
317
|
|
|
* @throws DoctrineStaticMetaException |
|
318
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
319
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
|
320
|
|
|
*/ |
|
321
|
2 |
|
protected function getPathsForOwningTraitsAndInterfaces( |
|
322
|
|
|
string $hasType, |
|
323
|
|
|
string $ownedEntityFqn |
|
324
|
|
|
): array { |
|
325
|
|
|
try { |
|
326
|
2 |
|
$ownedHasName = $this->namespaceHelper->getOwnedHasName( |
|
327
|
2 |
|
$hasType, |
|
328
|
2 |
|
$ownedEntityFqn, |
|
329
|
2 |
|
$this->srcSubFolderName, |
|
330
|
2 |
|
$this->projectRootNamespace |
|
331
|
|
|
); |
|
332
|
2 |
|
$reciprocatedHasName = $this->namespaceHelper->getReciprocatedHasName( |
|
333
|
2 |
|
$ownedEntityFqn, |
|
334
|
2 |
|
$this->srcSubFolderName, |
|
335
|
2 |
|
$this->projectRootNamespace |
|
336
|
|
|
); |
|
337
|
2 |
|
$owningTraitFqn = $this->getOwningTraitFqn($hasType, $ownedEntityFqn); |
|
338
|
2 |
|
list($traitName, , $traitSubDirsNoEntities) = $this->parseFullyQualifiedName($owningTraitFqn); |
|
339
|
2 |
|
$owningTraitPath = $this->pathHelper->getPathFromNameAndSubDirs( |
|
340
|
2 |
|
$this->pathToProjectRoot, |
|
341
|
2 |
|
$traitName, |
|
342
|
2 |
|
$traitSubDirsNoEntities |
|
343
|
|
|
); |
|
344
|
2 |
|
if (!\file_exists($owningTraitPath)) { |
|
345
|
|
|
$this->generateRelationCodeForEntity($ownedEntityFqn); |
|
346
|
|
|
} |
|
347
|
2 |
|
$owningInterfaceFqn = $this->getOwningInterfaceFqn($hasType, $ownedEntityFqn); |
|
348
|
2 |
|
list($interfaceName, , $interfaceSubDirsNoEntities) = $this->parseFullyQualifiedName($owningInterfaceFqn); |
|
349
|
2 |
|
$owningInterfacePath = $this->pathHelper->getPathFromNameAndSubDirs( |
|
350
|
2 |
|
$this->pathToProjectRoot, |
|
351
|
2 |
|
$interfaceName, |
|
352
|
2 |
|
$interfaceSubDirsNoEntities |
|
353
|
|
|
); |
|
354
|
2 |
|
$reciprocatingInterfacePath = \preg_replace( |
|
355
|
2 |
|
'%Has(Required|)' . $ownedHasName . '%', |
|
356
|
2 |
|
'Reciprocates' . $reciprocatedHasName, |
|
357
|
2 |
|
$owningInterfacePath |
|
358
|
|
|
); |
|
359
|
|
|
|
|
360
|
|
|
return [ |
|
361
|
2 |
|
$owningTraitPath, |
|
362
|
2 |
|
$owningInterfacePath, |
|
363
|
2 |
|
$reciprocatingInterfacePath, |
|
364
|
|
|
]; |
|
365
|
|
|
} catch (\Exception $e) { |
|
366
|
|
|
throw new DoctrineStaticMetaException( |
|
367
|
|
|
'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
|
368
|
|
|
$e->getCode(), |
|
369
|
|
|
$e |
|
370
|
|
|
); |
|
371
|
|
|
} |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
/** |
|
375
|
|
|
* @param string $hasType |
|
376
|
|
|
* @param string $ownedEntityFqn |
|
377
|
|
|
* |
|
378
|
|
|
* @return string |
|
379
|
|
|
* @throws DoctrineStaticMetaException |
|
380
|
|
|
*/ |
|
381
|
2 |
|
public function getOwningTraitFqn(string $hasType, string $ownedEntityFqn): string |
|
382
|
|
|
{ |
|
383
|
2 |
|
return $this->namespaceHelper->getOwningTraitFqn( |
|
384
|
2 |
|
$hasType, |
|
385
|
2 |
|
$ownedEntityFqn, |
|
386
|
2 |
|
$this->projectRootNamespace, |
|
387
|
2 |
|
$this->srcSubFolderName |
|
388
|
|
|
); |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
/** |
|
392
|
|
|
* Generate the relation traits for specified Entity |
|
393
|
|
|
* |
|
394
|
|
|
* This works by copying the template traits folder over and then updating the file contents, name and path |
|
395
|
|
|
* |
|
396
|
|
|
* @param string $entityFqn Fully Qualified Name of Entity |
|
397
|
|
|
* |
|
398
|
|
|
* @throws DoctrineStaticMetaException |
|
399
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
400
|
|
|
*/ |
|
401
|
|
|
public function generateRelationCodeForEntity(string $entityFqn): void |
|
402
|
|
|
{ |
|
403
|
|
|
$invokable = new GenerateRelationCodeForEntity( |
|
404
|
|
|
$entityFqn, |
|
405
|
|
|
$this->pathToProjectRoot, |
|
406
|
|
|
$this->projectRootNamespace, |
|
407
|
|
|
$this->srcSubFolderName, |
|
408
|
|
|
$this->namespaceHelper, |
|
409
|
|
|
$this->pathHelper, |
|
410
|
|
|
$this->findAndReplaceHelper |
|
411
|
|
|
); |
|
412
|
|
|
$invokable($this->getRelativePathRelationsGenerator()); |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
|
|
/** |
|
416
|
|
|
* Generator that yields relative paths of all the files in the relations template path and the SplFileInfo objects |
|
417
|
|
|
* |
|
418
|
|
|
* Use a PHP Generator to iterate over a recursive iterator iterator and then yield: |
|
419
|
|
|
* - key: string $relativePath |
|
420
|
|
|
* - value: \SplFileInfo $fileInfo |
|
421
|
|
|
* |
|
422
|
|
|
* The `finally` step unsets the recursiveIterator once everything is done |
|
423
|
|
|
* |
|
424
|
|
|
* @return \Generator |
|
425
|
|
|
*/ |
|
426
|
2 |
|
public function getRelativePathRelationsGenerator(): \Generator |
|
427
|
|
|
{ |
|
428
|
|
|
try { |
|
429
|
2 |
|
$recursiveIterator = new \RecursiveIteratorIterator( |
|
430
|
2 |
|
new \RecursiveDirectoryIterator( |
|
431
|
2 |
|
\realpath(AbstractGenerator::RELATIONS_TEMPLATE_PATH), |
|
432
|
2 |
|
\RecursiveDirectoryIterator::SKIP_DOTS |
|
433
|
|
|
), |
|
434
|
2 |
|
\RecursiveIteratorIterator::SELF_FIRST |
|
435
|
|
|
); |
|
436
|
2 |
|
foreach ($recursiveIterator as $path => $fileInfo) { |
|
437
|
2 |
|
$relativePath = rtrim( |
|
438
|
2 |
|
$this->getFilesystem()->makePathRelative( |
|
439
|
2 |
|
$path, |
|
440
|
2 |
|
\realpath(AbstractGenerator::RELATIONS_TEMPLATE_PATH) |
|
441
|
|
|
), |
|
442
|
2 |
|
'/' |
|
443
|
|
|
); |
|
444
|
2 |
|
yield $relativePath => $fileInfo; |
|
445
|
|
|
} |
|
446
|
2 |
|
} finally { |
|
447
|
2 |
|
$recursiveIterator = null; |
|
448
|
2 |
|
unset($recursiveIterator); |
|
449
|
|
|
} |
|
450
|
2 |
|
} |
|
451
|
|
|
|
|
452
|
|
|
/** |
|
453
|
|
|
* @param string $hasType |
|
454
|
|
|
* @param string $ownedEntityFqn |
|
455
|
|
|
* |
|
456
|
|
|
* @return string |
|
457
|
|
|
* @throws DoctrineStaticMetaException |
|
458
|
|
|
*/ |
|
459
|
2 |
|
public function getOwningInterfaceFqn(string $hasType, string $ownedEntityFqn): string |
|
460
|
|
|
{ |
|
461
|
2 |
|
return $this->namespaceHelper->getOwningInterfaceFqn( |
|
462
|
2 |
|
$hasType, |
|
463
|
2 |
|
$ownedEntityFqn, |
|
464
|
2 |
|
$this->projectRootNamespace, |
|
465
|
2 |
|
$this->srcSubFolderName |
|
466
|
|
|
); |
|
467
|
|
|
} |
|
468
|
|
|
|
|
469
|
|
|
/** |
|
470
|
|
|
* Add the specified trait to the specified class |
|
471
|
|
|
* |
|
472
|
|
|
* @param string $classPath |
|
473
|
|
|
* @param string $traitPath |
|
474
|
|
|
* |
|
475
|
|
|
* @throws DoctrineStaticMetaException |
|
476
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
477
|
|
|
*/ |
|
478
|
2 |
|
protected function useRelationTraitInClass(string $classPath, string $traitPath): void |
|
479
|
|
|
{ |
|
480
|
|
|
try { |
|
481
|
2 |
|
$class = PhpClass::fromFile($classPath); |
|
482
|
|
|
} catch (Error $e) { |
|
483
|
|
|
throw new DoctrineStaticMetaException( |
|
484
|
|
|
'PHP parsing error when loading class ' . $classPath . ': ' . $e->getMessage(), |
|
485
|
|
|
$e->getCode(), |
|
486
|
|
|
$e |
|
487
|
|
|
); |
|
488
|
|
|
} |
|
489
|
|
|
try { |
|
490
|
2 |
|
$trait = PhpTrait::fromFile($traitPath); |
|
491
|
|
|
} catch (Error $e) { |
|
492
|
|
|
throw new DoctrineStaticMetaException( |
|
493
|
|
|
'PHP parsing error when loading class ' . $classPath . ': ' . $e->getMessage(), |
|
494
|
|
|
$e->getCode(), |
|
495
|
|
|
$e |
|
496
|
|
|
); |
|
497
|
|
|
} |
|
498
|
2 |
|
$class->addTrait($trait); |
|
499
|
2 |
|
$this->codeHelper->generate($class, $classPath); |
|
500
|
2 |
|
} |
|
501
|
|
|
|
|
502
|
|
|
/** |
|
503
|
|
|
* Add the specified interface to the specified entity interface |
|
504
|
|
|
* |
|
505
|
|
|
* @param string $classPath |
|
506
|
|
|
* @param string $interfacePath |
|
507
|
|
|
* |
|
508
|
|
|
* @throws \ReflectionException |
|
509
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
510
|
|
|
*/ |
|
511
|
2 |
|
protected function useRelationInterfaceInEntityInterface(string $classPath, string $interfacePath): void |
|
512
|
|
|
{ |
|
513
|
2 |
|
$entityFqn = PhpClass::fromFile($classPath)->getQualifiedName(); |
|
514
|
2 |
|
$entityInterfaceFqn = $this->namespaceHelper->getEntityInterfaceFromEntityFqn($entityFqn); |
|
515
|
2 |
|
$entityInterfacePath = (new \ts\Reflection\ReflectionClass($entityInterfaceFqn))->getFileName(); |
|
516
|
2 |
|
$entityInterface = PhpInterface::fromFile($entityInterfacePath); |
|
517
|
2 |
|
$relationInterface = PhpInterface::fromFile($interfacePath); |
|
518
|
2 |
|
$entityInterface->addInterface($relationInterface); |
|
519
|
2 |
|
$this->codeHelper->generate($entityInterface, $entityInterfacePath); |
|
520
|
2 |
|
} |
|
521
|
|
|
|
|
522
|
|
|
/** |
|
523
|
|
|
* Get the inverse of a hasType |
|
524
|
|
|
* |
|
525
|
|
|
* @param string $hasType |
|
526
|
|
|
* |
|
527
|
|
|
* @return string |
|
528
|
|
|
* @throws DoctrineStaticMetaException |
|
529
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
|
530
|
|
|
*/ |
|
531
|
2 |
|
protected function getInverseHasType(string $hasType): string |
|
532
|
|
|
{ |
|
533
|
|
|
switch ($hasType) { |
|
534
|
2 |
|
case self::HAS_ONE_TO_ONE: |
|
535
|
2 |
|
case self::HAS_REQUIRED_ONE_TO_ONE: |
|
536
|
2 |
|
case self::HAS_MANY_TO_MANY: |
|
537
|
2 |
|
case self::HAS_REQUIRED_MANY_TO_MANY: |
|
538
|
2 |
|
return \str_replace( |
|
539
|
2 |
|
self::PREFIX_OWNING, |
|
540
|
2 |
|
self::PREFIX_INVERSE, |
|
541
|
2 |
|
$hasType |
|
542
|
|
|
); |
|
543
|
|
|
|
|
544
|
2 |
|
case self::HAS_INVERSE_ONE_TO_ONE: |
|
545
|
2 |
|
case self::HAS_REQUIRED_INVERSE_ONE_TO_ONE: |
|
546
|
2 |
|
case self::HAS_INVERSE_MANY_TO_MANY: |
|
547
|
2 |
|
case self::HAS_REQUIRED_INVERSE_MANY_TO_MANY: |
|
548
|
|
|
return \str_replace( |
|
549
|
|
|
self::PREFIX_INVERSE, |
|
550
|
|
|
self::PREFIX_OWNING, |
|
551
|
|
|
$hasType |
|
552
|
|
|
); |
|
553
|
|
|
|
|
554
|
2 |
|
case self::HAS_MANY_TO_ONE: |
|
555
|
2 |
|
return self::HAS_ONE_TO_MANY; |
|
556
|
|
|
|
|
557
|
2 |
|
case self::HAS_REQUIRED_MANY_TO_ONE: |
|
558
|
2 |
|
return self::HAS_REQUIRED_ONE_TO_MANY; |
|
559
|
|
|
|
|
560
|
2 |
|
case self::HAS_ONE_TO_MANY: |
|
561
|
2 |
|
return self::HAS_MANY_TO_ONE; |
|
562
|
|
|
|
|
563
|
2 |
|
case self::HAS_REQUIRED_ONE_TO_MANY: |
|
564
|
2 |
|
return self::HAS_REQUIRED_MANY_TO_ONE; |
|
565
|
|
|
|
|
566
|
|
|
default: |
|
567
|
|
|
throw new DoctrineStaticMetaException( |
|
568
|
|
|
'invalid $hasType ' . $hasType . ' when trying to get the inverted relation' |
|
569
|
|
|
); |
|
570
|
|
|
} |
|
571
|
|
|
} |
|
572
|
|
|
|
|
573
|
|
|
/** |
|
574
|
|
|
* Take a relationship and a possibility of being required and ensure it is set as the correct relationship |
|
575
|
|
|
* |
|
576
|
|
|
* @param string $relation |
|
577
|
|
|
* @param bool $required |
|
578
|
|
|
* |
|
579
|
|
|
* @return string |
|
580
|
|
|
*/ |
|
581
|
2 |
|
private function updateHasTypeForPossibleRequired(string $relation, bool $required): string |
|
582
|
|
|
{ |
|
583
|
2 |
|
$inverseIsRequired = \ts\stringContains($relation, self::PREFIX_REQUIRED); |
|
584
|
2 |
|
if (false === $required) { |
|
585
|
2 |
|
if (false === $inverseIsRequired) { |
|
|
|
|
|
|
586
|
2 |
|
return $relation; |
|
587
|
|
|
} |
|
588
|
|
|
|
|
589
|
2 |
|
return $this->removeRequiredToRelation($relation); |
|
590
|
|
|
} |
|
591
|
2 |
|
if (true === $required) { |
|
|
|
|
|
|
592
|
2 |
|
if (true === $inverseIsRequired) { |
|
|
|
|
|
|
593
|
2 |
|
return $relation; |
|
594
|
|
|
} |
|
595
|
|
|
|
|
596
|
2 |
|
return $this->addRequiredToRelation($relation); |
|
597
|
|
|
} |
|
|
|
|
|
|
598
|
|
|
} |
|
599
|
|
|
|
|
600
|
2 |
|
private function removeRequiredToRelation(string $relation): string |
|
601
|
|
|
{ |
|
602
|
2 |
|
if (0 !== strpos($relation, self::PREFIX_REQUIRED)) { |
|
603
|
|
|
throw new \RuntimeException('Trying to remove the Required prefix, but it is not set: ' . $relation); |
|
604
|
|
|
} |
|
605
|
|
|
|
|
606
|
2 |
|
return substr($relation, 8); |
|
607
|
|
|
} |
|
608
|
|
|
|
|
609
|
2 |
|
private function addRequiredToRelation(string $relation): string |
|
610
|
|
|
{ |
|
611
|
2 |
|
if (0 === strpos($relation, self::PREFIX_REQUIRED)) { |
|
612
|
|
|
throw new \RuntimeException('Trying to add the Required prefix, but it is already set: ' . $relation); |
|
613
|
|
|
} |
|
614
|
|
|
|
|
615
|
2 |
|
return self::PREFIX_REQUIRED . $relation; |
|
616
|
|
|
} |
|
617
|
|
|
} |
|
618
|
|
|
|