@@ -862,17 +862,15 @@ private function createService(Definition $definition, $id, $tryProxy = true)
862862return $ proxy ;
863863 }
864864
865- $ parameterBag =$ this ->getParameterBag ();
866-
867865if (null !==$ definition ->getFile ()) {
868- require_once $ parameterBag ->resolveValue ($ definition ->getFile ());
866+ require_once $ this ->resolveValue ($ definition ->getFile ());
869867 }
870868
871- $ arguments =$ this ->resolveServices ($ parameterBag -> unescapeValue ( $ parameterBag -> resolveValue ($ definition ->getArguments () )));
869+ $ arguments =$ this ->resolveServices ($ this -> resolveValue ($ definition ->getArguments ()));
872870
873871if (null !==$ factory =$ definition ->getFactory ()) {
874872if (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 ]);
876874 }elseif (!is_string ($ factory )) {
877875throw new RuntimeException (sprintf ('Cannot create service "%s" because of invalid factory ' ,$ id ));
878876 }
@@ -887,7 +885,7 @@ private function createService(Definition $definition, $id, $tryProxy = true)
887885 }
888886 }
889887 }else {
890- $ r =new \ReflectionClass ($ parameterBag ->resolveValue ($ definition ->getClass ()));
888+ $ r =new \ReflectionClass ($ this ->resolveValue ($ definition ->getClass ()));
891889
892890$ service =null ===$ r ->getConstructor () ?$ r ->newInstance () :$ r ->newInstanceArgs ($ arguments );
893891
@@ -901,7 +899,7 @@ private function createService(Definition $definition, $id, $tryProxy = true)
901899$ this ->shareService ($ definition ,$ service ,$ id );
902900 }
903901
904- $ properties =$ this ->resolveServices ($ parameterBag -> unescapeValue ( $ parameterBag -> resolveValue ($ definition ->getProperties () )));
902+ $ properties =$ this ->resolveServices ($ this -> resolveValue ($ definition ->getProperties ()));
905903foreach ($ propertiesas $ name =>$ value ) {
906904$ service ->$ name =$ value ;
907905 }
@@ -912,7 +910,7 @@ private function createService(Definition $definition, $id, $tryProxy = true)
912910
913911if ($ callable =$ definition ->getConfigurator ()) {
914912if (is_array ($ callable )) {
915- $ callable [0 ] =$ parameterBag ->resolveValue ($ callable [0 ]);
913+ $ callable [0 ] =$ this ->resolveValue ($ callable [0 ]);
916914
917915if ($ callable [0 ]instanceof Reference) {
918916$ callable [0 ] =$ this ->get ((string )$ callable [0 ],$ callable [0 ]->getInvalidBehavior ());
@@ -1028,17 +1026,26 @@ public function getExpressionLanguageProviders()
10281026/**
10291027 * Resolves env parameter placeholders in a string.
10301028 *
1031- * @param mixed $value The value to resolve
1032- * @param string|null $format A sprintf() formatto use as replacement for envplaceholders or null touse thedefault 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 Acallable or sprintf() formatreturning the replacement foreach envvar name or null toresolve back to theoriginal "%env(VAR)%" format
1031+ * @param array &$usedEnvs Env vars found while resolving are added to this array
10341032 *
10351033 * @return string The string with env parameters resolved
1034+ *
1035+ * @throws InvalidArgumentException When $format is neither a string nor a callable nor null
10361036 */
10371037public function resolveEnvPlaceholders ($ value ,$ format =null ,array &$ usedEnvs =null )
10381038 {
10391039if (null ===$ format ) {
10401040$ format ='%%env(%s)%% ' ;
10411041 }
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+ }
10421049
10431050if (is_array ($ value )) {
10441051$ result =array ();
@@ -1055,11 +1062,15 @@ public function resolveEnvPlaceholders($value, $format = null, array &$usedEnvs
10551062
10561063$ bag =$ this ->getParameterBag ();
10571064$ envPlaceholders =$ baginstanceof EnvPlaceholderParameterBag ?$ bag ->getEnvPlaceholders () :$ this ->envPlaceholders ;
1065+ $ resolved =array ();
10581066
10591067foreach ($ envPlaceholdersas $ env =>$ placeholders ) {
10601068foreach ($ placeholdersas $ placeholder ) {
10611069if (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 );
10631074$ usedEnvs [$ env ] =$ env ;
10641075$ this ->envCounters [$ env ] =isset ($ this ->envCounters [$ env ]) ?1 +$ this ->envCounters [$ env ] :1 ;
10651076 }
@@ -1088,6 +1099,23 @@ public function getEnvCounters()
10881099return $ this ->envCounters ;
10891100 }
10901101
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+
10911119/**
10921120 * Returns the Service Conditionals.
10931121 *
@@ -1134,7 +1162,7 @@ private function callMethod($service, $call)
11341162 }
11351163 }
11361164
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 ])));
11381166 }
11391167
11401168/**