Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e7e5cfd

Browse files
committed
Allow "json:" env var processor to parse null values
Amend EnvVarProcessorTest to include all possible json values
1 parent 5e52160 commit e7e5cfd

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
throw new RuntimeException(sprintf('Invalid JSON in env var "%s": '.json_last_error_msg(), $name));
130130
}
131131

132-
if (!is_array($env)) {
133-
throw new RuntimeException(sprintf('Invalid JSON env var "%s": array expected, %s given.', $name, gettype($env)));
132+
if (null !== $env && !is_array($env)) {
133+
throw new RuntimeException(sprintf('Invalid JSON env var "%s": array or null expected, %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+
public function testDumpedJsonEnvParameters()
436+
{
437+
$container = new ContainerBuilder();
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 = new PhpDumper($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+
require self::$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
public function testCustomEnvParameters()
436457
{
437458
$container = new ContainerBuilder();

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-
public function testGetEnvJson()
236+
/**
237+
* @dataProvider validJson
238+
*/
239+
public function testGetEnvJson($value, $processed)
237240
{
238241
$processor = new EnvVarProcessor(new Container());
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-
return json_encode(array(1));
246+
return $value;
244247
});
245248

246-
$this->assertSame(array(1), $result);
249+
$this->assertSame($processed, $result);
250+
}
251+
252+
public function validJson()
253+
{
254+
return array(
255+
array('[1]', [1]),
256+
array('{"key": "value"}', ['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+
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
4+
use Symfony\Component\DependencyInjection\ContainerInterface;
5+
use Symfony\Component\DependencyInjection\Container;
6+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
7+
use Symfony\Component\DependencyInjection\Exception\LogicException;
8+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
9+
use Symfony\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_JsonParameters extends 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+
public function __construct()
28+
{
29+
$this->parameters = $this->getDefaultParameters();
30+
31+
$this->services = $this->privates = array();
32+
33+
$this->aliases = array();
34+
}
35+
36+
public function reset()
37+
{
38+
$this->privates = array();
39+
parent::reset();
40+
}
41+
42+
public function compile()
43+
{
44+
throw new LogicException('You cannot compile a dumped container that was already compiled.');
45+
}
46+
47+
public function isCompiled()
48+
{
49+
return true;
50+
}
51+
52+
public function getRemovedIds()
53+
{
54+
return array(
55+
'Psr\\Container\\ContainerInterface' => true,
56+
'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true,
57+
);
58+
}
59+
60+
public function getParameter($name)
61+
{
62+
$name = (string) $name;
63+
64+
if (!(isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || array_key_exists($name, $this->parameters))) {
65+
throw new InvalidArgumentException(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+
public function hasParameter($name)
75+
{
76+
$name = (string) $name;
77+
78+
return isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || array_key_exists($name, $this->parameters);
79+
}
80+
81+
public function setParameter($name, $value)
82+
{
83+
throw new LogicException('Impossible to call set() on a frozen ParameterBag.');
84+
}
85+
86+
public function getParameterBag()
87+
{
88+
if (null === $this->parameterBag) {
89+
$parameters = $this->parameters;
90+
foreach ($this->loadedDynamicParameters as $name => $loaded) {
91+
$parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
92+
}
93+
$this->parameterBag = new FrozenParameterBag($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+
private function getDynamicParameter($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: throw new InvalidArgumentException(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+
protected function getDefaultParameters()
132+
{
133+
return array(
134+
'env(foo)' => '["foo","bar"]',
135+
'env(bar)' => 'null',
136+
);
137+
}
138+
}

0 commit comments

Comments
 (0)