@@ -107,6 +107,8 @@ class YamlFileLoader extends FileLoader
107
107
108
108
private $ yamlParser ;
109
109
110
+ private $ anonymousServicesCount ;
111
+
110
112
/**
111
113
* {@inheritdoc}
112
114
*/
@@ -133,14 +135,15 @@ public function load($resource, $type = null)
133
135
}
134
136
135
137
foreach ($ content ['parameters ' ] as $ key => $ value ) {
136
- $ this ->container ->setParameter ($ key , $ this ->resolveServices ($ value ));
138
+ $ this ->container ->setParameter ($ key , $ this ->resolveServices ($ value, $ resource , true ));
137
139
}
138
140
}
139
141
140
142
// extensions
141
143
$ this ->loadFromExtensions ($ content );
142
144
143
145
// services
146
+ $ this ->anonymousServicesCount = 0 ;
144
147
$ this ->setCurrentDir (dirname ($ path ));
145
148
try {
146
149
$ this ->parseDefinitions ($ content , $ resource );
@@ -416,19 +419,19 @@ private function parseDefinition($id, $service, $file, array $defaults)
416
419
}
417
420
418
421
if (isset ($ service ['arguments ' ])) {
419
- $ definition ->setArguments ($ this ->resolveServices ($ service ['arguments ' ]));
422
+ $ definition ->setArguments ($ this ->resolveServices ($ service ['arguments ' ], $ file ));
420
423
}
421
424
422
425
if (isset ($ service ['properties ' ])) {
423
- $ definition ->setProperties ($ this ->resolveServices ($ service ['properties ' ]));
426
+ $ definition ->setProperties ($ this ->resolveServices ($ service ['properties ' ], $ file ));
424
427
}
425
428
426
429
if (isset ($ service ['configurator ' ])) {
427
430
$ definition ->setConfigurator ($ this ->parseCallable ($ service ['configurator ' ], 'configurator ' , $ id , $ file ));
428
431
}
429
432
430
433
if (isset ($ service ['getters ' ])) {
431
- $ definition ->setOverriddenGetters ($ this ->resolveServices ($ service ['getters ' ]));
434
+ $ definition ->setOverriddenGetters ($ this ->resolveServices ($ service ['getters ' ], $ file ));
432
435
}
433
436
434
437
if (isset ($ service ['calls ' ])) {
@@ -439,10 +442,10 @@ private function parseDefinition($id, $service, $file, array $defaults)
439
442
foreach ($ service ['calls ' ] as $ call ) {
440
443
if (isset ($ call ['method ' ])) {
441
444
$ method = $ call ['method ' ];
442
- $ args = isset ($ call ['arguments ' ]) ? $ this ->resolveServices ($ call ['arguments ' ]) : array ();
445
+ $ args = isset ($ call ['arguments ' ]) ? $ this ->resolveServices ($ call ['arguments ' ], $ file ) : array ();
443
446
} else {
444
447
$ method = $ call [0 ];
445
- $ args = isset ($ call [1 ]) ? $ this ->resolveServices ($ call [1 ]) : array ();
448
+ $ args = isset ($ call [1 ]) ? $ this ->resolveServices ($ call [1 ], $ file ) : array ();
446
449
}
447
450
448
451
$ definition ->addMethodCall ($ method , $ args );
@@ -553,15 +556,15 @@ private function parseCallable($callable, $parameter, $id, $file)
553
556
if (false !== strpos ($ callable , ': ' ) && false === strpos ($ callable , ':: ' )) {
554
557
$ parts = explode (': ' , $ callable );
555
558
556
- return array ($ this ->resolveServices ('@ ' .$
F438
span>parts [0 ]), $ parts [1 ]);
559
+ return array ($ this ->resolveServices ('@ ' .$ parts [0 ], $ file ), $ parts [1 ]);
557
560
}
558
561
559
562
return $ callable ;
560
563
}
561
564
562
565
if (is_array ($ callable )) {
563
566
if (isset ($ callable [0 ]) && isset ($ callable [1 ])) {
564
- return array ($ this ->resolveServices ($ callable [0 ]), $ callable [1 ]);
567
+ return array ($ this ->resolveServices ($ callable [0 ], $ file ), $ callable [1 ]);
565
568
}
566
569
567
570
if ('factory ' === $ parameter && isset ($ callable [1 ]) && null === $ callable [0 ]) {
@@ -653,11 +656,13 @@ private function validate($content, $file)
653
656
/**
654
657
* Resolves services.
655
658
*
656
- * @param mixed $value
659
+ * @param mixed $value
660
+ * @param string $file
661
+ * @param bool $isParameter
657
662
*
658
663
* @return array|string|Reference|ArgumentInterface
659
664
*/
660
- private function resolveServices ($ value )
665
+ private function resolveServices ($ value, $ file , $ isParameter = false )
661
666
{
662
667
if ($ value instanceof TaggedValue) {
663
668
$ argument = $ value ->getValue ();
@@ -666,7 +671,7 @@ private function resolveServices($value)
666
671
throw new InvalidArgumentException ('"!iterator" tag only accepts sequences. ' );
667
672
}
668
673
669
- return new IteratorArgument ($ this ->resolveServices ($ argument ));
674
+ return new IteratorArgument ($ this ->resolveServices ($ argument, $ file , $ isParameter ));
670
675
}
671
676
if ('service_locator ' === $ value ->getTag ()) {
672
677
if (!is_array ($ argument )) {
@@ -679,7 +684,7 @@ private function resolveServices($value)
679
684
}
680
685
}
681
686
682
- return new ServiceLocatorArgument ($ this ->resolveServices ($ argument ));
687
+ return new ServiceLocatorArgument ($ this ->resolveServices ($ argument, $ file , $ isParameter ));
683
688
}
684
689
if ('closure_proxy ' === $ value ->getTag ()) {
685
690
if (!is_array ($ argument ) || array (0 , 1 ) !== array_keys ($ argument ) || !is_string ($ argument [0 ]) || !is_string ($ argument [1 ]) || 0 !== strpos ($ argument [0 ], '@ ' ) || 0 === strpos ($ argument [0 ], '@@ ' )) {
@@ -696,12 +701,39 @@ private function resolveServices($value)
696
701
697
702
return new ClosureProxyArgument ($ argument [0 ], $ argument [1 ], $ invalidBehavior );
698
703
}
704
+ // Anonymous service
705
+ if ('service ' === $ value ->getTag ()) {
706
+ if ($ isParameter ) {
707
+ throw new InvalidArgumentException (sprintf ('Using an anonymous service in a parameter is not allowed in "%s". ' , $ file ));
708
+ }
709
+
710
+ $ isLoadingInstanceof = $ this ->isLoadingInstanceof ;
711
+ $ this ->isLoadingInstanceof = false ;
712
+ $ instanceof = $ this ->instanceof ;
713
+ $ this ->instanceof = array ();
714
+
715
+ $ id = sprintf ('%d_%s ' , ++$ this ->anonymousServicesCount , hash ('sha256 ' , $ file ));
716
+ $ this ->parseDefinition ($ id , $ argument , $ file , array ());
717
+
718
+ if (!$ this ->container ->hasDefinition ($ id )) {
719
+ throw new InvalidArgumentException (sprintf ('Creating an alias using the tag "!service" is not allowed in "%s". ' , $ file ));
720
+ }
721
+
722
+ $ this ->container ->getDefinition ($ id )->setPublic (false );
723
+
724
+ $ this ->isLoadingInstanceof = $ isLoadingInstanceof ;
725
+ $ this ->instanceof = $ instanceof ;
726
+
727
+ return new Reference ($ id );
728
+ }
699
729
700
730
throw new InvalidArgumentException (sprintf ('Unsupported tag "!%s". ' , $ value ->getTag ()));
701
731
}
702
732
703
733
if (is_array ($ value )) {
704
- $ value = array_map (array ($ this , 'resolveServices ' ), $ value );
734
+ foreach ($ value as &$ v ) {
735
+ $ v = $ this ->resolveServices ($ v , $ file , $ isParameter );
736
+ }
705
737
} elseif (is_string ($ value ) && 0 === strpos ($ value , '@= ' )) {
706
738
return new Expression (substr ($ value , 2 ));
707
739
} elseif (is_string ($ value ) && 0 === strpos ($ value , '@ ' )) {
0 commit comments