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

Skip to content

Commit 56f0257

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

File tree

13 files changed

+99
-45
lines changed

13 files changed

+99
-45
lines changed
src/Symfony/Bridge/Doctrine/DependencyInjection/AbstractDoctrineExtension.php
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,18 @@ protected function loadCacheDriver($cacheName, $objectManagerName, array $cacheD
384384

385385
if (!isset($cacheDriver['namespace'])) {
386386
// generate a unique namespace for the given application
387-
$env = $container->getParameter('kernel.root_dir').$container->getParameter('kernel.environment');
387+
$env = '';
388+
foreach (array('kernel.name', 'kernel.environment', 'kernel.debug', 'cache.prefix.seed') as $key) {
389+
if ($container->hasParameter($key)) {
390+
if (method_exists($container, 'resolveValue')) {
391+
$env .= '.'.$container->resolveValue($container->getParameter($key), true);
392+
} else {
393+
$env .= '.'.$container->getParameterBag()->resolveValue($container->getParameter($key));
394+
}
395+
}
396+
}
388397
$hash = hash('sha256', $env);
389-
$namespace = 'sf2'.$this->getMappingResourceExtension().'_'.$objectManagerName.'_'.$hash;
398+
$namespace = 'sf_'.$this->getMappingResourceExtension().'_'.$objectManagerName.'_'.$hash;
390399

391400
$cacheDriver['namespace'] = $namespace;
392401
}

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolClearerPass.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ final class CachePoolClearerPass implements CompilerPassInterface
2525
*/
2626
public function process(ContainerBuilder $container)
2727
{
28+
$container->getParameterBag()->remove('cache.prefix.seed');
29+
2830
foreach ($container->findTaggedServiceIds('cache.pool') as $id => $attributes) {
2931
foreach (array_reverse($attributes) as $attr) {
3032
if (isset($attr['clearer'])) {

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPass.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ public function process(ContainerBuilder $container)
3333

3434
foreach (array('kernel.name', 'kernel.environment', 'kernel.debug', 'cache.prefix.seed') as $key) {
3535
if ($container->hasParameter($key)) {
36-
$namespaceSuffix .= '.'.$container->getParameter($key);
36+
$namespaceSuffix .= '.'.$container->resolveValue($container->getParameter($key), true);
3737
}
3838
}
39-
$container->getParameterBag()->remove('cache.prefix.seed');
4039

4140
$aliases = $container->getAliases();
4241
$attributes = array(

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"php": ">=5.5.9",
2020
"symfony/cache": "~3.2",
2121
"symfony/class-loader": "~3.2",
22-
"symfony/dependency-injection": "~3.2",
22+
"symfony/dependency-injection": "~3.3",
2323
"symfony/config": "~2.8|~3.0",
2424
"symfony/event-dispatcher": "~2.8|~3.0",
2525
"symfony/http-foundation": "~3.1",

src/Symfony/Component/DependencyInjection/Compiler/AutoAliasServicePass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function process(ContainerBuilder $container)
3131
throw new InvalidArgumentException(sprintf('Missing tag information "format" on auto_alias service "%s".', $serviceId));
3232
}
3333

34-
$aliasId = $container->getParameterBag()->resolveValue($tag['format']);
34+
$aliasId = $container->resolveValue($tag['format']);
3535
if ($container->hasDefinition($aliasId) || $container->hasAlias($aliasId)) {
3636
$container->setAlias($serviceId, new Alias($aliasId));
3737
}

src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ private function getReflectionClass($id, Definition $definition)
298298
return false;
299299
}
300300

301-
$class = $this->container->getParameterBag()->resolveValue($class);
301+
$class = $this->container->resolveValue($class);
302302

303303
try {
304304
$reflector = new \ReflectionClass($class);

src/Symfony/Component/DependencyInjection/Compiler/MergeExtensionConfigurationPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function process(ContainerBuilder $container)
4242
// this extension was not called
4343
continue;
4444
}
45-
$config = $container->getParameterBag()->resolveValue($config);
45+
$config = $container->resolveValue($config);
4646

4747
$tmpContainer = new ContainerBuilder($container->getParameterBag());
4848
$tmpContainer->setResourceTracking($container->isTrackingResources());

src/Symfony/Component/DependencyInjection/Compiler/ResolveParameterPlaceHoldersPass.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,26 @@ class ResolveParameterPlaceHoldersPass implements CompilerPassInterface
3030
*/
3131
public function process(ContainerBuilder $container)
3232
{
33-
$parameterBag = $container->getParameterBag();
34-
3533
foreach ($container->getDefinitions() as $id => $definition) {
3634
try {
37-
$definition->setClass($parameterBag->resolveValue($definition->getClass()));
38-
$definition->setFile($parameterBag->resolveValue($definition->getFile()));
39-
$definition->setArguments($parameterBag->resolveValue($definition->getArguments()));
35+
$definition->setClass($container->resolveValue($definition->getClass()));
36+
$definition->setFile($container->resolveValue($definition->getFile()));
37+
$definition->setArguments($container->resolveValue($definition->getArguments()));
4038

4139
$factory = $definition->getFactory();
4240

4341
if (is_array($factory) && isset($factory[0])) {
44-
$factory[0] = $parameterBag->resolveValue($factory[0]);
42+
$factory[0] = $container->resolveValue($factory[0]);
4543
$definition->setFactory($factory);
4644
}
4745

4846
$calls = array();
4947
foreach ($definition->getMethodCalls() as $name => $arguments) {
50-
$calls[$parameterBag->resolveValue($name)] = $parameterBag->resolveValue($arguments);
48+
$calls[$container->resolveValue($name)] = $container->resolveValue($arguments);
5149
}
5250
$definition->setMethodCalls($calls);
5351

54-
$definition->setProperties($parameterBag->resolveValue($definition->getProperties()));
52+
$definition->setProperties($container->resolveValue($definition->getProperties()));
5553
} catch (ParameterNotFoundException $e) {
5654
$e->setSourceId($id);
5755

@@ -61,10 +59,10 @@ public function process(ContainerBuilder $container)
6159

6260
$aliases = array();
6361
foreach ($container->getAliases() as $name => $target) {
64-
$aliases[$parameterBag->resolveValue($name)] = $parameterBag->resolveValue($target);
62+
$aliases[$container->resolveValue($name)] = $container->resolveValue($target);
6563
}
6664
$container->setAliases($aliases);
6765

68-
$parameterBag->resolve();
66+
$container->getParameterBag()->resolve();
6967
}
7068
}

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 55 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 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 Closure 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 wh 10000 ile 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] = (string) call_user_func($format, $env);
1059+
}
1060+
$string = str_ireplace($placeholder, $resolved[$env], $string);
10501061
$usedEnvs[$env] = $env;
10511062
$this->envCounters[$env] = isset($this->envCounters[$env]) ? 1 + $this->envCounters[$env] : 1;
10521063
}
@@ -1075,6 +1086,37 @@ 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 mixed $resolveEnvValues Whether %env(VAR)% parameters should be replaced by the value of the corresponding environment variable or not
1094+
* @param array $resolving An array of keys that are being resolved (used internally to detect circular references)
1095+
*
1096+
* @return mixed The resolved value
1097+
*/
1098+
public function resolveValue($value, $resolveEnvValues = false, array $resolving = array())
1099+
{
1100+
if (is_array($value)) {
1101+
$args = array();
1102+
foreach ($value as $k => $v) {
1103+
$args[$this->resolveValue($k, $resolveEnvValues, $resolving)] = $this->resolveValue($v, $resolveEnvValues, $resolving);
1104+
}
1105+
1106+
return $args;
1107+
}
1108+
1109+
if (!is_string($value)) {
1110+
return $value;
1111+
}
1112+
1113+
$parameterBag = $this->getParameterBag();
1114+
$value = $parameterBag->resolveString($value, $resolving);
1115+
$value = $parameterBag->unescapeValue($value);
1116+
1117+
return $resolveEnvValues ? $this->resolveEnvPlaceholders($value, array($this, 'getEnv')) : $value;
1118+
}
1119+
10781120
/**
10791121
* Returns the Service Conditionals.
10801122
*
@@ -1121,7 +1163,7 @@ private function callMethod($service, $call)
11211163
}
11221164
}
11231165

1124-
call_user_func_array(array($service, $call[0]), $this->resolveServices($this->getParameterBag()->unescapeValue($this->getParameterBag()->resolveValue($call[1]))));
1166+
call_user_func_array(array($service, $call[0]), $this->resolveServices($this->resolveValue($call[1])));
11251167
}
11261168

11271169
/**

src/Symfony/Component/DependencyInjection/Dumper/GraphvizDumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ private function findNodes()
171171
}
172172

173173
try {
174-
$class = $this->container->getParameterBag()->resolveValue($class);
174+
$class = $this->container->resolveValue($class);
175175
} catch (ParameterNotFoundException $e) {
176176
}
177177

src/Symfony/Component/DependencyInjection/Extension/Extension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,6 @@ protected function isConfigEnabled(ContainerBuilder $container, array $config)
113113
throw new InvalidArgumentException("The config array has no 'enabled' key.");
114114
}
115115

116-
return (bool) $container->getParameterBag()->resolveValue($config['enabled']);
116+
return (bool) $container->resolveValue($config['enabled']);
117117
}
118118
}

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', 'F%%');
509+
$container->setParameter('bar', '%env(DUMMY_ENV_VAR)%');
510+
511+
$this->assertStringStartsWith('abc F% def env_DUMMY_ENV_VAR_', $container->resolveValue('abc %foo% def %bar%'));
512+
$this->assertSame('abc F% def du%%y', $container->resolveValue('abc %foo% def %bar%', true));
513+
514+
unset($_ENV['DUMMY_ENV_VAR']);
515+
}
516+
503517
/**
504518
* @expectedException \LogicException
505519
*/

src/Symfony/Component/DependencyInjection/Tests/Extension/ExtensionTest.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,17 @@ class ExtensionTest extends \PHPUnit_Framework_TestCase
1818
*/
1919
public function testIsConfigEnabledReturnsTheResolvedValue($enabled)
2020
{
21-
$pb = $this->getMockBuilder('Symfony\Component\DependencyInjection\ParameterBag\ParameterBag')
22-
->setMethods(array('resolveValue'))
23-
->getMock()
24-
;
25-
2621
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')
27-
->setMethods(array('getParameterBag'))
22+
->setMethods(array('resolveValue'))
2823
->getMock()
2924
;
3025

31-
$pb->expects($this->once())
26+
$container->expects($this->once())
3227
->method('resolveValue')
3328
->with($this->equalTo($enabled))
3429
->will($this->returnValue($enabled))
3530
;
3631

37-
$container->expects($this->once())
38-
->method('getParameterBag')
39-
->will($this->returnValue($pb))
40-
;
41-
4232
$extension = $this->getMockBuilder('Symfony\Component\DependencyInjection\Extension\Extension')
4333
->setMethods(array())
4434
->getMockForAbstractClass()

0 commit comments

Comments
 (0)
0