15
15
use Symfony \Component \DependencyInjection \ContainerBuilder ;
16
16
use Symfony \Component \DependencyInjection \Exception \InvalidArgumentException ;
17
17
use Symfony \Component \DependencyInjection \Reference ;
18
+ use Symfony \Component \DependencyInjection \TypedReference ;
18
19
19
20
/**
20
21
* Trait that allows a generic method to find and sort service by priority option in the tag.
@@ -55,41 +56,51 @@ private function findAndSortTaggedServices($tagName, ContainerBuilder $container
55
56
foreach ($ container ->findTaggedServiceIds ($ tagName , true ) as $ serviceId => $ attributes ) {
56
57
$ defaultPriority = null ;
57
58
$ defaultIndex = null ;
59
+ $ class = $ container ->getDefinition ($ serviceId )->getClass ();
60
+ $ class = $ container ->getParameterBag ()->resolveValue ($ class ) ?: null ;
58
61
59
62
foreach ($ attributes as $ attribute ) {
60
63
$ index = $ priority = null ;
61
64
62
65
if (isset ($ attribute ['priority ' ])) {
63
66
$ priority = $ attribute ['priority ' ];
64
- } elseif (null === $ defaultPriority && $ defaultPriorityMethod ) {
65
- $ defaultPriority = PriorityTaggedServiceUtil::getDefaultPriority ($ container , $ serviceId , $ defaultPriorityMethod , $ tagName );
67
+ } elseif (null === $ defaultPriority && $ defaultPriorityMethod && $ class ) {
68
+ $ defaultPriority = PriorityTaggedServiceUtil::getDefaultPriority ($ container , $ serviceId , $ class , $ defaultPriorityMethod , $ tagName );
66
69
}
67
70
$ priority = $ priority ?? $ defaultPriority ?? $ defaultPriority = 0 ;
68
71
69
72
if (null === $ indexAttribute && !$ needsIndexes ) {
70
- $ services [] = [$ priority , ++$ i , null , $ serviceId ];
73
+ $ services [] = [$ priority , ++$ i , null , $ serviceId, null ];
71
74
continue 2 ;
72
75
}
73
76
74
77
if (null !== $ indexAttribute && isset ($ attribute [$ indexAttribute ])) {
75
78
$ index = $ attribute [$ indexAttribute ];
76
- } elseif (null === $ defaultIndex && $ defaultIndexMethod ) {
77
- $ defaultIndex = PriorityTaggedServiceUtil::getDefaultIndex ($ container , $ serviceId , $ defaultIndexMethod , $ tagName , $ indexAttribute );
79
+ } elseif (null === $ defaultIndex && $ defaultIndexMethod && $ class ) {
80
+ $ defaultIndex = PriorityTaggedServiceUtil::getDefaultIndex ($ container , $ serviceId , $ class , $ defaultIndexMethod , $ tagName , $ indexAttribute );
78
81
}
79
82
$ index = $ index ?? $ defaultIndex ?? $ defaultIndex = $ serviceId ;
80
83
81
- $ services [] = [$ priority , ++$ i , $ index , $ serviceId ];
84
+ $ services [] = [$ priority , ++$ i , $ index , $ serviceId, $ class ];
82
85
}
83
86
}
84
87
85
88
uasort ($ services , static function ($ a , $ b ) { return $ b [0 ] <=> $ a [0 ] ?: $ a [1 ] <=> $ b [1 ]; });
86
89
87
90
$ refs = [];
88
- foreach ($ services as [, , $ index , $ serviceId ]) {
91
+ foreach ($ services as [, , $ index , $ serviceId , $ class ]) {
92
+ if (!$ class ) {
93
+ $ reference = new Reference ($ serviceId );
94
+ } elseif ($ index === $ serviceId ) {
95
+ $ reference = new TypedReference ($ serviceId , $ class );
96
+ } else {
97
+ $ reference = new TypedReference ($ serviceId , $ class , ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE , $ index );
98
+ }
99
+
89
100
if (null === $ index ) {
90
- $ refs [] = new Reference ( $ serviceId ) ;
101
+ $ refs [] = $ reference ;
91
102
} else {
92
- $ refs [$ index ] = new Reference ( $ serviceId ) ;
103
+ $ refs [$ index ] = $ reference ;
93
104
}
94
105
}
95
106
@@ -105,11 +116,8 @@ class PriorityTaggedServiceUtil
105
116
/**
106
117
* Gets the index defined by the default index method.
107
118
*/
108
- public static function getDefaultIndex (ContainerBuilder $ container , string $ serviceId , string $ defaultIndexMethod , string $ tagName , string $ indexAttribute ): ?string
119
+ public static function getDefaultIndex (ContainerBuilder $ container , string $ serviceId , string $ class , string $ defaultIndexMethod , string $ tagName , string $ indexAttribute ): ?string
109
120
{
110
- $ class = $ container ->getDefinition ($ serviceId )->getClass ();
111
- $ class = $ container ->getParameterBag ()->resolveValue ($ class ) ?: null ;
112
-
113
121
if (!($ r = $ container ->getReflectionClass ($ class )) || !$ r ->hasMethod ($ defaultIndexMethod )) {
114
122
return null ;
115
123
}
@@ -134,11 +142,8 @@ public static function getDefaultIndex(ContainerBuilder $container, string $serv
134
142
/**
135
143
* Gets the priority defined by the default priority method.
136
144
*/
137
- public static function getDefaultPriority (ContainerBuilder $ container , string $ serviceId , string $ defaultPriorityMethod , string $ tagName ): ?int
145
+ public static function getDefaultPriority (ContainerBuilder $ container , string $ serviceId , string $ class , string $ defaultPriorityMethod , string $ tagName ): ?int
138
146
{
139
- $ class = $ container ->getDefinition ($ serviceId )->getClass ();
140
- $ class = $ container ->getParameterBag ()->resolveValue ($ class ) ?: null ;
141
-
142
147
if (!($ r = $ container ->getReflectionClass ($ class )) || !$ r ->hasMethod ($ defaultPriorityMethod )) {
143
148
return null ;
144
149
}
0 commit comments