1818use Symfony \Component \DependencyInjection \Compiler \Compiler ;
1919use Symfony \Component \DependencyInjection \Compiler \CompilerPassInterface ;
2020use Symfony \Component \DependencyInjection \Compiler \PassConfig ;
21+ use Symfony \Component \DependencyInjection \Compiler \ResolveEnvPlaceholdersPass ;
2122use Symfony \Component \DependencyInjection \Exception \BadMethodCallException ;
2223use Symfony \Component \DependencyInjection \Exception \InvalidArgumentException ;
2324use Symfony \Component \DependencyInjection \Exception \LogicException ;
@@ -729,9 +730,7 @@ public function compile(/*$resolveEnvPlaceholders = false*/)
729730 $ bag = $ this ->getParameterBag ();
730731
731732 if ($ resolveEnvPlaceholders && $ bag instanceof EnvPlaceholderParameterBag) {
732- $ this ->parameterBag = new ParameterBag ($ bag ->resolveEnvReferences ($ bag ->all ()));
733- $ this ->envPlaceholders = $ bag ->getEnvPlaceholders ();
734- $ this ->parameterBag = $ bag = new ParameterBag ($ this ->resolveEnvPlaceholders ($ this ->parameterBag ->all (), true ));
733+ $ compiler ->addPass (new ResolveEnvPlaceholdersPass (), PassConfig::TYPE_AFTER_REMOVING , -1000 );
735734 }
736735
737736 $ compiler ->compile ($ this );
@@ -744,11 +743,15 @@ public function compile(/*$resolveEnvPlaceholders = false*/)
744743
745744 $ this ->extensionConfigs = array ();
746745
747- parent ::compile ();
748-
749746 if ($ bag instanceof EnvPlaceholderParameterBag) {
747+ if ($ resolveEnvPlaceholders ) {
748+ $ this ->parameterBag = new ParameterBag ($ this ->resolveEnvPlaceholders ($ bag ->all (), true ));
749+ }
750+
750751 $ this ->envPlaceholders = $ bag ->getEnvPlaceholders ();
751752 }
753+
754+ parent ::compile ();
752755 }
753756
754757 /**
@@ -1313,12 +1316,19 @@ public function resolveEnvPlaceholders($value, $format = null, array &$usedEnvs
13131316 foreach ($ envPlaceholders as $ env => $ placeholders ) {
13141317 foreach ($ placeholders as $ placeholder ) {
13151318 if (false !== stripos ($ value , $ placeholder )) {
1316- if (true !== $ format ) {
1319+ if (true === $ format ) {
1320+ $ resolved = $ bag ->escapeValue ($ this ->getEnv ($ env ));
1321+ } else {
13171322 $ resolved = sprintf ($ format , $ env );
1318- } elseif ($ placeholder === $ resolved = $ bag ->escapeValue ($ this ->getEnv ($ env ))) {
1319- $ resolved = $ bag ->all ()[strtolower ("env( $ env) " )];
13201323 }
1321- $ value = str_ireplace ($ placeholder , $ resolved , $ value );
1324+ if ($ placeholder === $ value ) {
1325+ $ value = $ resolved ;
1326+ } else {
1327+ if (!is_string ($ resolved ) && !is_numeric ($ resolved )) {
1328+ throw new RuntimeException (sprintf ('A string value must be composed of strings and/or numbers, but found parameter "env(%s)" of type %s inside string value "%s". ' , $ env , gettype ($ resolved ), $ value ));
1329+ }
1330+ $ value = str_ireplace ($ placeholder , $ resolved , $ value );
1331+ }
13221332 $ usedEnvs [$ env ] = $ env ;
13231333 $ this ->envCounters [$ env ] = isset ($ this ->envCounters [$ env ]) ? 1 + $ this ->envCounters [$ env ] : 1 ;
13241334 }
@@ -1393,6 +1403,28 @@ public static function getServiceConditionals($value)
13931403 return $ services ;
13941404 }
13951405
1406+ /**
1407+ * {@inheritdoc}
1408+ */
1409+ protected function getEnv ($ name )
1410+ {
1411+ $ value = parent ::getEnv ($ name );
1412+
1413+ if (!is_string ($ value ) || !$ this ->getParameterBag () instanceof EnvPlaceholderParameterBag) {
1414+ return $ value ;
1415+ }
1416+
1417+ foreach ($ this ->getParameterBag ()->getEnvPlaceholders () as $ env => $ placeholders ) {
1418+ if (isset ($ placeholders [$ value ])) {
1419+ $ bag = new ParameterBag ($ this ->getParameterBag ()->all ());
1420+
1421+ return $ bag ->unescapeValue ($ bag ->get ("env( $ name) " ));
1422+ }
1423+ }
1424+
1425+ return $ value ;
1426+ }
1427+
13961428 /**
13971429 * Retrieves the currently set proxy instantiator or instantiates one.
13981430 *
0 commit comments