@@ -460,7 +460,7 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
460
460
$ this ->loading [$ id ] = true ;
461
461
462
462
try {
463
- $ service = $ this ->createService ($ definition , $ id );
463
+ $ service = $ this ->createService ($ definition , new \ SplObjectStorage (), $ id );
464
464
} catch (\Exception $ e ) {
465
465
unset($ this ->loading [$ id ]);
466
466
@@ -846,8 +846,12 @@ public function findDefinition($id)
846
846
*
847
847
* @internal this method is public because of PHP 5.3 limitations, do not use it explicitly in your code
848
848
*/
849
- public function createService (Definition $ definition , $ id , $ tryProxy = true )
849
+ public function createService (Definition $ definition , \ SplObjectStorage $ inlinedDefinitions , $ id = null , $ tryProxy = true )
850
850
{
851
+ if (null === $ id && isset ($ inlinedDefinitions [$ definition ])) {
852
+ return $ inlinedDefinitions [$ definition ];
853
+ }
854
+
851
855
if ($ definition instanceof DefinitionDecorator) {
852
856
throw new RuntimeException (sprintf ('Constructing service "%s" from a parent definition is not supported at build time. ' , $ id ));
853
857
}
@@ -868,11 +872,11 @@ public function createService(Definition $definition, $id, $tryProxy = true)
868
872
->instantiateProxy (
869
873
$ container ,
870
874
$ definition ,
871
- $ id , function () use ($ definition , $ id , $ container ) {
872
- return $ container ->createService ($ definition , $ id , false );
875
+ $ id , function () use ($ definition , $ inlinedDefinitions , $ id , $ container ) {
876
+ return $ container ->createService ($ definition , $ inlinedDefinitions , $ id , false );
873
877
}
874
878
);
875
- $ this ->shareService ($ definition , $ proxy , $ id );
879
+ $ this ->shareService ($ definition , $ proxy , $ id, $ inlinedDefinitions );
876
880
877
881
return $ proxy ;
878
882
}
@@ -883,11 +887,11 @@ public function createService(Definition $definition, $id, $tryProxy = true)
883
887
require_once $ parameterBag ->resolveValue ($ definition ->getFile ());
884
888
}
885
889
886
- $ arguments = $ this ->resolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getArguments ())));
890
+ $ arguments = $ this ->doResolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getArguments ())), $ inlinedDefinitions );
887
891
888
892
if (null !== $ factory = $ definition ->getFactory ()) {
889
893
if (is_array ($ factory )) {
890
- $ factory = array ($ this ->resolveServices ($ parameterBag ->resolveValue ($ factory [0 ])), $ factory [1 ]);
894
+ $ factory = array ($ this ->doResolveServices ($ parameterBag ->resolveValue ($ factory [0 ]), $ inlinedDefinitions ), $ factory [1 ]);
891
895
} elseif (!is_string ($ factory )) {
892
896
throw new RuntimeException (sprintf ('Cannot create service "%s" because of invalid factory ' , $ id ));
893
897
}
@@ -923,16 +927,16 @@ public function createService(Definition $definition, $id, $tryProxy = true)
923
927
924
928
if ($ tryProxy || !$ definition ->isLazy ()) {
925
929
// share only if proxying failed, or if not a proxy
926
- $ this ->shareService ($ definition , $ service , $ id );
930
+ $ this ->shareService ($ definition , $ service , $ id, $ inlinedDefinitions );
927
931
}
928
932
929
- $ properties = $ this ->resolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getProperties ())));
933
+ $ properties = $ this ->doResolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getProperties ())), $ inlinedDefinitions );
930
934
foreach ($ properties as $ name => $ value ) {
931
935
$ service ->$ name = $ value ;
932
936
}
933
937
934
938
foreach ($ definition ->getMethodCalls () as $ call ) {
935
- $ this ->callMethod ($ service , $ call );
939
+ $ this ->callMethod ($ service , $ call, $ inlinedDefinitions );
936
940
}
937
941
938
942
if ($ callable = $ definition ->getConfigurator ()) {
@@ -942,7 +946,7 @@ public function createService(Definition $definition, $id, $tryProxy = true)
942
946
if ($ callable [0 ] instanceof Reference) {
943
947
$ callable [0 ] = $ this ->get ((string ) $ callable [0 ], $ callable [0 ]->getInvalidBehavior ());
944
948
} elseif ($ callable [0 ] instanceof Definition) {
945
- $ callable [0 ] = $ this ->createService ($ callable [0 ], null );
949
+ $ callable [0 ] = $ this ->createService ($ callable [0 ], $ inlinedDefinitions );
946
950
}
947
951
}
948
952
@@ -965,15 +969,20 @@ public function createService(Definition $definition, $id, $tryProxy = true)
965
969
* the real service instances and all expressions evaluated
966
970
*/
967
971
public function resolveServices ($ value )
972
+ {
973
+ return $ this ->doResolveServices ($ value , new \SplObjectStorage ());
974
+ }
975
+
976
+ private function doResolveServices ($ value , \SplObjectStorage $ inlinedDefinitions )
968
977
{
969
978
if (is_array ($ value )) {
970
979
foreach ($ value as $ k => $ v ) {
971
- $ value [$ k ] = $ this ->resolveServices ($ v );
980
+ $ value [$ k ] = $ this ->doResolveServices ($ v, $ inlinedDefinitions );
972
981
}
973
982
} elseif ($ value instanceof Reference) {
974
983
$ value = $ this ->get ((string ) $ value , $ value ->getInvalidBehavior ());
975
984
} elseif ($ value instanceof Definition) {
976
- $ value = $ this ->createService ($ value , null );
985
+ $ value = $ this ->createService ($ value , $ inlinedDefinitions );
977
986
} elseif ($ value instanceof Expression) {
978
987
$ value = $ this ->getExpressionLanguage ()->evaluate ($ value , array ('container ' => $ this ));
979
988
}
@@ -1111,14 +1120,14 @@ private function synchronize($id)
1111
1120
foreach ($ definition ->getMethodCalls () as $ call ) {
1112
1121
foreach ($ call [1 ] as $ argument ) {
1113
1122
if ($ argument instanceof Reference && $ id == (string ) $ argument ) {
1114
- $ this ->callMethod ($ this ->get ($ definitionId ), $ call );
1123
+ $ this ->callMethod ($ this ->get ($ definitionId ), $ call, new \ SplObjectStorage () );
1115
1124
}
1116
1125
}
1117
1126
}
1118
1127
}
1119
1128
}
1120
1129
1121
- private function callMethod ($ service , $ call )
1130
+ private function callMethod ($ service , $ call, \ SplObjectStorage $ inlinedDefinitions )
1122
1131
{
1123
1132
$ services = self ::getServiceConditionals ($ call [1 ]);
1124
1133
@@ -1128,7 +1137,7 @@ private function callMethod($service, $call)
1128
1137
}
1129
1138
}
1130
1139
1131
- call_user_func_array (array ($ service , $ call [0 ]), $ this ->resolveServices ($ this ->getParameterBag ()->unescapeValue ($ this ->getParameterBag ()->resolveValue ($ call [1 ]))));
1140
+ call_user_func_array (array ($ service , $ call [0 ]), $ this ->doResolveServices ($ this ->getParameterBag ()->unescapeValue ($ this ->getParameterBag ()->resolveValue ($ call [1 ])), $ inlinedDefinitions ));
1132
1141
}
1133
1142
1134
1143
/**
@@ -1140,9 +1149,14 @@ private function callMethod($service, $call)
1140
1149
*
1141
1150
* @throws InactiveScopeException
1142
1151
*/
1143
- private function shareService (Definition $ definition , $ service , $ id )
1152
+ private function shareService (Definition $ definition , $ service , $ id, \ SplObjectStorage $ inlinedDefinitions )
1144
1153
{
1145
- if (null !== $ id && $ definition ->isShared () && self ::SCOPE_PROTOTYPE !== $ scope = $ definition ->getScope (false )) {
1154
+ if (!$ definition ->isShared () || self ::SCOPE_PROTOTYPE === $ scope = $ definition ->getScope ()) {
1155
+ return ;
1156
+ }
1157
+ if (null === $ id ) {
1158
+ $ inlinedDefinitions [$ definition ] = $ service ;
1159
+ } else {
1146
1160
if (self ::SCOPE_CONTAINER !== $ scope && !isset ($ this ->scopedServices [$ scope ])) {
1147
1161
throw new InactiveScopeException ($ id , $ scope );
1148
1162
}
0 commit comments