@@ -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
*/
@@ -123,6 +125,8 @@ public function load($resource, $type = null)
123
125
return ;
124
126
}
125
127
128
+ $ this ->anonymousServicesCount = 0 ;
129
+
126
130
// imports
127
131
$ this ->parseImports ($ content , $ path );
128
132
@@ -133,7 +137,7 @@ public function load($resource, $type = null)
133
137
}
134
138
135
139
foreach ($ content ['parameters ' ] as $ key => $ value ) {
136
- $ this ->container ->setParameter ($ key , $ this ->resolveServices ($ value ));
140
+ $ this ->container ->setParameter ($ key , $ this ->resolveServices ($ value, $ resource , true ));
137
141
}
138
142
}
139
143
@@ -416,19 +420,19 @@ private function parseDefinition($id, $service, $file, array $defaults)
416
420
}
417
421
418
422
if (isset ($ service ['arguments ' ])) {
419
- $ definition ->setArguments ($ this ->resolveServices ($ service ['arguments ' ]));
423
+ $ definition ->setArguments ($ this ->resolveServices ($ service ['arguments ' ], $ file ));
420
424
}
421
425
422
426
if (isset ($ service ['properties ' ])) {
423
- $ definition ->setProperties ($ this ->resolveServices ($ service ['properties ' ]));
427
+ $ definition ->setProperties ($ this ->resolveServices ($ service ['properties ' ], $ file ));
424
428
}
425
429
426
430
if (isset ($ service ['configurator ' ])) {
427
431
$ definition ->setConfigurator ($ this ->parseCallable ($ service ['configurator ' ], 'configurator ' , $ id , $ file ));
428
432
}
429
433
430
434
if (isset ($ service ['getters ' ])) {
431
- $ definition ->setOverriddenGetters ($ this ->resolveServices ($ service ['getters ' ]));
435
+ $ definition ->setOverriddenGetters ($ this ->resolveServices ($ service ['getters ' ], $ file ));
432
436
}
433
437
434
438
if (isset ($ service ['calls ' ])) {
@@ -439,10 +443,10 @@ private function parseDefinition($id, $service, $file, array $defaults)
439
443
foreach ($ service ['calls ' ] as $ call ) {
440
444
if (isset ($ call ['method ' ])) {
441
445
$ method = $ call ['method ' ];
442
- $ args = isset ($ call ['arguments ' ]) ? $ this ->resolveServices ($ call ['arguments ' ]) : array ();
446
+ $ args = isset ($ call ['arguments ' ]) ? $ this ->resolveServices ($ call ['arguments ' ], $ file ) : array ();
443
447
} else {
444
448
$ method = $ call [0 ];
445
- $ args = isset ($ call [1 ]) ? $ this ->resolveServices ($ call [1 ]) : array ();
449
+ $ args = isset ($ call [1 ]) ? $ this ->resolveServices ($ call [1 ], $ file ) : array ();
446
450
}
447
451
448
452
$ definition ->addMethodCall ($ method , $ args );
@@ -538,6 +542,7 @@ private function parseDefinition($id, $service, $file, array $defaults)
538
542
* @param string $parameter A parameter (e.g. 'factory' or 'configurator')
539
543
* @param string $id A service identifier
540
544
* @param string $file A parsed file
545
+ * @param array $defaults
541
546
*
542
547
* @throws InvalidArgumentException When errors are occuried
543
548
*
@@ -553,15 +558,15 @@ private function parseCallable($callable, $parameter, $id, $file)
553
558
if (false !== strpos ($ callable , ': ' ) && false === strpos ($ callable , ':: ' )) {
554
559
$ parts = explode (': ' , $ callable );
555
560
556
- return array ($ this ->resolveServices ('@ ' .$ parts [0 ]), $ parts [1 ]);
561
+ return array ($ this ->resolveServices ('@ ' .$ parts [0 ], $ file ), $ parts [1 ]);
557
562
}
558
563
559
564
return $ callable ;
560
565
}
561
566
562
567
if (is_array ($ callable )) {
563
568
if (isset ($ callable [0 ]) && isset ($ callable [1 ])) {
564
- return array ($ this ->resolveServices ($ callable [0 ]), $ callable [1 ]);
569
+ return array ($ this ->resolveServices ($ callable [0 ], $ file ), $ callable [1 ]);
565
570
}
566
571
567
572
if ('factory ' === $ parameter && isset ($ callable [1 ]) && null === $ callable [0 ]) {
@@ -653,11 +658,13 @@ private function validate($content, $file)
653
658
/**
654
659
* Resolves services.
655
660
*
656
- * @param mixed $value
661
+ * @param mixed $value
662
+ * @param string $file
663
+ * @param bool $isParameter
657
664
*
658
665
* @return array|string|Reference|ArgumentInterface
659
666
*/
660
- private function resolveServices ($ value )
667
+ private function resolveServices ($ value, $ file , $ isParameter = false )
661
668
{
662
669
if ($ value instanceof TaggedValue) {
663
670
$ argument = $ value ->getValue ();
@@ -666,7 +673,7 @@ private function resolveServices($value)
666
673
throw new InvalidArgumentException ('"!iterator" tag only accepts sequences. ' );
667
674
}
668
675
669
- return new IteratorArgument ($ this ->resolveServices ($ argument ));
676
+ return new IteratorArgument ($ this ->resolveServices ($ argument, $ file ));
670
677
}
671
678
if ('service_locator ' === $ value ->getTag ()) {
672
679
if (!is_array ($ argument )) {
@@ -679,7 +686,7 @@ private function resolveServices($value)
679
686
}
680
687
}
681
688
682
- return new ServiceLocatorArgument ($ this ->resolveServices ($ argument ));
689
+ return new ServiceLocatorArgument ($ this ->resolveServices ($ argument, $ file ));
683
690
}
684
691
if ('closure_proxy ' === $ value ->getTag ()) {
685
692
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 +703,39 @@ private function resolveServices($value)
696
703
697
704
return new ClosureProxyArgument ($ argument [0 ], $ argument [1 ], $ invalidBehavior );
698
705
}
706
+ // Anonymous service
707
+ if ('service ' === $ value ->getTag ()) {
708
+ if ($ isParameter ) {
709
+ throw new InvalidArgumentException (sprintf ('Using an anonymous service in a parameter is not allowed in "%s". ' , $ file ));
710
+ }
711
+
712
+ $ isLoadingInstanceof = $ this ->isLoadingInstanceof ;
713
+ $ this ->isLoadingInstanceof = false ;
714
+ $ instanceof = $ this ->instanceof ;
715
+ $ this ->instanceof = array ();
716
+
717
+ $ id = sprintf ('%d_%s ' , ++$ this ->anonymousServicesCount , hash ('sha256 ' , $ file ));
718
+ $ this ->parseDefinition ($ id , $ argument , $ file , array ());
719
+
720
+ if (!$ this ->container ->hasDefinition ($ id )) {
721
+ throw new InvalidArgumentException (sprintf ('Creating an alias using the tag "!service" is not allowed in "%s". ' , $ file ));
722
+ }
723
+
724
+ $ this ->container ->getDefinition ($ id )->setPublic (false );
725
+
726
+ $ this ->isLoadingInstanceof = $ isLoadingInstanceof ;
727
+ $ this ->instanceof = $ instanceof ;
728
+
729
+ return new Reference ($ id );
730
+ }
699
731
700
732
throw new InvalidArgumentException (sprintf ('Unsupported tag "!%s". ' , $ value ->getTag ()));
701
733
}
702
734
703
735
if (is_array ($ value )) {
704
- $ value = array_map (array ($ this , 'resolveServices ' ), $ value );
736
+ foreach ($ value as &$ v ) {
737
+ $ v = $ this ->resolveServices ($ v , $ file );
738
+ }
705
739
} elseif (is_string ($ value ) && 0 === strpos ($ value , '@= ' )) {
706
740
return new Expression (substr ($ value , 2 ));
707
741
} elseif (is_string ($ value ) && 0 === strpos ($ value , '@ ' )) {
0 commit comments