@@ -110,6 +110,18 @@ class BeanDescriptor implements BeanDescriptorInterface
110
110
* @var BeanRegistry
111
111
*/
112
112
private $ registry ;
113
+ /**
114
+ * @var MethodDescriptorInterface[][]
115
+ */
116
+ private $ descriptorsByMethodName = [];
117
+ /**
118
+ * @var DirectForeignKeyMethodDescriptor[]|null
119
+ */
120
+ private $ directForeignKeysDescriptors = null ;
121
+ /**
122
+ * @var PivotTableMethodsDescriptor[]|null
123
+ */
124
+ private $ pivotTableDescriptors = null ;
113
125
114
126
public function __construct (
115
127
Table $ table ,
@@ -144,6 +156,11 @@ public function __construct(
144
156
public function initBeanPropertyDescriptors (): void
145
157
{
146
158
$ this ->beanPropertyDescriptors = $ this ->getProperties ($ this ->table );
159
+
160
+ //init the list of method names with regular properties names
161
+ foreach ($ this ->beanPropertyDescriptors as $ beanPropertyDescriptor ) {
162
+ $ this ->checkForDuplicate ($ beanPropertyDescriptor );
163
+ }
147
164
}
148
165
149
166
/**
@@ -373,68 +390,82 @@ private function generateBeanConstructor() : MethodGenerator
373
390
*/
374
391
private function getDirectForeignKeysDescriptors (): array
375
392
{
393
+ if ($ this ->directForeignKeysDescriptors !== null ) {
394
+ return $ this ->directForeignKeysDescriptors ;
395
+ }
376
396
$ fks = $ this ->tdbmSchemaAnalyzer ->getIncomingForeignKeys ($ this ->table ->getName ());
377
397
378
398
$ descriptors = [];
379
399
380
400
foreach ($ fks as $ fk ) {
381
- $ descriptors [] = new DirectForeignKeyMethodDescriptor ($ fk , $ this ->table , $ this ->namingStrategy , $ this ->annotationParser , $ this ->beanNamespace );
401
+ $ desc = new DirectForeignKeyMethodDescriptor ($ fk , $ this ->table , $ this ->namingStrategy , $ this ->annotationParser , $ this ->beanNamespace );
402
+ $ this ->checkForDuplicate ($ desc );
403
+ $ descriptors [] = $ desc ;
382
404
}
383
405
384
- return $ descriptors ;
406
+ $ this ->directForeignKeysDescriptors = $ descriptors ;
407
+ return $ this ->directForeignKeysDescriptors ;
385
408
}
386
409
387
410
/**
388
411
* @return PivotTableMethodsDescriptor[]
389
412
*/
390
413
private function getPivotTableDescriptors (): array
391
414
{
415
+ if ($ this ->pivotTableDescriptors !== null ) {
416
+ return $ this ->pivotTableDescriptors ;
417
+ }
392
418
$ descs = [];
393
419
foreach ($ this ->schemaAnalyzer ->detectJunctionTables (true ) as $ table ) {
394
420
// There are exactly 2 FKs since this is a pivot table.
395
421
$ fks = array_values ($ table ->getForeignKeys ());
396
422
397
423
if ($ fks [0 ]->getForeignTableName () === $ this ->table ->getName ()) {
398
424
list ($ localFk , $ remoteFk ) = $ fks ;
399
- $ descs [] = new PivotTableMethodsDescriptor ($ table , $ localFk , $ remoteFk , $ this ->namingStrategy , $ this ->beanNamespace , $ this ->annotationParser );
425
+ $ desc = new PivotTableMethodsDescriptor ($ table , $ localFk , $ remoteFk , $ this ->namingStrategy , $ this ->beanNamespace , $ this ->annotationParser );
426
+ $ this ->checkForDuplicate ($ desc );
427
+ $ descs [] = $ desc ;
400
428
}
401
429
if ($ fks [1 ]->getForeignTableName () === $ this ->table ->getName ()) {
402
430
list ($ remoteFk , $ localFk ) = $ fks ;
403
- $ descs [] = new PivotTableMethodsDescriptor ($ table , $ localFk , $ remoteFk , $ this ->namingStrategy , $ this ->beanNamespace , $ this ->annotationParser );
431
+ $ desc = new PivotTableMethodsDescriptor ($ table , $ localFk , $ remoteFk , $ this ->namingStrategy , $ this ->beanNamespace , $ this ->annotationParser );
432
+ $ this ->checkForDuplicate ($ desc );
433
+ $ descs [] = $ desc ;
404
434
}
405
435
}
406
436
407
- return $ descs ;
437
+ $ this ->pivotTableDescriptors = $ descs ;
438
+ return $ this ->pivotTableDescriptors ;
439
+ }
440
+
441
+ /**
442
+ * Check the method name isn't already used and flag the associated descriptors to use their alternative names if it is the case
443
+ */
444
+ private function checkForDuplicate (MethodDescriptorInterface $ descriptor ): void
445
+ {
446
+ $ name = $ descriptor ->getName ();
447
+ if (!isset ($ this ->descriptorsByMethodName [$ name ])) {
448
+ $ this ->descriptorsByMethodName [$ name ] = [];
449
+ }
450
+ $ this ->descriptorsByMethodName [$ name ][] = $ descriptor ;
451
+ if (count ($ this ->descriptorsByMethodName [$ name ]) > 1 ) {
452
+ foreach ($ this ->descriptorsByMethodName [$ name ] as $ duplicateDescriptor ) {
453
+ $ duplicateDescriptor ->useAlternativeName ();
454
+ }
455
+ }
408
456
}
409
457
410
458
/**
411
459
* Returns the list of method descriptors (and applies the alternative name if needed).
412
460
*
413
- * @return MethodDescriptorInterface []
461
+ * @return RelationshipMethodDescriptorInterface []
414
462
*/
415
463
public function getMethodDescriptors (): array
416
464
{
417
465
$ directForeignKeyDescriptors = $ this ->getDirectForeignKeysDescriptors ();
418
466
$ pivotTableDescriptors = $ this ->getPivotTableDescriptors ();
419
467
420
- $ descriptors = array_merge ($ directForeignKeyDescriptors , $ pivotTableDescriptors );
421
-
422
- // Descriptors by method names
423
- $ descriptorsByMethodName = [];
424
-
425
- foreach ($ descriptors as $ descriptor ) {
426
- $ descriptorsByMethodName [$ descriptor ->getName ()][] = $ descriptor ;
427
- }
428
-
429
- foreach ($ descriptorsByMethodName as $ descriptorsForMethodName ) {
430
- if (count ($ descriptorsForMethodName ) > 1 ) {
431
- foreach ($ descriptorsForMethodName as $ descriptor ) {
432
- $ descriptor ->useAlternativeName ();
433
- }
434
- }
435
- }
436
-
437
- return $ descriptors ;
468
+ return array_merge ($ directForeignKeyDescriptors , $ pivotTableDescriptors );
438
469
}
439
470
440
471
public function generateJsonSerialize (): MethodGenerator
0 commit comments