18
18
use Symfony \Component \DependencyInjection \Compiler \Compiler ;
19
19
use Symfony \Component \DependencyInjection \Compiler \CompilerPassInterface ;
20
20
use Symfony \Component \DependencyInjection \Compiler \PassConfig ;
21
+ use Symfony \Component \DependencyInjection \Compiler \ResolveEnvPlaceholdersPass ;
21
22
use Symfony \Component \DependencyInjection \Exception \BadMethodCallException ;
22
23
use Symfony \Component \DependencyInjection \Exception \InvalidArgumentException ;
23
24
use Symfony \Component \DependencyInjection \Exception \LogicException ;
@@ -729,9 +730,7 @@ public function compile(/*$resolveEnvPlaceholders = false*/)
729
730
$ bag = $ this ->getParameterBag ();
730
731
731
732
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 );
735
734
}
736
735
737
736
$ compiler ->compile ($ this );
@@ -744,11 +743,15 @@ public function compile(/*$resolveEnvPlaceholders = false*/)
744
743
745
744
$ this ->extensionConfigs = array ();
746
745
747
- parent ::compile ();
748
-
749
746
if ($ bag instanceof EnvPlaceholderParameterBag) {
747
+ if ($ resolveEnvPlaceholders ) {
748
+ $ this ->parameterBag = new ParameterBag ($ this ->resolveEnvPlaceholders ($ bag ->all (), true ));
749
+ }
750
+
750
751
$ this ->envPlaceholders = $ bag ->getEnvPlaceholders ();
751
752
}
753
+
754
+ parent ::compile ();
752
755
}
753
756
754
757
/**
@@ -1313,12 +1316,19 @@ public function resolveEnvPlaceholders($value, $format = null, array &$usedEnvs
1313
1316
foreach ($ envPlaceholders as $ env => $ placeholders ) {
1314
1317
foreach ($ placeholders as $ placeholder ) {
1315
1318
if (false !== stripos ($ value , $ placeholder )) {
1316
- if (true !== $ format ) {
1319
+ if (true === $ format ) {
1320
+ $ resolved = $ bag ->escapeValue ($ this ->getEnv ($ env ));
1321
+ } else {
1317
1322
$ resolved = sprintf ($ format , $ env );
1318
- } elseif ($ placeholder === $ resolved = $ bag ->escapeValue ($ this ->getEnv ($ env ))) {
1319
- $ resolved = $ bag ->all ()[strtolower ("env( $ env) " )];
1320
1323
}
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
+ }
1322
1332
$ usedEnvs [$ env ] = $ env ;
1323
1333
$ this ->envCounters [$ env ] = isset ($ this ->envCounters [$ env ]) ? 1 + $ this ->envCounters [$ env ] : 1 ;
1324
1334
}
@@ -1393,6 +1403,26 @@ public static function getServiceConditionals($value)
1393
1403
return $ services ;
1394
1404
}
1395
1405
1406
+ /**
1407
+ * {@inheritdoc}
1408
+ */
1409
+ protected function getEnv ($ name )
1410
+ {
1411
+ $ value = parent ::getEnv ($ name );
1412
+
1413
+ if ($ this ->getParameterBag () instanceof EnvPlaceholderParameterBag) {
1414
+ foreach ($ this ->getParameterBag ()->getEnvPlaceholders () as $ env => $ placeholders ) {
1415
+ if (isset ($ placeholders [$ value ])) {
1416
+ $ bag = new ParameterBag ($ this ->getParameterBag ()->all ());
1417
+
1418
+ return $ bag ->unescapeValue ($ bag ->get ("env( $ name) " ));
1419
+ }
1420
+ }
1421
+ }
1422
+
1423
+ return $ value ;
1424
+ }
1425
+
1396
1426
/**
1397
1427
* Retrieves the currently set proxy instantiator or instantiates one.
1398
1428
*
0 commit comments