@@ -45,7 +45,6 @@ class AutowirePass extends AbstractRecursivePass
45
45
private $ decoratedMethodIndex ;
46
46
private $ decoratedMethodArgumentIndex ;
47
47
private $ typesClone ;
48
- private $ combinedAliases ;
49
48
50
49
public function __construct (bool $ throwOnAutowireException = true )
51
50
{
@@ -61,8 +60,6 @@ public function __construct(bool $throwOnAutowireException = true)
61
60
*/
62
61
public function process (ContainerBuilder $ container )
63
62
{
64
- $ this ->populateCombinedAliases ($ container );
65
-
66
63
try {
67
64
$ this ->typesClone = clone $ this ;
68
65
parent ::process ($ container );
@@ -75,7 +72,6 @@ public function process(ContainerBuilder $container)
75
72
$ this ->decoratedMethodIndex = null ;
76
73
$ this ->decoratedMethodArgumentIndex = null ;
77
74
$ this ->typesClone = null ;
78
- $ this ->combinedAliases = [];
79
75
}
80
76
}
81
77
@@ -371,12 +367,12 @@ private function getAutowiredReference(TypedReference $reference, bool $filterTy
371
367
return new TypedReference ($ alias , $ type , $ reference ->getInvalidBehavior ());
372
368
}
373
369
374
- if (null !== ($ alias = $ this ->combinedAliases [ $ alias ] ?? null ) && !$ this ->container ->findDefinition ($ alias )->isAbstract ()) {
370
+ if (null !== ($ alias = $ this ->getCombinedAlias ( $ type , $ name ) ?? null ) && !$ this ->container ->findDefinition ($ alias )->isAbstract ()) {
375
371
return new TypedReference ($ alias , $ type , $ reference ->getInvalidBehavior ());
376
372
}
377
373
378
374
if ($ this ->container ->has ($ name ) && !$ this ->container ->findDefinition ($ name )->isAbstract ()) {
379
- foreach ($ this ->container ->getAliases () + $ this -> combinedAliases as $ id => $ alias ) {
375
+ foreach ($ this ->container ->getAliases () as $ id => $ alias ) {
380
376
if ($ name === (string ) $ alias && str_starts_with ($ id , $ type .' $ ' )) {
381
377
return new TypedReference ($ name , $ type , $ reference ->getInvalidBehavior ());
382
378
}
@@ -388,7 +384,7 @@ private function getAutowiredReference(TypedReference $reference, bool $filterTy
388
384
return new TypedReference ($ type , $ type , $ reference ->getInvalidBehavior ());
389
385
}
390
386
391
- if (null !== ($ alias = $ this ->combinedAliases [ $ type] ?? null ) && !$ this ->container ->findDefinition ($ alias )->isAbstract ()) {
387
+ if (null !== ($ alias = $ this ->getCombinedAlias ( $ type) ?? null ) && !$ this ->container ->findDefinition ($ alias )->isAbstract ()) {
392
388
return new TypedReference ($ alias , $ type , $ reference ->getInvalidBehavior ());
393
389
}
394
390
@@ -582,44 +578,31 @@ private function populateAutowiringAlias(string $id): void
582
578
}
583
579
}
584
580
585
- private function populateCombinedAliases ( ContainerBuilder $ container ): void
581
+ private function getCombinedAlias ( string $ type , string $ name = null ): ? string
586
582
{
587
- $ this ->combinedAliases = [];
588
- $ reverseAliases = [];
589
-
590
- foreach ($ container ->getAliases () as $ id => $ alias ) {
591
- if (!preg_match ('/(?(DEFINE)(?<V>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+))^((?&V)(?: \\\\(?&V))*+)(?: \$((?&V)))?$/ ' , $ id , $ m )) {
592
- continue ;
593
- }
594
-
595
- $ type = $ m [2 ];
596
- $ name = $ m [3 ] ?? '' ;
597
- $ reverseAliases [(string ) $ alias ][$ name ][] = $ type ;
583
+ if (str_contains ($ type , '& ' )) {
584
+ $ types = explode ('& ' , $ type );
585
+ } elseif (str_contains ($ type , '| ' )) {
586
+ $ types = explode ('| ' , $ type );
587
+ } else {
588
+ return null ;
598
589
}
599
590
600
- foreach ($ reverseAliases as $ alias => $ names ) {
601
- foreach ($ names as $ name => $ types ) {
602
- if (2 > $ count = \count ($ types )) {
603
- continue ;
604
- }
605
- sort ($ types );
606
- $ i = 1 << $ count ;
607
-
608
- // compute the powerset of the list of types
609
- while ($ i --) {
610
- $ set = [];
611
- for ($ j = 0 ; $ j < $ count ; ++$ j ) {
612
- if ($ i & (1 << $ j )) {
613
- $ set [] = $ types [$ j ];
614
- }
615
- }
591
+ $ alias = null ;
592
+ $ suffix = $ name ? ' $ ' .$ name : '' ;
616
593
617
- if (2 <= \count ($ set )) {
618
- $ this ->combinedAliases [implode ('& ' , $ set ).('' === $ name ? '' : ' $ ' .$ name )] = $ alias ;
619
- $ this ->combinedAliases [implode ('| ' , $ set ).('' === $ name ? '' : ' $ ' .$ name )] = $ alias ;
620
- }
621
- }
594
+ foreach ($ types as $ type ) {
595
+ if (!$ this ->container ->hasAlias ($ type .$ suffix )) {
596
+ return null ;
597
+ }
598
+
599
+ if (null === $ alias ) {
600
+ $ alias = (string ) $ this ->container ->getAlias ($ type .$ suffix );
601
+ } elseif ((string ) $ this ->container ->getAlias ($ type .$ suffix ) !== $ alias ) {
602
+ return null ;
622
603
}
623
604
}
605
+
606
+ return $ alias ;
624
607
}
625
608
}
0 commit comments