8000 [DI] Add ContainerBuilder::resolveValue($value, $resolveEnvValues = f… · symfony/symfony@0a7ec76 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0a7ec76

Browse files
[DI] Add ContainerBuilder::resolveValue($value, $resolveEnvValues = false)
1 parent 9361c5e commit 0a7ec76

File tree

2 files changed

+82
-13
lines changed

2 files changed

+82
-13
lines changed

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 68 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -862,17 +862,15 @@ private function createService(Definition $definition, $id, $tryProxy = true)
862862
return $proxy;
863863
}
864864

865-
$parameterBag = $this->getParameterBag();
866-
867865
if (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

873871
if (null !== $factory = $definition->getFactory()) {
874872
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]);
876874
} elseif (!is_string($factory)) {
877875
throw 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

@@ -905,14 +903,14 @@ private function 10000 createService(Definition $definition, $id, $tryProxy = true)
905903
$this->callMethod($service, $call);
906904
}
907905

908-
$properties = $this->resolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($definition->getProperties())));
906+
$properties = $this->resolveServices($this->resolveValue($definition->getProperties()));
909907
foreach ($properties as $name => $value) {
910908
$service->$name = $value;
911909
}
912910

913911
if ($callable = $definition->getConfigurator()) {
914912
if (is_array($callable)) {
915-
$callable[0] = $parameterBag->resolveValue($callable[0]);
913+
$callable[0] = $this->resolveValue($callable[0]);
916914

917915
if ($callable[0] instanceof Reference) {
918916
$callable[0] = $this->get((string) $callable[0], $callable[0]->getInvalidBehavior());
@@ -1028,11 +1026,13 @@ public function getExpressionLanguageProviders()
10281026
/**
10291027
* Resolves env parameter placeholders in a string.
10301028
*
1031-
* @param string $string The string 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 string $string The string 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
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
*/
10371037
public function resolveEnvPlaceholders($string, $format = null, array &$usedEnvs = null)
10381038
{
@@ -1042,11 +1042,22 @@ public function resolveEnvPlaceholders($string, $format = null, array &$usedEnvs
10421042
if (null === $format) {
10431043
$format = '%%env(%s)%%';
10441044
}
1045+
if (is_string($format)) {
1046+
$format = function ($env) use ($format) {
1047+
return sprintf($format, $env);
1048+
};
1049+
} elseif (!is_callable($format)) {
1050+
throw new InvalidArgumentException('$format must string, callable or null.');
1051+
}
1052+
$resolved = array();
10451053

10461054
foreach ($envPlaceholders as $env => $placeholders) {
10471055
foreach ($placeholders as $placeholder) {
10481056
if (false !== stripos($string, $placeholder)) {
1049-
$string = str_ireplace($placeholder, sprintf($format, $env), $string);
1057+
if (!isset($resolved[$env])) {
1058+
$resolved[$env] = array((string) call_user_func($format, $env));
1059+
}
1060+
$string = str_ireplace($placeholder, $resolved[$env][0], $string);
10501061
$usedEnvs[$env] = $env;
10511062
$this->envCounters[$env] = isset($this->envCounters[$env]) ? 1 + $this->envCounters[$env] : 1;
10521063
}
@@ -1075,6 +1086,19 @@ public function getEnvCounters()
10751086
return $this->envCounters;
10761087
}
10771088

1089+
/**
1090+
* Replaces parameter placeholders (%name%) and unescapes percent signs.
1091+
*
1092+
* @param mixed $value A value
1093+
* @param bool $resolveEnvValues Whether %env(VAR)% parameters should be replaced by the value of the corresponding environment variable or not
1094+
*
1095+
* @return mixed The resolved value
1096+
*/
1097+
public function resolveValue($value, $resolveEnvValues = false)
1098+
{
1099+
return $this->doResolveValue($value, $resolveEnvValues, array());
1100+
}
1101+
10781102
/**
10791103
* Returns the Service Conditionals.
10801104
*
@@ -1121,7 +1145,7 @@ private function callMethod($service, $call)
11211145
}
11221146
}
11231147

1124-
call_user_func_array(array($service, $call[0]), $this->resolveServices($this->getParameterBag()->unescapeValue($this->getParameterBag()->resolveValue($call[1]))));
1148+
call_user_func_array(array($service, $call[0]), $this->resolveServices($this->resolveValue($call[1])));
11251149
}
11261150

11271151
/**
@@ -1149,4 +1173,35 @@ private function getExpressionLanguage()
11491173

11501174
return $this->expressionLanguage;
11511175
}
1176+
1177+
/**
1178+
* Replaces parameter placeholders (%name%) and unescapes percent signs.
1179+
*
1180+
* @param mixed $value A value
1181+
* @param bool $resolveEnvValues Whether %env(VAR)% parameters should be replaced by the value of the corresponding environment variable or not
1182+
* @param array $resolving An array of keys that are being resolved (used internally to detect circular references)
1183+
*
1184+
* @return mixed The resolved value
1185+
*/
1186+
private function doResolveValue($value, $resolveEnvValues, array $resolving)
1187+
{
1188+
if (is_array($value)) {
1189+
$args = array();
1190+
foreach ($value as $k => $v) {
1191+
$args[$this->doResolveValue($k, $resolveEnvValues, $resolving)] = $this->doResolveValue($v, $resolveEnvValues, $resolving);
1192+
}
1193+
1194+
return $args;
1195+
}
1196+
1197+
if (!is_string($value)) {
1198+
return $value;
1199+
}
1200+
1201+
$parameterBag = $this->getParameterBag();
1202+
$value = $parameterBag->resolveString($value, $resolving);
1203+
$value = $parameterBag->unescapeValue($value);
1204+
1205+
return $resolveEnvValues ? $this->resolveEnvPlaceholders($value, array($this, 'getEnv')) : $value;
1206+
}
11521207
}

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,20 @@ public function testMerge()
500500
$this->assertEquals(array('Foo' => 0, 'Bar' => 1), $container->getEnvCounters());
501501
}
502502

503+
public function testResolveValue()
504+
{
505+
$_ENV['DUMMY_ENV_VAR'] = 'du%%y';
506+
507+
$container = new ContainerBuilder();
508+
$container->setParameter('foo', array('F%% %bar%'));
509+
$container->setParameter('bar', '%env(DUMMY_ENV_VAR)%');
510+
511+
$this->assertStringStartsWith('F% env_DUMMY_ENV_VAR_', $container->resolveValue('%foo%')[0]);
512+
$this->assertSame('du%%y', $container->resolveValue('%bar%', true));
513+
514+
unset($_ENV['DUMMY_ENV_VAR']);
515+
}
516+
503517
/**
504518
* @expectedException \LogicException
505519
*/

0 commit comments

Comments
 (0)
0