@@ -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
@@ -901,7 +899,7 @@ private function createService(Definition $definition, $id, $tryProxy = true)
901
899
$ this ->shareService ($ definition , $ service , $ id );
902
900
}
903
901
904
- $ properties = $ this ->resolveServices ($ parameterBag -> unescapeValue ( $ parameterBag -> resolveValue ($ definition ->getProperties () )));
902
+ $ properties = $ this ->resolveServices ($ this -> resolveValue ($ definition ->getProperties ()));
905
903
foreach ($ properties as $ name => $ value ) {
906
904
$ service ->$ name = $ value ;
907
905
}
@@ -912,7 +910,7 @@ private function createService(Definition $definition, $id, $tryProxy = true)
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,17 +1026,26 @@ public function getExpressionLanguageProviders()
1028
1026
/**
1029
1027
* Resolves env parameter placeholders in a string.
1030
1028
*
1031
- * @param mixed $value The value 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 mixed $value The value 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 ($ value , $ format = null , array &$ usedEnvs = null )
1038
1038
{
1039
1039
if (null === $ format ) {
1040
1040
$ format = '%%env(%s)%% ' ;
1041
1041
}
1042
+ if (is_string ($ format )) {
1043
+ $ format = function ($ env ) use ($ format ) {
1044
+ return sprintf ($ format , $ env );
1045
+ };
1046
+ } elseif (!is_callable ($ format )) {
1047
+ throw new InvalidArgumentException ('$format must string, callable or null. ' );
1048
+ }
1042
1049
1043
1050
if (is_array ($ value )) {
1044
1051
$ result = array ();
@@ -1055,11 +1062,15 @@ public function resolveEnvPlaceholders($value, $format = null, array &$usedEnvs
1055
1062
1056
1063
$ bag = $ this ->getParameterBag ();
1057
1064
$ envPlaceholders = $ bag instanceof EnvPlaceholderParameterBag ? $ bag ->getEnvPlaceholders () : $ this ->envPlaceholders ;
1065
+ $ resolved = array ();
1058
1066
1059
1067
foreach ($ envPlaceholders as $ env => $ placeholders ) {
1060
1068
foreach ($ placeholders as $ placeholder ) {
1061
1069
if (false !== stripos ($ value , $ placeholder )) {
1062
- $ value = str_ireplace ($ placeholder , sprintf ($ format , $ env ), $ value );
1070
+ if (!isset ($ resolved [$ env ])) {
1071
+ $ resolved [$ env ] = array ((string ) call_user_func ($ format , $ env ));
1072
+ }
1073
+ $ value = str_ireplace ($ placeholder , $ resolved [$ env ][0 ], $ value );
1063
1074
$ usedEnvs [$ env ] = $ env ;
1064
1075
$ this ->envCounters [$ env ] = isset ($ this ->envCounters [$ env ]) ? 1 + $ this ->envCounters [$ env ] : 1 ;
1065
1076
}
@@ -1088,6 +1099,23 @@ public function getEnvCounters()
1088
1099
return $ this ->envCounters ;
1089
1100
}
1090
1101
1102
+ /**
1103
+ * Replaces parameter placeholders (%name%) and unescapes percent signs.
1104
+ *
1105
+ * @param mixed $value A value
1106
+ * @param bool $resolveEnvValues Whether %env(VAR)% parameters should be replaced by the value of the corresponding environment variable or not
1107
+ *
1108
+ * @return mixed The resolved value
1109
+ */
1110
+ public function resolveValue ($ value , $ resolveEnvValues = false )
1111
+ {
1112
+ $ parameterBag = $ this ->getParameterBag ();
1113
+ $ value = $ parameterBag ->resolveValue ($ value );
1114
+ $ value = $ parameterBag ->unescapeValue ($ value );
1115
+
1116
+ return $ resolveEnvValues ? $ this ->resolveEnvPlaceholders ($ value , array ($ this , 'getEnv ' )) : $ value ;
1117
+ }
1118
+
1091
1119
/**
1092
1120
* Returns the Service Conditionals.
1093
1121
*
@@ -1134,7 +1162,7 @@ private function callMethod($service, $call)
1134
1162
}
1135
1163
}
1136
1164
1137
- call_user_func_array (array ($ service , $ call [0 ]), $ this ->resolveServices ($ this ->getParameterBag ()-> unescapeValue ( $ this -> getParameterBag ()-> resolveValue ($ call [1 ]) )));
1165
+ call_user_func_array (array ($ service , $ call [0 ]), $ this ->resolveServices ($ this ->resolveValue ($ call [1 ])));
1138
1166
}
1139
1167
1140
1168
/**
0 commit comments