|
21 | 21 | use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
|
22 | 22 | use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
|
23 | 23 | use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
|
| 24 | +use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag; |
24 | 25 | use Symfony\Component\Config\Resource\FileResource;
|
25 | 26 | use Symfony\Component\Config\Resource\ResourceInterface;
|
26 | 27 | use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\InstantiatorInterface;
|
@@ -89,6 +90,16 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
|
89 | 90 | */
|
90 | 91 | private $usedTags = array();
|
91 | 92 |
|
| 93 | + /** |
| 94 | + * @var string[][] A map of env var names to their placeholders |
| 95 | + */ |
| 96 | + private $envPlaceholders = array(); |
| 97 | + |
| 98 | + /** |
| 99 | + * @var int[] A map of env vars to their resolution counter. |
| 100 | + */ |
| 101 | + private $envCounters = array(); |
| 102 | + |
92 | 103 | private $compiled = false;
|
93 | 104 |
|
94 | 105 | /**
|
@@ -481,6 +492,18 @@ public function merge(ContainerBuilder $container)
|
481 | 492 |
|
482 | 493 | $this->extensionConfigs[$name] = array_merge($this->extensionConfigs[$name], $container->getExtensionConfig($name));
|
483 | 494 | }
|
| 495 | + |
| 496 | + if ($this->getParameterBag() instanceof EnvPlaceholderParameterBag && $container->getParameterBag() instanceof EnvPlaceholderParameterBag) { |
| 497 | + $this->getParameterBag()->mergeEnvPlaceholders($container->getParameterBag()); |
| 498 | + } |
| 499 | + |
| 500 | + foreach ($container->envCounters as $env => $count) { |
| 501 | + if (!isset($this->envCounters[$env])) { |
| 502 | + $this->envCounters[$env] = $count; |
| 503 | + } else { |
| 504 | + $this->envCounters[$env] += $count; |
| 505 | + } |
| 506 | + } |
484 | 507 | }
|
485 | 508 |
|
486 | 509 | /**
|
@@ -551,8 +574,11 @@ public function compile()
|
551 | 574 | }
|
552 | 575 |
|
553 | 576 | $this->extensionConfigs = array();
|
| 577 | + $bag = $this->getParameterBag(); |
554 | 578 |
|
555 | 579 | parent::compile();
|
| 580 | + |
| 581 | + $this->envPlaceholders = $bag instanceof EnvPlaceholderParameterBag ? $bag->getEnvPlaceholders() : array(); |
556 | 582 | }
|
557 | 583 |
|
558 | 584 | /**
|
@@ -995,6 +1021,56 @@ public function getExpressionLanguageProviders()
|
995 | 1021 | return $this->expressionLanguageProviders;
|
996 | 1022 | }
|
997 | 1023 |
|
| 1024 | + /** |
| 1025 | + * Resolves env parameter placeholders in a string. |
| 1026 | + * |
| 1027 | + * @param string $string The string to resolve |
| 1028 | + * @param string|null $format A sprintf() format to use as replacement for env placeholders or null to use the default parameter format |
| 1029 | + * @param array &$usedEnvs Env vars found while resolving are added to this array |
| 1030 | + * |
| 1031 | + * @return string The string with env parameters resolved |
| 1032 | + */ |
| 1033 | + public function resolveEnvPlaceholders($string, $format = null, array &$usedEnvs = null) |
| 1034 | + { |
| 1035 | + $bag = $this->getParameterBag(); |
| 1036 | + $envPlaceholders = $bag instanceof EnvPlaceholderParameterBag ? $bag->getEnvPlaceholders() : $this->envPlaceholders; |
| 1037 | + |
| 1038 | + if (null === $format) { |
| 1039 | + $format = '%%env(%s)%%'; |
| 1040 | + } |
| 1041 | + |
| 1042 | + foreach ($envPlaceholders as $env => $placeholders) { |
| 1043 | + foreach ($placeholders as $placeholder) { |
| 1044 | + if (false !== stripos($string, $placeholder)) { |
| 1045 | + $string = str_ireplace($placeholder, sprintf($format, $env), $string); |
| 1046 | + $usedEnvs[$env] = $env; |
| 1047 | + $this->envCounters[$env] = isset($this->envCounters[$env]) ? 1 + $this->envCounters[$env] : 1; |
| 1048 | + } |
| 1049 | + } |
| 1050 | + } |
| 1051 | + |
| 1052 | + return $string; |
| 1053 | + } |
| 1054 | + |
| 1055 | + /** |
| 1056 | + * Get statistics about env usage. |
| 1057 | + * |
| 1058 | + * @return int[] The number of time each env vars has been resolved |
| 1059 | + */ |
| 1060 | + public function getEnvCounters() |
| 1061 | + { |
| 1062 | + $bag = $this->getParameterBag(); |
| 1063 | + $envPlaceholders = $bag instanceof EnvPlaceholderParameterBag ? $bag->getEnvPlaceholders() : $this->envPlaceholders; |
| 1064 | + |
| 1065 | + foreach ($envPlaceholders as $env => $placeholders) { |
| 1066 | + if (!isset($this->envCounters[$env])) { |
| 1067 | + $this->envCounters[$env] = 0; |
| 1068 | + } |
| 1069 | + } |
| 1070 | + |
| 1071 | + return $this->envCounters; |
| 1072 | + } |
| 1073 | + |
998 | 1074 | /**
|
999 | 1075 | * Returns the Service Conditionals.
|
1000 | 1076 | *
|
|
0 commit comments