1515use Symfony \Component \DependencyInjection \ContainerBuilder ;
1616use Symfony \Component \DependencyInjection \Exception \InvalidArgumentException ;
1717use Symfony \Component \DependencyInjection \Reference ;
18+ use Symfony \Component \DependencyInjection \TypedReference ;
1819
1920/**
2021 * 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
5556 foreach ($ container ->findTaggedServiceIds ($ tagName , true ) as $ serviceId => $ attributes ) {
5657 $ defaultPriority = null ;
5758 $ defaultIndex = null ;
59+ $ class = $ container ->getDefinition ($ serviceId )->getClass ();
60+ $ class = $ container ->getParameterBag ()->resolveValue ($ class ) ?: null ;
5861
5962 foreach ($ attributes as $ attribute ) {
6063 $ index = $ priority = null ;
6164
6265 if (isset ($ attribute ['priority ' ])) {
6366 $ 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 );
6669 }
6770 $ priority = $ priority ?? $ defaultPriority ?? $ defaultPriority = 0 ;
6871
6972 if (null === $ indexAttribute && !$ needsIndexes ) {
70- $ services [] = [$ priority , ++$ i , null , $ serviceId ];
73+ $ services [] = [$ priority , ++$ i , null , $ serviceId, null ];
7174 continue 2 ;
7275 }
7376
7477 if (null !== $ indexAttribute && isset ($ attribute [$ indexAttribute ])) {
7578 $ 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 );
7881 }
7982 $ index = $ index ?? $ defaultIndex ?? $ defaultIndex = $ serviceId ;
8083
81- $ services [] = [$ priority , ++$ i , $ index , $ serviceId ];
84+ $ services [] = [$ priority , ++$ i , $ index , $ serviceId, $ class ];
8285 }
8386 }
8487
8588 uasort ($ services , static function ($ a , $ b ) { return $ b [0 ] <=> $ a [0 ] ?: $ a [1 ] <=> $ b [1 ]; });
8689
8790 $ 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+
89100 if (null === $ index ) {
90- $ refs [] = new Reference ( $ serviceId ) ;
101+ $ refs [] = $ reference ;
91102 } else {
92- $ refs [$ index ] = new Reference ( $ serviceId ) ;
103+ $ refs [$ index ] = $ reference ;
93104 }
94105 }
95106
@@ -105,11 +116,8 @@ class PriorityTaggedServiceUtil
105116 /**
106117 * Gets the index defined by the default index method.
107118 */
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
109120 {
110- $ class = $ container ->getDefinition ($ serviceId )->getClass ();
111- $ class = $ container ->getParameterBag ()->resolveValue ($ class ) ?: null ;
112-
113121 if (!($ r = $ container ->getReflectionClass ($ class )) || !$ r ->hasMethod ($ defaultIndexMethod )) {
114122 return null ;
115123 }
@@ -134,11 +142,8 @@ public static function getDefaultIndex(ContainerBuilder $container, string $serv
134142 /**
135143 * Gets the priority defined by the default priority method.
136144 */
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
138146 {
139- $ class = $ container ->getDefinition ($ serviceId )->getClass ();
140- $ class = $ container ->getParameterBag ()->resolveValue ($ class ) ?: null ;
141-
142147 if (!($ r = $ container ->getReflectionClass ($ class )) || !$ r ->hasMethod ($ defaultPriorityMethod )) {
143148 return null ;
144149 }
0 commit comments