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

Commitabc7480

Browse files
committed
Allow "json:" env var processor to parse null values
Amend EnvVarProcessorTest to include all possible json values
1 parent5e52160 commitabc7480

File tree

4 files changed

+178
-6
lines changed

4 files changed

+178
-6
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ public function getEnv($prefix, $name, \Closure $getEnv)
129129
thrownewRuntimeException(sprintf('Invalid JSON in env var "%s":'.json_last_error_msg(),$name));
130130
}
131131

132-
if (!is_array($env)) {
133-
thrownewRuntimeException(sprintf('Invalid JSON env var "%s": array expected, %s given.',$name,gettype($env)));
132+
if (null !==$env &&!is_array($env)) {
133+
thrownewRuntimeException(sprintf('Invalid JSON env var "%s": arrayor nullexpected, %s given.',$name,gettype($env)));
134134
}
135135

136136
return$env;

‎src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,27 @@ public function testDumpedCsvEnvParameters()
432432
$this->assertSame(array('foo','bar'),$container->getParameter('hello'));
433433
}
434434

435+
publicfunctiontestDumpedJsonEnvParameters()
436+
{
437+
$container =newContainerBuilder();
438+
$container->setParameter('env(foo)','["foo","bar"]');
439+
$container->setParameter('env(bar)','null');
440+
$container->setParameter('hello','%env(json:foo)%');
441+
$container->setParameter('hello-bar','%env(json:bar)%');
442+
$container->compile();
443+
444+
$dumper =newPhpDumper($container);
445+
$dumper->dump();
446+
447+
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_json_env.php',$dumper->dump(array('class' =>'Symfony_DI_PhpDumper_Test_JsonParameters')));
448+
449+
putenv('foobar="hello"');
450+
requireself::$fixturesPath.'/php/services_json_env.php';
451+
$container =new \Symfony_DI_PhpDumper_Test_JsonParameters();
452+
$this->assertSame(array('foo','bar'),$container->getParameter('hello'));
453+
$this->assertNull($container->getParameter('hello-bar'));
454+
}
455+
435456
publicfunctiontestCustomEnvParameters()
436457
{
437458
$container =newContainerBuilder();

‎src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,17 +233,29 @@ public function testGetEnvBase64()
233233
$this->assertSame('hello',$result);
234234
}
235235

236-
publicfunctiontestGetEnvJson()
236+
/**
237+
* @dataProvider validJson
238+
*/
239+
publicfunctiontestGetEnvJson($value,$processed)
237240
{
238241
$processor =newEnvVarProcessor(newContainer());
239242

240-
$result =$processor->getEnv('json','foo',function ($name) {
243+
$result =$processor->getEnv('json','foo',function ($name)use ($value){
241244
$this->assertSame('foo',$name);
242245

243-
returnjson_encode(array(1));
246+
return$value;
244247
});
245248

246-
$this->assertSame(array(1),$result);
249+
$this->assertSame($processed,$result);
250+
}
251+
252+
publicfunctionvalidJson()
253+
{
254+
returnarray(
255+
array('[1]',array(1)),
256+
array('{"key": "value"}',array('key' =>'value')),
257+
array(null,null),
258+
);
247259
}
248260

249261
/**
@@ -284,6 +296,7 @@ public function otherJsonValues()
284296
array(1.1),
285297
array(true),
286298
array(false),
299+
array('foo'),
287300
);
288301
}
289302

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
useSymfony\Component\DependencyInjection\Argument\RewindableGenerator;
4+
useSymfony\Component\DependencyInjection\ContainerInterface;
5+
useSymfony\Component\DependencyInjection\Container;
6+
useSymfony\Component\DependencyInjection\Exception\InvalidArgumentException;
7+
useSymfony\Component\DependencyInjection\Exception\LogicException;
8+
useSymfony\Component\DependencyInjection\Exception\RuntimeException;
9+
useSymfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
10+
11+
/**
12+
* This class has been auto-generated
13+
* by the Symfony Dependency Injection Component.
14+
*
15+
* @final since Symfony 3.3
16+
*/
17+
class Symfony_DI_PhpDumper_Test_JsonParametersextends Container
18+
{
19+
private$parameters;
20+
private$targetDirs =array();
21+
22+
/**
23+
* @internal but protected for BC on cache:clear
24+
*/
25+
protected$privates =array();
26+
27+
publicfunction__construct()
28+
{
29+
$this->parameters =$this->getDefaultParameters();
30+
31+
$this->services =$this->privates =array();
32+
33+
$this->aliases =array();
34+
}
35+
36+
publicfunctionreset()
37+
{
38+
$this->privates =array();
39+
parent::reset();
40+
}
41+
42+
publicfunctioncompile()
43+
{
44+
thrownewLogicException('You cannot compile a dumped container that was already compiled.');
45+
}
46+
47+
publicfunctionisCompiled()
48+
{
49+
returntrue;
50+
}
51+
52+
publicfunctiongetRemovedIds()
53+
{
54+
returnarray(
55+
'Psr\\Container\\ContainerInterface' =>true,
56+
'Symfony\\Component\\DependencyInjection\\ContainerInterface' =>true,
57+
);
58+
}
59+
60+
publicfunctiongetParameter($name)
61+
{
62+
$name = (string)$name;
63+
64+
if (!(isset($this->parameters[$name]) ||isset($this->loadedDynamicParameters[$name]) ||array_key_exists($name,$this->parameters))) {
65+
thrownewInvalidArgumentException(sprintf('The parameter "%s" must be defined.',$name));
66+
}
67+
if (isset($this->loadedDynamicParameters[$name])) {
68+
return$this->loadedDynamicParameters[$name] ?$this->dynamicParameters[$name] :$this->getDynamicParameter($name);
69+
}
70+
71+
return$this->parameters[$name];
72+
}
73+
74+
publicfunctionhasParameter($name)
75+
{
76+
$name = (string)$name;
77+
78+
returnisset($this->parameters[$name]) ||isset($this->loadedDynamicParameters[$name]) ||array_key_exists($name,$this->parameters);
79+
}
80+
81+
publicfunctionsetParameter($name,$value)
82+
{
83+
thrownewLogicException('Impossible to call set() on a frozen ParameterBag.');
84+
}
85+
86+
publicfunctiongetParameterBag()
87+
{
88+
if (null ===$this->parameterBag) {
89+
$parameters =$this->parameters;
90+
foreach ($this->loadedDynamicParametersas$name =>$loaded) {
91+
$parameters[$name] =$loaded ?$this->dynamicParameters[$name] :$this->getDynamicParameter($name);
92+
}
93+
$this->parameterBag =newFrozenParameterBag($parameters);
94+
}
95+
96+
return$this->parameterBag;
97+
}
98+
99+
private$loadedDynamicParameters =array(
100+
'hello' =>false,
101+
'hello-bar' =>false,
102+
);
103+
private$dynamicParameters =array();
104+
105+
/**
106+
* Computes a dynamic parameter.
107+
*
108+
* @param string The name of the dynamic parameter to load
109+
*
110+
* @return mixed The value of the dynamic parameter
111+
*
112+
* @throws InvalidArgumentException When the dynamic parameter does not exist
113+
*/
114+
privatefunctiongetDynamicParameter($name)
115+
{
116+
switch ($name) {
117+
case'hello':$value =$this->getEnv('json:foo');break;
118+
case'hello-bar':$value =$this->getEnv('json:bar');break;
119+
default:thrownewInvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.',$name));
120+
}
121+
$this->loadedDynamicParameters[$name] =true;
122+
123+
return$this->dynamicParameters[$name] =$value;
124+
}
125+
126+
/**
127+
* Gets the default parameters.
128+
*
129+
* @return array An array of the default parameters
130+
*/
131+
protectedfunctiongetDefaultParameters()
132+
{
133+
returnarray(
134+
'env(foo)' =>'["foo","bar"]',
135+
'env(bar)' =>'null',
136+
);
137+
}
138+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp