Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit58d827e

Browse files
[DI] Add ContainerBuilder::resolveValue($value, $resolveEnvValues = false)
1 parent98dcd51 commit58d827e

File tree

3 files changed

+61
-13
lines changed

3 files changed

+61
-13
lines changed

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,12 @@ private function registerCacheConfiguration(array $config, ContainerBuilder $con
12471247
if (isset($config['prefix_seed'])) {
12481248
$container->setParameter('cache.prefix.seed',$config['prefix_seed']);
12491249
}
1250+
if ($container->hasParameter('cache.prefix.seed')) {
1251+
$seed =$container->getParameter('cache.prefix.seed');
1252+
// Inline any env vars referenced in the parameter
1253+
$seed =$container->resolveValue($seed,true);
1254+
$container->setParameter('cache.prefix.seed',$container->getParameterBag()->escapeValue($seed));
1255+
}
12501256
foreach (array('doctrine','psr6','redis')as$name) {
12511257
if (isset($config[$name ='default_'.$name.'_provider'])) {
12521258
$container->setAlias('cache.'.$name,newAlias(Compiler\CachePoolPass::getServiceProvider($container,$config[$name]),false));

‎src/Symfony/Component/DependencyInjection/ContainerBuilder.php‎

Lines changed: 41 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
thrownewRuntimeException(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

@@ -901,7 +899,7 @@ private function createService(Definition $definition, $id, $tryProxy = true)
901899
$this->shareService($definition,$service,$id);
902900
}
903901

904-
$properties =$this->resolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($definition->getProperties())));
902+
$properties =$this->resolveServices($this->resolveValue($definition->getProperties()));
905903
foreach ($propertiesas$name =>$value) {
906904
$service->$name =$value;
907905
}
@@ -912,7 +910,7 @@ private function createService(Definition $definition, $id, $tryProxy = true)
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,17 +1026,26 @@ public function getExpressionLanguageProviders()
10281026
/**
10291027
* Resolves env parameter placeholders in a string.
10301028
*
1031-
* @param mixed $value The value to resolve
1032-
* @param string|null $format A sprintf() formatto use asreplacement for envplaceholdersor null tousethedefault parameter format
1033-
* @param array &$usedEnvs Env vars found while resolving are added to this array
1029+
* @param mixed$value The value to resolve
1030+
* @param string|callable|null $format Acallable orsprintf() formatreturning thereplacement foreachenvvar nameor null toresolve back totheoriginal "%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
publicfunctionresolveEnvPlaceholders($value,$format =null,array &$usedEnvs =null)
10381038
{
10391039
if (null ===$format) {
10401040
$format ='%%env(%s)%%';
10411041
}
1042+
if (is_string($format)) {
1043+
$format =function ($env)use ($format) {
1044+
returnsprintf($format,$env);
1045+
};
1046+
}elseif (!is_callable($format)) {
1047+
thrownewInvalidArgumentException('$format must string, callable or null.');
1048+
}
10421049

10431050
if (is_array($value)) {
10441051
$result =array();
@@ -1055,11 +1062,15 @@ public function resolveEnvPlaceholders($value, $format = null, array &$usedEnvs
10551062

10561063
$bag =$this->getParameterBag();
10571064
$envPlaceholders =$baginstanceof EnvPlaceholderParameterBag ?$bag->getEnvPlaceholders() :$this->envPlaceholders;
1065+
$resolved =array();
10581066

10591067
foreach ($envPlaceholdersas$env =>$placeholders) {
10601068
foreach ($placeholdersas$placeholder) {
10611069
if (false !==stripos($value,$placeholder)) {
1062-
$value =str_ireplace($placeholder,sprintf($format,$env),$value);
1070+
if (!isset($resolved[$env])) {
1071+
$resolved[$env] =array((string)call_user_func($format,$env));
1072+
}
1073+
$value =str_ireplace($placeholder,$resolved[$env][0],$value);
10631074
$usedEnvs[$env] =$env;
10641075
$this->envCounters[$env] =isset($this->envCounters[$env]) ?1 +$this->envCounters[$env] :1;
10651076
}
@@ -1088,6 +1099,23 @@ public function getEnvCounters()
10881099
return$this->envCounters;
10891100
}
10901101

1102+
/**
1103+
* Replaces parameter placeholders (%name%) and unescapes percent signs.
1104+
*
1105+
* @param mixed $value A value
1106+
* @param bool $resolveEnvValues Whether %env(VAR)% parameters should be replaced by the value of the corresponding environment variable or not
1107+
*
1108+
* @return mixed The resolved value
1109+
*/
1110+
publicfunctionresolveValue($value,$resolveEnvValues =false)
1111+
{
1112+
$parameterBag =$this->getParameterBag();
1113+
$value =$parameterBag->resolveValue($value);
1114+
$value =$parameterBag->unescapeValue($value);
1115+
1116+
return$resolveEnvValues ?$this->resolveEnvPlaceholders($value,array($this,'getEnv')) :$value;
1117+
}
1118+
10911119
/**
10921120
* Returns the Service Conditionals.
10931121
*
@@ -1134,7 +1162,7 @@ private function callMethod($service, $call)
11341162
}
11351163
}
11361164

1137-
call_user_func_array(array($service,$call[0]),$this->resolveServices($this->getParameterBag()->unescapeValue($this->getParameterBag()->resolveValue($call[1]))));
1165+
call_user_func_array(array($service,$call[0]),$this->resolveServices($this->resolveValue($call[1])));
11381166
}
11391167

11401168
/**

‎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+
publicfunctiontestResolveValue()
504+
{
505+
$_ENV['DUMMY_ENV_VAR'] ='du%%y';
506+
507+
$container =newContainerBuilder();
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)

[8]ページ先頭

©2009-2025 Movatter.jp