@@ -74,7 +74,7 @@ protected function processValue($value, $isRoot = false)
74
74
throw $ e ;
75
75
}
76
76
77
- $ this ->container ->getDefinition ($ this ->currentId )->addError ($ e ->getMessage ());
77
+ $ this ->container ->getDefinition ($ this ->currentId )->addError ($ e ->getMessageCallback () ?? $ e -> getMessage ());
78
78
79
79
return parent ::processValue ($ value , $ isRoot );
80
80
}
@@ -86,16 +86,20 @@ private function doProcessValue($value, $isRoot = false)
86
86
if ($ ref = $ this ->getAutowiredReference ($ value )) {
87
87
return $ ref ;
88
88
}
89
- $ message = $ this ->createTypeNotFoundMessage ($ value , 'it ' );
90
-
91
89
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
+ $ message = function () use ($ container , $ value ) {
94
+ return $ this ->createTypeNotFoundMessage ($ container , $ value , 'it ' );
95
+ };
96
+
92
97
// since the error message varies by referenced id and $this->currentId, so should the id of the dummy errored definition
93
98
$ this ->container ->register ($ id = sprintf ('.errored.%s.%s ' , $ this ->currentId , (string ) $ value ), $ value ->getType ())
94
99
->addError ($ message );
95
100
96
101
return new TypedReference ($ id , $ value ->getType (), $ value ->getInvalidBehavior (), $ value ->getName ());
97
102
}
98
- $ this ->container ->log ($ this , $ message );
99
103
}
100
104
$ value = parent ::processValue ($ value , $ isRoot );
101
105
@@ -222,14 +226,18 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
222
226
223
227
$ getValue = function () use ($ type , $ parameter , $ class , $ method ) {
224
228
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 ));
229
+ $ container = new ContainerBuilder ($ this ->container ->getParameterBag ());
230
+ $ container ->setAliases ($ this ->container ->getAliases ());
231
+ $ container ->setDefinitions ($ this ->container ->getDefinitions ());
232
+ $ failureMessage = function () use ($ container , $ ref , $ parameter , $ class , $ method ) {
233
+ return $ this ->createTypeNotFoundMessage ($ container , $ ref , sprintf ('argument "$%s" of method "%s()" ' , $ parameter ->name , $ class !== $ this ->currentId ? $ class .':: ' .$ method : $ method ));
234
+ };
226
235
227
236
if ($ parameter ->isDefaultValueAvailable ()) {
228
237
$ value = $ parameter ->getDefaultValue ();
229
238
} elseif (!$ parameter ->allowsNull ()) {
230
239
throw new AutowiringFailedException ($ this ->currentId , $ failureMessage );
231
240
}
232
- $ this ->container ->log ($ this , $ failureMessage );
233
241
}
234
242
235
243
return $ value ;
@@ -307,27 +315,27 @@ private function getAutowiredReference(TypedReference $reference)
307
315
/**
308
316
* Populates the list of available types.
309
317
*/
310
- private function populateAvailableTypes ()
318
+ private function populateAvailableTypes (ContainerBuilder $ container )
311
319
{
312
320
$ this ->types = array ();
313
321
$ this ->ambiguousServiceTypes = array ();
314
322
315
- foreach ($ this -> container ->getDefinitions () as $ id => $ definition ) {
316
- $ this ->populateAvailableType ($ id , $ definition );
323
+ foreach ($ container ->getDefinitions () as $ id => $ definition ) {
324
+ $ this ->populateAvailableType ($ container , $ id , $ definition );
317
325
}
318
326
}
319
327
320
328
/**
321
329
* Populates the list of available types for a given definition.
322
330
*/
323
- private function populateAvailableType (string $ id , Definition $ definition )
331
+ private function populateAvailableType (ContainerBuilder $ container , string $ id , Definition $ definition )
324
332
{
325
333
// Never use abstract services
326
334
if ($ definition ->isAbstract ()) {
327
335
return ;
328
336
}
329
337
330
- if ('' === $ id || '. ' === $ id [0 ] || $ definition ->isDeprecated () || !$ reflectionClass = $ this -> container ->getReflectionClass ($ definition ->getClass (), false )) {
338
+ if ('' === $ id || '. ' === $ id [0 ] || $ definition ->isDeprecated () || !$ reflectionClass = $ container ->getReflectionClass ($ definition ->getClass (), false )) {
331
339
return ;
332
340
}
333
341
@@ -367,9 +375,9 @@ private function set(string $type, string $id)
367
375
$ this ->ambiguousServiceTypes [$ type ][] = $ id ;
368
376
}
369
377
370
- private function createTypeNotFoundMessage (TypedReference $ reference , $ label )
378
+ private function createTypeNotFoundMessage (ContainerBuilder $ container , TypedReference $ reference , $ label )
371
379
{
372
- if (!$ r = $ this -> container ->getReflectionClass ($ type = $ reference ->getType (), false )) {
380
+ if (!$ r = $ container ->getReflectionClass ($ type = $ reference ->getType (), false )) {
373
381
// either $type does not exist or a parent class does not exist
374
382
try {
375
383
$ resource = new ClassExistenceResource ($ type , false );
@@ -382,8 +390,8 @@ private function createTypeNotFoundMessage(TypedReference $reference, $label)
382
390
383
391
$ message = sprintf ('has type "%s" but this class %s. ' , $ type , $ parentMsg ? sprintf ('is missing a parent class (%s) ' , $ parentMsg ) : 'was not found ' );
384
392
} else {
385
- $ alternatives = $ this ->createTypeAlternatives ($ reference );
386
- $ message = $ this -> container ->has ($ type ) ? 'this service is abstract ' : 'no such service exists ' ;
393
+ $ alternatives = $ this ->createTypeAlternatives ($ container , $ reference );
394
+ $ message = $ container ->has ($ type ) ? 'this service is abstract ' : 'no such service exists ' ;
387
395
$ message = sprintf ('references %s "%s" but %s.%s ' , $ r ->isInterface () ? 'interface ' : 'class ' , $ type , $ message , $ alternatives );
388
396
389
397
if
F438
($ r ->isInterface () && !$ alternatives ) {
@@ -401,18 +409,18 @@ private function createTypeNotFoundMessage(TypedReference $reference, $label)
401
409
return $ message ;
402
410
}
403
411
404
- private function createTypeAlternatives (TypedReference $ reference )
412
+ private function createTypeAlternatives (ContainerBuilder $ container , TypedReference $ reference )
405
413
{
406
414
// try suggesting available aliases first
407
- if ($ message = $ this ->getAliasesSuggestionForType ($ type = $ reference ->getType ())) {
415
+ if ($ message = $ this ->getAliasesSuggestionForType ($ container , $ type = $ reference ->getType ())) {
408
416
return ' ' .$ message ;
409
417
}
410
418
if (null === $ this ->ambiguousServiceTypes ) {
411
- $ this ->populateAvailableTypes ();
419
+ $ this ->populateAvailableTypes ($ container );
412
420
}
413
421
414
- $ servicesAndAliases = $ this -> container ->getServiceIds ();
415
- if (!$ this -> container ->has ($ type ) && false !== $ key = array_search (strtolower ($ type ), array_map ('strtolower ' , $ servicesAndAliases ))) {
422
+ $ servicesAndAliases = $ container ->getServiceIds ();
423
+ if (!$ container ->has ($ type ) && false !== $ key = array_search (strtolower ($ type ), array_map ('strtolower ' , $ servicesAndAliases ))) {
416
424
return sprintf (' Did you mean "%s"? ' , $ servicesAndAliases [$ key ]);
417
425
} elseif (isset ($ this ->ambiguousServiceTypes [$ type ])) {
418
426
$ message = sprintf ('one of these existing services: "%s" ' , implode ('", " ' , $ this ->ambiguousServiceTypes [$ type ]));
@@ -425,11 +433,11 @@ private function createTypeAlternatives(TypedReference $reference)
425
433
return sprintf (' You should maybe alias this %s to %s. ' , class_exists ($ type , false ) ? 'class ' : 'interface ' , $ message );
426
434
}
427
435
428
- private function getAliasesSuggestionForType ($ type , $ extraContext = null )
436
+ private function getAliasesSuggestionForType (ContainerBuilder $ container , $ type , $ extraContext = null )
429
437
{
430
438
$ aliases = array ();
431
439
foreach (class_parents ($ type ) + class_implements ($ type ) as $ parent ) {
432
- if ($ this -> container ->has ($ parent ) && !$ this -> container ->findDefinition ($ parent )->isAbstract ()) {
440
+ if ($ container ->has ($ parent ) && !$ container ->findDefinition ($ parent )->isAbstract ()) {
433
441
$ aliases [] = $ parent ;
434
442
}
435
443
}
0 commit comments