@@ -519,6 +519,14 @@ public function has($id)
519
519
*/
520
520
public function get ($ id , $ invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE )
521
521
{
522
+ return $ this ->doGet ($ id , $ invalidBehavior );
523
+ }
524
+
525
+ private function doGet ($ id , $ invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE , array &$ inlineServices = array ())
526
+ {
527
+ if (isset ($ inlineServices [$ id ])) {
528
+ return $ inlineServices [$ id ];
529
+ }
522
530
if (ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $ invalidBehavior ) {
523
531
return parent ::get ($ id , $ invalidBehavior );
524
532
}
@@ -527,7 +535,7 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
527
535
}
528
536
529
537
if (!isset ($ this ->definitions [$ id ]) && isset ($ this ->aliasDefinitions [$ id ])) {
530
- return $ this ->get ((string ) $ this ->aliasDefinitions [$ id ], $ invalidBehavior );
538
+ return $ this ->doGet ((string ) $ this ->aliasDefinitions [$ id ], $ invalidBehavior, $ inlineServices );
531
539
}
532
540
533
541
try {
@@ -544,7 +552,7 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
544
552
$ this ->{$ loading }[$ id ] = true ;
545
553
546
554
try {
547
- $ service = $ this ->createService ($ definition , new \ SplObjectStorage () , $ id );
555
+ $ service = $ this ->createService ($ definition , $ inlineServices , $ id );
548
556
} finally {
549
557
unset($ this ->{$ loading }[$ id ]);
550
558
}
@@ -978,10 +986,10 @@ public function findDefinition($id)
978
986
* @throws RuntimeException When the service is a synthetic service
979
987
* @throws InvalidArgumentException When configure callable is not callable
980
988
*/
981
- private function createService (Definition $ definition , \ SplObjectStorage $ inlinedDefinitions , $ id = null , $ tryProxy = true )
989
+ private function createService (Definition $ definition , array & $ inlineServices , $ id = null , $ tryProxy = true )
982
990
{
983
- if (null === $ id && isset ($ inlinedDefinitions [ $ definition ])) {
984
- return $ inlinedDefinitions [ $ definition ];
991
+ if (null === $ id && isset ($ inlineServices [ $ h = spl_object_hash ( $ definition) ])) {
992
+ return $ inlineServices [ $ h ];
985
993
}
986
994
987
995
if ($ definition instanceof ChildDefinition) {
@@ -1002,11 +1010,11 @@ private function createService(Definition $definition, \SplObjectStorage $inline
1002
1010
->instantiateProxy (
1003
1011
$ this ,
1004
1012
$ definition ,
1005
- $ id , function () use ($ definition , $ inlinedDefinitions , $ id ) {
1006
- return $ this ->createService ($ definition , $ inlinedDefinitions , $ id , false );
1013
+ $ id , function () use ($ definition , & $ inlineServices , $ id ) {
1014
+ return $ this ->createService ($ definition , $ inlineServices , $ id , false );
1007
1015
}
1008
1016
);
1009
- $ this ->shareService ($ definition , $ proxy , $ id , $ inlinedDefinitions );
1017
+ $ this ->shareService ($ definition , $ proxy , $ id , $ inlineServices );
1010
1018
1011
1019
return $ proxy ;
1012
1020
}
@@ -1017,15 +1025,15 @@ private function createService(Definition $definition, \SplObjectStorage $inline
1017
1025
require_once $ parameterBag ->resolveValue ($ definition ->getFile ());
1018
1026
}
1019
1027
1020
- $ arguments = $ this ->doResolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getArguments ())), $ inlinedDefinitions );
1028
+ $ arguments = $ this ->doResolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getArguments ())), $ inlineServices );
1021
1029
1022
1030
if (null !== $ id && $ definition ->isShared () && isset ($ this ->services [$ id ]) && ($ tryProxy || !$ definition ->isLazy ())) {
1023
1031
return $ this ->services [$ id ];
1024
1032
}
1025
1033
1026
1034
if (null !== $ factory = $ definition ->getFactory ()) {
1027
1035
if (is_array ($ factory )) {
1028
- $ factory = array ($ this ->doResolveServices ($ parameterBag ->resolveValue ($ factory [0 ]), $ inlinedDefinitions ), $ factory [1 ]);
1036
+ $ factory = array ($ this ->doResolveServices ($ parameterBag ->resolveValue ($ factory [0 ]), $ inlineServices ), $ factory [1 ]);
1029
1037
} elseif (!is_string ($ factory )) {
1030
1038
throw new RuntimeException (sprintf ('Cannot create service "%s" because of invalid factory ' , $ id ));
1031
1039
}
@@ -1051,26 +1059,26 @@ private function createService(Definition $definition, \SplObjectStorage $inline
1051
1059
1052
1060
if ($ tryProxy || !$ definition ->isLazy ()) {
1053
1061
// share only if proxying failed, or if not a proxy
1054
- $ this ->shareService ($ definition , $ service , $ id , $ inlinedDefinitions );
1062
+ $ this ->shareService ($ definition , $ service , $ id , $ inlineServices );
1055
1063
}
1056
1064
1057
- $ properties = $ this ->doResolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getProperties ())), $ inlinedDefinitions );
1065
+ $ properties = $ this ->doResolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getProperties ())), $ inlineServices );
1058
1066
foreach ($ properties as $ name => $ value ) {
1059
1067
$ service ->$ name = $ value ;
1060
1068
}
1061
1069
1062
1070
foreach ($ definition ->getMethodCalls () as $ call ) {
1063
- $ this ->callMethod ($ service , $ call , $ inlinedDefinitions );
1071
+ $ this ->callMethod ($ service , $ call , $ inlineServices );
1064
1072
}
1065
1073
1066
1074
if ($ callable = $ definition ->getConfigurator ()) {
1067
1075
if (is_array ($ callable )) {
1068
1076
$ callable [0 ] = $ parameterBag ->resolveValue ($ callable [0 ]);
1069
1077
1070
1078
if ($ callable [0 ] instanceof Reference) {
1071
- $ callable [0 ] = $ this ->get ((string ) $ callable [0 ], $ callable [0 ]->getInvalidBehavior ());
1079
+ $ callable [0 ] = $ this ->doGet ((string ) $ callable [0 ], $ callable [0 ]->getInvalidBehavior (), $ inlineServices );
1072
1080
} elseif ($ callable [0 ] instanceof Definition) {
1073
- $ callable [0 ] = $ this ->createService ($ callable [0 ], $ inlinedDefinitions );
1081
+ $ callable [0 ] = $ this ->createService ($ callable [0 ], $ inlineServices );
1074
1082
}
1075
1083
}
1076
1084
@@ -1094,14 +1102,14 @@ private function createService(Definition $definition, \SplObjectStorage $inline
1094
1102
*/
1095
1103
public function resolveServices ($ value )
1096
1104
{
1097
- return $ this ->doResolveServices ($ value, new \ SplObjectStorage () );
1105
+ return $ this ->doResolveServices ($ value );
1098
1106
}
1099
1107
1100
- private function doResolveServices ($ value , \ SplObjectStorage $ inlinedDefinitions )
1108
+ private function doResolveServices ($ value , array & $ inlineServices = array () )
1101
1109
{
1102
1110
if (is_array ($ value )) {
1103
1111
foreach ($ value as $ k => $ v ) {
1104
- $ value [$ k ] = $ this ->doResolveServices ($ v , $ inlinedDefinitions );
1112
+ $ value [$ k ] = $ this ->doResolveServices ($ v , $ inlineServices );
1105
1113
}
1106
1114
} elseif ($ value instanceof ServiceClosureArgument) {
1107
1115
$ reference = $ value ->getValues ()[0 ];
@@ -1117,7 +1125,7 @@ private function doResolveServices($value, \SplObjectStorage $inlinedDefinitions
1117
1125
}
1118
1126
}
1119
1127
foreach (self ::getInitializedConditionals ($ v ) as $ s ) {
1120
- if (!$ this ->get ($ s , ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE )) {
1128
+ if (!$ this ->doGet ($ s , ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE )) {
1121
1129
continue 2 ;
1122
1130
}
1123
1131
}
@@ -1133,7 +1141,7 @@ private function doResolveServices($value, \SplObjectStorage $inlinedDefinitions
1133
1141
}
1134
1142
}
1135
1143
foreach (self ::getInitializedConditionals ($ v ) as $ s ) {
1136
- if (!$ this ->get ($ s , ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE )) {
1144
+ if (!$ this ->doGet ($ s , ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE )) {
1137
1145
continue 2 ;
1138
1146
}
1139
1147
}
@@ -1144,9 +1152,9 @@ private function doResolveServices($value, \SplObjectStorage $inlinedDefinitions
1144
1152
return $ count ;
1145
1153
});
1146
1154
} elseif ($ value instanceof Reference) {
1147
- $ value = $ this ->get ((string ) $ value , $ value ->getInvalidBehavior ());
1155
+ $ value = $ this ->doGet ((string ) $ value , $ value ->getInvalidBehavior (), $ inlineServices );
1148
1156
} elseif ($ value instanceof Definition) {
1149
- $ value = $ this ->createService ($ value , $ inlinedDefinitions );
1157
+ $ value = $ this ->createService ($ value , $ inlineServices );
1150
1158
} elseif ($ value instanceof Expression) {
1151
1159
$ value = $ this ->getExpressionLanguage ()->evaluate ($ value , array ('container ' => $ this ));
1152
1160
}
@@ -1443,20 +1451,20 @@ private function getProxyInstantiator(): InstantiatorInterface
1443
1451
return $ this ->proxyInstantiator ;
1444
1452
}
1445
1453
1446
- private function callMethod ($ service , $ call , \ SplObjectStorage $ inlinedDefinitions )
1454
+ private function callMethod ($ service , $ call , array & $ inlineServices )
1447
1455
{
1448
1456
foreach (self ::getServiceConditionals ($ call [1 ]) as $ s ) {
1449
1457
if (!$ this ->has ($ s )) {
1450
1458
return ;
1451
1459
}
1452
1460
}
1453
1461
foreach (self ::getInitializedConditionals ($ call [1 ]) as $ s ) {
1454
- if (!$ this ->get ($ s , ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE )) {
1462
+ if (!$ this ->doGet ($ s , ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE , $ inlineServices )) {
1455
1463
return ;
1456
1464
}
1457
1465
}
1458
1466
1459
- call_user_func_array (array ($ service , $ call [0 ]), $ this ->doResolveServices ($ this ->getParameterBag ()->unescapeValue ($ this ->getParameterBag ()->resolveValue ($ call [1 ])), $ inlinedDefinitions ));
1467
+ call_user_func_array (array ($ service , $ call [0 ]), $ this ->doResolveServices ($ this ->getParameterBag ()->unescapeValue ($ this ->getParameterBag ()->resolveValue ($ call [1 ])), $ inlineServices ));
1460
1468
}
1461
1469
1462
1470
/**
@@ -1466,14 +1474,11 @@ private function callMethod($service, $call, \SplObjectStorage $inlinedDefinitio
1466
1474
* @param object $service
1467
1475
* @param string|null $id
1468
1476
*/
1469
- private function shareService (Definition $ definition , $ service , $ id , \ SplObjectStorage $ inlinedDefinitions )
1477
+ private function shareService (Definition $ definition , $ service , $ id , array & $ inlineServices )
1470
1478
{
1471
- if (!$ definition ->isShared ()) {
1472
- return ;
1473
- }
1474
- if (null === $ id ) {
1475
- $ inlinedDefinitions [$ definition ] = $ service ;
1476
- } else {
1479
+ $ inlineServices [null !== $ id ? $ id : spl_object_hash ($ definition )] = $ service ;
1480
+
1481
+ if (null !== $ id && $ definition ->isShared ()) {
1477
1482
$ this ->services [$ id ] = $ service ;
1478
1483
unset($ this ->loading [$ id ], $ this ->alreadyLoading [$ id ]);
1479
1484
}
0 commit comments