@@ -65,15 +65,34 @@ private function findAndSortTaggedServices(string|TaggedIteratorArgument $tagNam
6565 $ class = $ definition ->getClass ();
6666 $ class = $ container ->getParameterBag ()->resolveValue ($ class ) ?: null ;
6767 $ reflector = null !== $ class ? $ container ->getReflectionClass ($ class ) : null ;
68- $ checkTaggedItem = !$ definition ->hasTag ($ definition ->isAutoconfigured () ? 'container.ignore_attributes ' : $ tagName );
68+ $ loadFromDefaultMethods = $ reflector && null !== $ defaultPriorityMethod ;
69+
70+ if ($ reflector && $ definition ->isAutoconfigured () && !$ definition ->hasTag ('container.ignore_attributes ' ) && $ phpAttributes = $ reflector ->getAttributes (AsTaggedItem::class)) {
71+ $ loadFromAttributes = $ attributes !== $ tagAttributes = array_filter ($ attributes );
72+ $ attributes = [];
73+ foreach ($ phpAttributes as $ attribute ) {
74+ $ attribute = $ attribute ->newInstance ();
75+ $ attributes [] = [
76+ 'priority ' => $ attribute ->priority ,
77+ $ indexAttribute ?? '' => $ attribute ->index ,
78+ ];
79+ if (null === $ defaultPriority ) {
80+ $ defaultPriority = $ attribute ->priority ?? 0 ;
81+ $ defaultIndex = $ attribute ->index ;
82+ }
83+ }
84+ $ attributes = $ loadFromAttributes ? array_merge ($ attributes , $ tagAttributes ) : $ tagAttributes ;
85+ }
6986
7087 foreach ($ attributes as $ attribute ) {
7188 $ index = $ priority = null ;
7289
7390 if (isset ($ attribute ['priority ' ])) {
7491 $ priority = $ attribute ['priority ' ];
75- } elseif (null === $ defaultPriority && $ defaultPriorityMethod && $ reflector ) {
76- $ defaultPriority = PriorityTaggedServiceUtil::getDefault ($ serviceId , $ reflector , $ defaultPriorityMethod , $ tagName , 'priority ' , $ checkTaggedItem );
92+ } elseif ($ loadFromDefaultMethods ) {
93+ $ defaultPriority = PriorityTaggedServiceUtil::getDefault ($ serviceId , $ reflector , $ defaultPriorityMethod , $ tagName , 'priority ' ) ?? $ defaultPriority ;
94+ $ defaultIndex = PriorityTaggedServiceUtil::getDefault ($ serviceId , $ reflector , $ defaultIndexMethod ?? 'getDefaultName ' , $ tagName , $ indexAttribute ) ?? $ defaultIndex ;
95+ $ loadFromDefaultMethods = false ;
7796 }
7897 $ priority ??= $ defaultPriority ??= 0 ;
7998
@@ -85,41 +104,26 @@ private function findAndSortTaggedServices(string|TaggedIteratorArgument $tagNam
85104 if (null !== $ indexAttribute && isset ($ attribute [$ indexAttribute ])) {
86105 $ index = $ parameterBag ->resolveValue ($ attribute [$ indexAttribute ]);
87106 }
88- if (null === $ index && null === $ defaultIndex && $ defaultPriorityMethod && $ reflector ) {
89- $ defaultIndex = PriorityTaggedServiceUtil::getDefault ($ serviceId , $ reflector , $ defaultIndexMethod ?? 'getDefaultName ' , $ tagName , $ indexAttribute , $ checkTaggedItem );
107+ if (null === $ index && $ loadFromDefaultMethods ) {
108+ $ defaultPriority = PriorityTaggedServiceUtil::getDefault ($ serviceId , $ reflector , $ defaultPriorityMethod , $ tagName , 'priority ' ) ?? $ defaultPriority ;
109+ $ defaultIndex = PriorityTaggedServiceUtil::getDefault ($ serviceId , $ reflector , $ defaultIndexMethod ?? 'getDefaultName ' , $ tagName , $ indexAttribute ) ?? $ defaultIndex ;
110+ $ loadFromDefaultMethods = false ;
90111 }
91112 $ index ??= $ defaultIndex ??= $ definition ->getTag ('container.decorator ' )[0 ]['id ' ] ?? $ serviceId ;
92113
93114 $ services [] = [$ priority , ++$ i , $ index , $ serviceId , $ class ];
94115 }
95-
96- if ($ reflector ) {
97- $ attributes = $ reflector ->getAttributes (AsTaggedItem::class);
98- $ attributeCount = \count ($ attributes );
99-
100- foreach ($ attributes as $ attribute ) {
101- $ instance = $ attribute ->newInstance ();
102-
103- if (!$ instance ->index && 1 < $ attributeCount ) {
104- throw new InvalidArgumentException (\sprintf ('Attribute "%s" on class "%s" cannot have an empty index when repeated. ' , AsTaggedItem::class, $ class ));
105- }
106-
107- $ services [] = [$ instance ->priority ?? 0 , ++$ i , $ instance ->index ?? $ serviceId , $ serviceId , $ class ];
108- }
109- }
110116 }
111117
112118 uasort ($ services , static fn ($ a , $ b ) => $ b [0 ] <=> $ a [0 ] ?: $ a [1 ] <=> $ b [1 ]);
113119
114120 $ refs = [];
115121 foreach ($ services as [, , $ index , $ serviceId , $ class ]) {
116- if (!$ class ) {
117- $ reference = new Reference ($ serviceId );
118- } elseif ($ index === $ serviceId ) {
119- $ reference = new TypedReference ($ serviceId , $ class );
120- } else {
121- $ reference = new TypedReference ($ serviceId , $ class , ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE , $ index );
122- }
122+ $ reference = match (true ) {
123+ !$ class => new Reference ($ serviceId ),
124+ $ index === $ serviceId => new TypedReference ($ serviceId , $ class ),
125+ default => new TypedReference ($ serviceId , $ class , ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE , $ index ),
126+ };
123127
124128 if (null === $ index ) {
125129 $ refs [] = $ reference ;
@@ -137,25 +141,16 @@ private function findAndSortTaggedServices(string|TaggedIteratorArgument $tagNam
137141 */
138142class PriorityTaggedServiceUtil
139143{
140- public static function getDefault (string $ serviceId , \ReflectionClass $ r , string $ defaultMethod , string $ tagName , ?string $ indexAttribute, bool $ checkTaggedItem ): string |int |null
144+ public static function getDefault (string $ serviceId , \ReflectionClass $ r , string $ defaultMethod , string $ tagName , ?string $ indexAttribute ): string |int |null
141145 {
142- $ class = $ r ->getName ();
143-
144- if (!$ checkTaggedItem && !$ r ->hasMethod ($ defaultMethod )) {
145- return null ;
146- }
147-
148- if ($ checkTaggedItem && !$ r ->hasMethod ($ defaultMethod )) {
149- foreach ($ r ->getAttributes (AsTaggedItem::class) as $ attribute ) {
150- return 'priority ' === $ indexAttribute ? $ attribute ->newInstance ()->priority : $ attribute ->newInstance ()->index ;
151- }
152-
146+ if (!$ r ->hasMethod ($ defaultMethod )) {
153147 return null ;
154148 }
155149
156150 if ($ r ->isInterface ()) {
157151 return null ;
158152 }
153+ $ class = $ r ->name ;
159154
160155 if (null !== $ indexAttribute ) {
161156 $ service = $ class !== $ serviceId ? \sprintf ('service "%s" ' , $ serviceId ) : 'on the corresponding service ' ;
0 commit comments