@@ -74,7 +74,7 @@ protected function processValue($value, $isRoot = false)
7474 throw $ e ;
7575 }
7676
77- $ this ->container ->getDefinition ($ this ->currentId )->addError ($ e ->getMessage ());
77+ $ this ->container ->getDefinition ($ this ->currentId )->addError ($ e ->getMessageCallback () ?? $ e -> getMessage ());
7878
7979 return parent ::processValue ($ value , $ isRoot );
8080 }
@@ -86,16 +86,21 @@ private function doProcessValue($value, $isRoot = false)
8686 if ($ ref = $ this ->getAutowiredReference ($ value )) {
8787 return $ ref ;
8888 }
89- $ message = $ this ->createTypeNotFoundMessage ($ value , 'it ' );
90-
9189 if (ContainerBuilder::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE === $ value ->getInvalidBehavior ()) {
90+ $ container = new ContainerBuilder ($ this ->container ->getParameterBag ());
91+ $ container ->setAliases ($ this ->container ->getAliases ());
92+ $ container ->setDefinitions ($ this ->container ->getDefinitions ());
93+ $ container ->setResourceTracking (false );
94+ $ message = function () use ($ container , $ value ) {
95+ return $ this ->createTypeNotFoundMessage ($ container , $ value , 'it ' );
96+ };
97+
9298 // since the error message varies by referenced id and $this->currentId, so should the id of the dummy errored definition
9399 $ this ->container ->register ($ id = sprintf ('.errored.%s.%s ' , $ this ->currentId , (string ) $ value ), $ value ->getType ())
94100 ->addError ($ message );
95101
96102 return new TypedReference ($ id , $ value ->getType (), $ value ->getInvalidBehavior (), $ value ->getName ());
97103 }
98- $ this ->container ->log ($ this , $ message );
99104 }
100105 $ value = parent ::processValue ($ value , $ isRoot );
101106
@@ -222,14 +227,19 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
222227
223228 $ getValue = function () use ($ type , $ parameter , $ class , $ method ) {
224229 if (!$ value = $ this ->getAutowiredReference ($ ref = new TypedReference ($ type , $ type , ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE , $ parameter ->name ))) {
225- $ failureMessage = $ this ->createTypeNotFoundMessage ($ ref , sprintf ('argument "$%s" of method "%s()" ' , $ parameter ->name , $ class !== $ this ->currentId ? $ class .':: ' .$ method : $ method ));
230+ $ container = new ContainerBuilder ($ this ->container ->getParameterBag ());
231+ $ container ->setAliases ($ this ->container ->getAliases ());
232+ $ container ->setDefinitions ($ this ->container ->getDefinitions ());
233+ $ container ->setResourceTracking (false );
234+ $ failureMessage = function () use ($ container , $ ref , $ parameter , $ class , $ method ) {
235+ return $ this ->createTypeNotFoundMessage ($ container , $ ref , sprintf ('argument "$%s" of method "%s()" ' , $ parameter ->name , $ class !== $ this ->currentId ? $ class .':: ' .$ method : $ method ));
236+ };
226237
227238 if ($ parameter ->isDefaultValueAvailable ()) {
228239 $ value = $ parameter ->getDefaultValue ();
229240 } elseif (!$ parameter ->allowsNull ()) {
230241 throw new AutowiringFailedException ($ this ->currentId , $ failureMessage );
231242 }
232- $ this ->container ->log ($ this , $ failureMessage );
233243 }
234244
235245 return $ value ;
@@ -307,27 +317,27 @@ private function getAutowiredReference(TypedReference $reference)
307317 /**
308318 * Populates the list of available types.
309319 */
310- private function populateAvailableTypes ()
320+ private function populateAvailableTypes (ContainerBuilder $ container )
311321 {
312322 $ this ->types = array ();
313323 $ this ->ambiguousServiceTypes = array ();
314324
315- foreach ($ this -> container ->getDefinitions () as $ id => $ definition ) {
316- $ this ->populateAvailableType ($ id , $ definition );
325+ foreach ($ container ->getDefinitions () as $ id => $ definition ) {
326+ $ this ->populateAvailableType ($ container , $ id , $ definition );
317327 }
318328 }
319329
320330 /**
321331 * Populates the list of available types for a given definition.
322332 */
323- private function populateAvailableType (string $ id , Definition $ definition )
333+ private function populateAvailableType (ContainerBuilder $ container , string $ id , Definition $ definition )
324334 {
325335 // Never use abstract services
326336 if ($ definition ->isAbstract ()) {
327337 return ;
328338 }
329339
330- if ('' === $ id || '. ' === $ id [0 ] || $ definition ->isDeprecated () || !$ reflectionClass = $ this -> container ->getReflectionClass ($ definition ->getClass (), false )) {
340+ if ('' === $ id || '. ' === $ id [0 ] || $ definition ->isDeprecated () || !$ reflectionClass = $ container ->getReflectionClass ($ definition ->getClass (), false )) {
331341 return ;
332342 }
333343
@@ -367,19 +377,9 @@ private function set(string $type, string $id)
367377 $ this ->ambiguousServiceTypes [$ type ][] = $ id ;
368378 }
369379
370- private function createTypeNotFoundMessage (TypedReference $ reference , $ label )
380+ private function createTypeNotFoundMessage (ContainerBuilder $ container , TypedReference $ reference , $ label )
371381 {
372- $ trackResources = $ this ->container ->isTrackingResources ();
373- $ this ->container ->setResourceTracking (false );
374- try {
375- if ($ r = $ this ->container ->getReflectionClass ($ type = $ reference ->getType (), false )) {
376- $ alternatives = $ this ->createTypeAlternatives ($ reference );
377- }
378- } finally {
379- $ this ->container ->setResourceTracking ($ trackResources );
380- }
381-
382- if (!$ r ) {
382+ if (!$ r = $ container ->getReflectionClass ($ type = $ reference ->getType (), false )) {
383383 // either $type does not exist or a parent class does not exist
384384 try {
385385 $ resource = new ClassExistenceResource ($ type , false );
@@ -392,7 +392,8 @@ private function createTypeNotFoundMessage(TypedReference $reference, $label)
392392
393393 $ message = sprintf ('has type "%s" but this class %s. ' , $ type , $ parentMsg ? sprintf ('is missing a parent class (%s) ' , $ parentMsg ) : 'was not found ' );
394394 } else {
395- $ message = $ this ->container ->has ($ type ) ? 'this service is abstract ' : 'no such service exists ' ;
395+ $ alternatives = $ this ->createTypeAlternatives ($ container , $ reference );
396+ $ message = $ container ->has ($ type ) ? 'this service is abstract ' : 'no such service exists ' ;
396397 $ message = sprintf ('references %s "%s" but %s.%s ' , $ r ->isInterface () ? 'interface ' : 'class ' , $ type , $ message , $ alternatives );
397398
398399 if ($ r ->isInterface () && !$ alternatives ) {
@@ -410,18 +411,18 @@ private function createTypeNotFoundMessage(TypedReference $reference, $label)
410411 return $ message ;
411412 }
412413
413- private function createTypeAlternatives (TypedReference $ reference )
414+ private function createTypeAlternatives (ContainerBuilder $ container , TypedReference $ reference )
414415 {
415416 // try suggesting available aliases first
416- if ($ message = $ this ->getAliasesSuggestionForType ($ type = $ reference ->getType ())) {
417+ if ($ message = $ this ->getAliasesSuggestionForType ($ container , $ type = $ reference ->getType ())) {
417418 return ' ' .$ message ;
418419 }
419420 if (null === $ this ->ambiguousServiceTypes ) {
420- $ this ->populateAvailableTypes ();
421+ $ this ->populateAvailableTypes ($ container );
421422 }
422423
423- $ servicesAndAliases = $ this -> container ->getServiceIds ();
424- if (!$ this -> container ->has ($ type ) && false !== $ key = array_search (strtolower ($ type ), array_map ('strtolower ' , $ servicesAndAliases ))) {
424+ $ servicesAndAliases = $ container ->getServiceIds ();
425+ if (!$ container ->has ($ type ) && false !== $ key = array_search (strtolower ($ type ), array_map ('strtolower ' , $ servicesAndAliases ))) {
425426 return sprintf (' Did you mean "%s"? ' , $ servicesAndAliases [$ key ]);
426427 } elseif (isset ($ this ->ambiguousServiceTypes [$ type ])) {
427428 $ message = sprintf ('one of these existing services: "%s" ' , implode ('", " ' , $ this ->ambiguousServiceTypes [$ type ]));
@@ -434,11 +435,11 @@ private function createTypeAlternatives(TypedReference $reference)
434435 return sprintf (' You should maybe alias this %s to %s. ' , class_exists ($ type , false ) ? 'class ' : 'interface ' , $ message );
435436 }
436437
437- private function getAliasesSuggestionForType ($ type , $ extraContext = null )
438+ private function getAliasesSuggestionForType (ContainerBuilder $ container , $ type , $ extraContext = null )
438439 {
439440 $ aliases = array ();
440441 foreach (class_parents ($ type ) + class_implements ($ type ) as $ parent ) {
441- if ($ this -> container ->has ($ parent ) && !$ this -> container ->findDefinition ($ parent )->isAbstract ()) {
442+ if ($ container ->has ($ parent ) && !$ container ->findDefinition ($ parent )->isAbstract ()) {
442443 $ aliases [] = $ parent ;
443444 }
444445 }
0 commit comments