@@ -862,17 +862,15 @@ private function createService(Definition $definition, $id, $tryProxy = true)
862
862
return $ proxy ;
863
863
}
864
864
865
- $ parameterBag = $ this ->getParameterBag ();
866
-
867
865
if (null !== $ definition ->getFile ()) {
868
- require_once $ parameterBag ->resolveValue ($ definition ->getFile ());
866
+ require_once $ this ->resolveValue ($ definition ->getFile ());
869
867
}
870
868
871
- $ arguments = $ this ->resolveServices ($ parameterBag -> unescapeValue ( $ parameterBag -> resolveValue ($ definition ->getArguments () )));
869
+ $ arguments = $ this ->resolveServices ($ this -> resolveValue ($ definition ->getArguments ()));
872
870
873
871
if (null !== $ factory = $ definition ->getFactory ()) {
874
872
if (is_array ($ factory )) {
875
- $ factory = array ($ this ->resolveServices ($ parameterBag ->resolveValue ($ factory [0 ])), $ factory [1 ]);
873
+ $ factory = array ($ this ->resolveServices ($ this ->resolveValue ($ factory [0 ])), $ factory [1 ]);
876
874
} elseif (!is_string ($ factory )) {
877
875
throw new RuntimeException (sprintf ('Cannot create service "%s" because of invalid factory ' , $ id ));
878
876
}
@@ -887,7 +885,7 @@ private function createService(Definition $definition, $id, $tryProxy = true)
887
885
}
888
886
}
889
887
} else {
890
- $ r = new \ReflectionClass ($ parameterBag ->resolveValue ($ definition ->getClass ()));
888
+ $ r = new \ReflectionClass ($ this ->resolveValue ($ definition ->getClass ()));
891
889
892
890
$ service = null === $ r ->getConstructor () ? $ r ->newInstance () : $ r ->newInstanceArgs ($ arguments );
893
891
@@ -905,14 +903,14 @@ private function
10000
createService(Definition $definition, $id, $tryProxy = true)
905
903
$ this ->callMethod ($ service , $ call );
906
904
}
907
905
908
- $ properties = $ this ->resolveServices ($ parameterBag -> unescapeValue ( $ parameterBag -> resolveValue ($ definition ->getProperties () )));
906
+ $ properties = $ this ->resolveServices ($ this -> resolveValue ($ definition ->getProperties ()));
909
907
foreach ($ properties as $ name => $ value ) {
910
908
$ service ->$ name = $ value ;
911
909
}
912
910
913
911
if ($ callable = $ definition ->getConfigurator ()) {
914
912
if (is_array ($ callable )) {
915
- $ callable [0 ] = $ parameterBag ->resolveValue ($ callable [0 ]);
913
+ $ callable [0 ] = $ this ->resolveValue ($ callable [0 ]);
916
914
917
915
if ($ callable [0 ] instanceof Reference) {
918
916
$ callable [0 ] = $ this ->get ((string ) $ callable [0 ], $ callable [0 ]->getInvalidBehavior ());
@@ -1028,11 +1026,13 @@ public function getExpressionLanguageProviders()
1028
1026
/**
1029
1027
* Resolves env parameter placeholders in a string.
1030
1028
*
1031
- * @param string $string The string to resolve
1032
- * @param string|null $format A sprintf() format to use as replacement for env placeholders or null to use the default parameter format
1033
- * @param array &$usedEnvs Env vars found while resolving are added to this array
1029
+ * @param string $string The string to resolve
1030
+ * @param string|callable| null $format A callable or sprintf() format returning the replacement for each env var name or null to resolve back to the original "%env(VAR)%" format
1031
+ * @param array &$usedEnvs Env vars found while resolving are added to this array
1034
1032
*
1035
1033
* @return string The string with env parameters resolved
1034
+ *
1035
+ * @throws InvalidArgumentException When $format is neither a string nor a callable nor null
1036
1036
*/
1037
1037
public function resolveEnvPlaceholders ($ string , $ format = null , array &$ usedEnvs = null )
1038
1038
{
@@ -1042,11 +1042,22 @@ public function resolveEnvPlaceholders($string, $format = null, array &$usedEnvs
1042
1042
if (null === $ format ) {
1043
1043
$ format = '%%env(%s)%% ' ;
1044
1044
}
1045
+ if (is_string ($ format )) {
1046
+ $ format = function ($ env ) use ($ format ) {
1047
+ return sprintf ($ format , $ env );
1048
+ };
1049
+ } elseif (!is_callable ($ format )) {
1050
+ throw new InvalidArgumentException ('$format must string, callable or null. ' );
1051
+ }
1052
+ $ resolved = array ();
1045
1053
1046
1054
foreach ($ envPlaceholders as $ env => $ placeholders ) {
1047
1055
foreach ($ placeholders as $ placeholder ) {
1048
1056
if (false !== stripos ($ string , $ placeholder )) {
1049
- $ string = str_ireplace ($ placeholder , sprintf ($ format , $ env ), $ string );
1057
+ if (!isset ($ resolved [$ env ])) {
1058
+ $ resolved [$ env ] = array ((string ) call_user_func ($ format , $ env ));
1059
+ }
1060
+ $ string = str_ireplace ($ placeholder , $ resolved [$ env ][0 ], $ string );
1050
1061
$ usedEnvs [$ env ] = $ env ;
1051
1062
$ this ->envCounters [$ env ] = isset ($ this ->envCounters [$ env ]) ? 1 + $ this ->envCounters [$ env ] : 1 ;
1052
1063
}
@@ -1075,6 +1086,19 @@ public function getEnvCounters()
1075
1086
return $ this ->envCounters ;
1076
1087
}
1077
1088
1089
+ /**
1090
+ * Replaces parameter placeholders (%name%) and unescapes percent signs.
1091
+ *
1092
+ * @param mixed $value A value
1093
+ * @param bool $resolveEnvValues Whether %env(VAR)% parameters should be replaced by the value of the corresponding environment variable or not
1094
+ *
1095
+ * @return mixed The resolved value
1096
+ */
1097
+ public function resolveValue ($ value , $ resolveEnvValues = false )
1098
+ {
1099
+ return $ this ->doResolveValue ($ value , $ resolveEnvValues , array ());
1100
+ }
1101
+
1078
1102
/**
1079
1103
* Returns the Service Conditionals.
1080
1104
*
@@ -1121,7 +1145,7 @@ private function callMethod($service, $call)
1121
1145
}
1122
1146
}
1123
1147
1124
- call_user_func_array (array ($ service , $ call [0 ]), $ this ->resolveServices ($ this ->getParameterBag ()-> unescapeValue ( $ this -> getParameterBag ()-> resolveValue ($ call [1 ]) )));
1148
+ call_user_func_array (array ($ service , $ call [0 ]), $ this ->resolveServices ($ this ->resolveValue ($ call [1 ])));
1125
1149
}
1126
1150
1127
1151
/**
@@ -1149,4 +1173,35 @@ private function getExpressionLanguage()
1149
1173
1150
1174
return $ this ->expressionLanguage ;
1151
1175
}
1176
+
1177
+ /**
1178
+ * Replaces parameter placeholders (%name%) and unescapes percent signs.
1179
+ *
1180
+ * @param mixed $value A value
1181
+ * @param bool $resolveEnvValues Whether %env(VAR)% parameters should be replaced by the value of the corresponding environment variable or not
1182
+ * @param array $resolving An array of keys that are being resolved (used internally to detect circular references)
1183
+ *
1184
+ * @return mixed The resolved value
1185
+ */
1186
+ private function doResolveValue ($ value , $ resolveEnvValues , array $ resolving )
1187
+ {
1188
+ if (is_array ($ value )) {
1189
+ $ args = array ();
1190
+ foreach ($ value as $ k => $ v ) {
1191
+ $ args [$ this ->doResolveValue ($ k , $ resolveEnvValues , $ resolving )] = $ this ->doResolveValue ($ v , $ resolveEnvValues , $ resolving );
1192
+ }
1193
+
1194
+ return $ args ;
1195
+ }
1196
+
1197
+ if (!is_string ($ value )) {
1198
+ return $ value ;
1199
+ }
1200
+
1201
+ $ parameterBag = $ this ->getParameterBag ();
1202
+ $ value = $ parameterBag ->resolveString ($ value , $ resolving );
1203
+ $ value = $ parameterBag ->unescapeValue ($ value );
1204
+
1205
+ return $ resolveEnvValues ? $ this ->resolveEnvPlaceholders ($ value , array ($ this , 'getEnv ' )) : $ value ;
1206
+ }
1152
1207
}
0 commit comments