@@ -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
@@ -375,12 +371,12 @@ private function getAutowiredReference(TypedReference $reference, bool $filterTy
375
371
return new TypedReference ($ alias , $ type , $ reference ->getInvalidBehavior ());
376
372
}
377
373
378
- if (null !== ($ alias = $ this ->combinedAliases [ $ alias ] ?? null ) && !$ this ->container ->findDefinition ($ alias )->isAbstract ()) {
374
+ if (null !== ($ alias = $ this ->getCombinedAlias ( $ type , $ name ) ?? null ) && !$ this ->container ->findDefinition ($ alias )->isAbstract ()) {
379
375
return new TypedReference ($ alias , $ type , $ reference ->getInvalidBehavior ());
380
376
}
381
377
382
378
if ($ this ->container ->has ($ name ) && !$ this ->container ->findDefinition ($ name )->isAbstract ()) {
383
- foreach ($ this ->container ->getAliases () + $ this -> combinedAliases as $ id => $ alias ) {
379
+ foreach ($ this ->container ->getAliases () as $ id => $ alias ) {
384
380
if ($ name === (string ) $ alias && str_starts_with ($ id , $ type .' $ ' )) {
385
381
return new TypedReference ($ name , $ type , $ reference ->getInvalidBehavior ());
386
382
}
@@ -392,7 +388,7 @@ private function getAutowiredReference(TypedReference $reference, bool $filterTy
392
388
return new TypedReference ($ type , $ type , $ reference ->getInvalidBehavior ());
393
389
}
394
390
395
- if (null !== ($ alias = $ this ->combinedAliases [ $ type] ?? null ) && !$ this ->container ->findDefinition ($ alias )->isAbstract ()) {
391
+ if (null !== ($ alias = $ this ->getCombinedAlias ( $ type) ?? null ) && !$ this ->container ->findDefinition ($ alias )->isAbstract ()) {
396
392
return new TypedReference ($ alias , $ type , $ reference ->getInvalidBehavior ());
397
393
}
398
394
@@ -586,44 +582,31 @@ private function populateAutowiringAlias(string $id): void
586
582
}
587
583
}
588
584
589
- private function populateCombinedAliases ( ContainerBuilder $ container ): void
585
+ private function getCombinedAlias ( string $ type , string $ name = null ): ? string
590
586
{
591
- $ this ->combinedAliases = [];
592
- $ reverseAliases = [];
593
-
594
- foreach ($ container ->getAliases () as $ id => $ alias ) {
595
- if (!preg_match ('/(?(DEFINE)(?<V>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+))^((?&V)(?: \\\\(?&V))*+)(?: \$((?&V)))?$/ ' , $ id , $ m )) {
596
- continue ;
597
- }
598
-
599
- $ type = $ m [2 ];
600
- $ name = $ m [3 ] ?? '' ;
601
- $ reverseAliases [(string ) $ alias ][$ name ][] = $ type ;
587
+ if (str_contains ($ type , '& ' )) {
588
+ $ types = explode ('& ' , $ type );
589
+ } elseif (str_contains ($ type , '| ' )) {
590
+ $ types = explode ('| ' , $ type );
591
+ } else {
592
+ return null ;
602
593
}
603
594
604
- foreach ($ reverseAliases as $ alias => $ names ) {
605
- foreach ($ names as $ name => $ types ) {
606
- if (2 > $ count = \count ($ types )) {
607
- continue ;
608
- }
609
- sort ($ types );
610
- $ i = 1 << $ count ;
611
-
612
- // compute the powerset of the list of types
613
- while ($ i --) {
614
- $ set = [];
615
- for ($ j = 0 ; $ j < $ count ; ++$ j ) {
616
- if ($ i & (1 << $ j )) {
617
- $ set [] = $ types [$ j ];
618
- }
619
- }
595
+ $ alias = null ;
596
+ $ suffix = $ name ? ' $ ' .$ name : '' ;
620
597
621
- if (2 <= \count ($ set )) {
622
- $ this ->combinedAliases [implode ('& ' , $ set ).('' === $ name ? '' : ' $ ' .$ name )] = $ alias ;
623
- $ this ->combinedAliases [implode ('| ' , $ set ).('' === $ name ? '' : ' $ ' .$ name )] = $ alias ;
624
- }
625
- }
598
+ foreach ($ types as $ type ) {
599
+ if (!$ this ->container ->hasAlias ($ type .$ suffix )) {
600
+ return null ;
601
+ }
602
+
603
+ if (null === $ alias ) {
604
+ $ alias = (string ) $ this ->container ->getAlias ($ type .$ suffix );
605
+ } elseif ((string ) $ this ->container ->getAlias ($ type .$ suffix ) !== $ alias ) {
606
+ return null ;
626
607
}
627
608
}
609
+
610
+ return $ alias ;
628
611
}
629
612
}
0 commit comments