@@ -50,7 +50,6 @@ class AutowirePass extends AbstractRecursivePass
50
50
private ?int $ decoratedMethodIndex = null ;
51
51
private ?int $ decoratedMethodArgumentIndex = null ;
52
52
private ?self $ typesClone = null ;
53
- private array $ combinedAliases ;
54
53
55
54
public function __construct (bool $ throwOnAutowireException = true )
56
55
{
@@ -63,8 +62,6 @@ public function __construct(bool $throwOnAutowireException = true)
63
62
64
63
public function process (ContainerBuilder $ container )
65
64
{
66
- $ this ->populateCombinedAliases ($ container );
67
-
68
65
try {
69
66
$ this ->typesClone = clone $ this ;
70
67
parent ::process ($ container );
@@ -77,7 +74,6 @@ public function process(ContainerBuilder $container)
77
74
$ this ->decoratedMethodIndex = null ;
78
75
$ this ->decoratedMethodArgumentIndex = null ;
79
76
$ this ->typesClone = null ;
80
- $ this ->combinedAliases = [];
81
77
}
82
78
}
83
79
@@ -401,12 +397,12 @@ private function getAutowiredReference(TypedReference $reference, bool $filterTy
401
397
return new TypedReference ($ alias , $ type , $ reference ->getInvalidBehavior ());
402
398
}
403
399
404
- if (null !== ($ alias = $ this ->combinedAliases [ $ alias ] ?? null ) && !$ this ->container ->findDefinition ($ alias )->isAbstract ()) {
400
+ if (null !== ($ alias = $ this ->getCombinedAlias ( $ type , $ name ) ?? null ) && !$ this ->container ->findDefinition ($ alias )->isAbstract ()) {
405
401
return new TypedReference ($ alias , $ type , $ reference ->getInvalidBehavior ());
406
402
}
407
403
408
404
if ($ this ->container ->has ($ name ) && !$ this ->container ->findDefinition ($ name )->isAbstract ()) {
409
- foreach ($ this ->container ->getAliases () + $ this -> combinedAliases as $ id => $ alias ) {
405
+ foreach ($ this ->container ->getAliases () as $ id => $ alias ) {
410
406
if ($ name === (string ) $ alias && str_starts_with ($ id , $ type .' $ ' )) {
411
407
return new TypedReference ($ name , $ type , $ reference ->getInvalidBehavior ());
412
408
}
@@ -418,7 +414,7 @@ private function getAutowiredReference(TypedReference $reference, bool $filterTy
418
414
return new TypedReference ($ type , $ type , $ reference ->getInvalidBehavior ());
419
415
}
420
416
421
- if (null !== ($ alias = $ this ->combinedAliases [ $ type] ?? null ) && !$ this ->container ->findDefinition ($ alias )->isAbstract ()) {
417
+ if (null !== ($ alias = $ this ->getCombinedAlias ( $ type) ?? null ) && !$ this ->container ->findDefinition ($ alias )->isAbstract ()) {
422
418
return new TypedReference ($ alias , $ type , $ reference ->getInvalidBehavior ());
423
419
}
424
420
@@ -624,44 +620,31 @@ private function populateAutowiringAlias(string $id): void
624
620
}
625
621
}
626
622
627
- private function populateCombinedAliases ( ContainerBuilder $ container ): void
623
+ private function getCombinedAlias ( string $ type , string $ name = null ): ? string
628
624
{
629
- $ this ->combinedAliases = [];
630
- $ reverseAliases = [];
631
-
632
- foreach ($ container ->getAliases () as $ id => $ alias ) {
633
- if (!preg_match ('/(?(DEFINE)(?<V>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+))^((?&V)(?: \\\\(?&V))*+)(?: \$((?&V)))?$/ ' , $ id , $ m )) {
634
- continue ;
635
- }
636
-
637
- $ type = $ m [2 ];
638
- $ name = $ m [3 ] ?? '' ;
639
- $ reverseAliases [(string ) $ alias ][$ name ][] = $ type ;
625
+ if (str_contains ($ type , '& ' )) {
626
+ $ types = explode ('& ' , $ type );
627
+ } elseif (str_contains ($ type , '| ' )) {
628
+ $ types = explode ('| ' , $ type );
629
+ } else {
630
+ return null ;
640
631
}
641
632
642
- foreach ($ reverseAliases as $ alias => $ names ) {
643
- foreach ($ names as $ name => $ types ) {
644
- if (2 > $ count = \count ($ types )) {
645
- continue ;
646
- }
647
- sort ($ types );
648
- $ i = 1 << $ count ;
649
-
650
- // compute the powerset of the list of types
651
- while ($ i --) {
652
- $ set = [];
653
- for ($ j = 0 ; $ j < $ count ; ++$ j ) {
654
- if ($ i & (1 << $ j )) {
655
- $ set [] = $ types [$ j ];
656
- }
657
- }
633
+ $ alias = null ;
634
+ $ suffix = $ name ? ' $ ' .$ name : '' ;
658
635
659
- if (2 <= \count ($ set )) {
660
- $ this ->combinedAliases [implode ('& ' , $ set ).('' === $ name ? '' : ' $ ' .$ name )] = $ alias ;
661
- $ this ->combinedAliases [implode ('| ' , $ set ).('' === $ name ? '' : ' $ ' .$ name )] = $ alias ;
662
- }
663
- }
636
+ foreach ($ types as $ type ) {
637
+ if (!$ this ->container ->hasAlias ($ type .$ suffix )) {
638
+ return null ;
639
+ }
640
+
641
+ if (null === $ alias ) {
642
+ $ alias = (string ) $ this ->container ->getAlias ($ type .$ suffix );
643
+ } elseif ((string ) $ this ->container ->getAlias ($ type .$ suffix ) !== $ alias ) {
644
+ return null ;
664
645
}
665
646
}
647
+
648
+ return $ alias ;
666
649
}
667
650
}
0 commit comments