Skip to content

Commit d730209

Browse files
dunglasfabpot
authored andcommitted
[DI] Add a simple CSV env var processor
1 parent 1df45e4 commit d730209

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed

src/Symfony/Component/DependencyInjection/EnvVarProcessor.php

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public static function getProvidedTypes()
3636
'base64' => 'string',
3737
'bool' => 'bool',
3838
'const' => 'bool|int|float|string|array',
39+
'csv' => 'array',
3940
'file' => 'string',
4041
'float' => 'float',
4142
'int' => 'int',
@@ -149,6 +150,10 @@ public function getEnv($prefix, $name, \Closure $getEnv)
149150
}, $env);
150151
}
151152

153+
if ('csv' === $prefix) {
154+
return str_getcsv($env);
155+
}
156+
152157
throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix));
153158
}
154159

src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterEnvVarProcessorsPassTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function testSimpleProcessor()
3333
'base64' => array('string'),
3434
'bool' => array('bool'),
3535
'const' => array('bool', 'int', 'float', 'string', 'array'),
36+
'csv' => array('array'),
3637
'file' => array('string'),
3738
'float' => array('float'),
3839
'int' => array('int'),

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

+17
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,23 @@ public function testDumpedBase64EnvParameters()
371371
$this->assertSame('world', $container->getParameter('hello'));
372372
}
373373

374+
public function testDumpedCsvEnvParameters()
375+
{
376+
$container = new ContainerBuilder();
377+
$container->setParameter('env(foo)', 'foo,bar');
378+
$container->setParameter('hello', '%env(csv:foo)%');
379+
$container->compile();
380+
381+
$dumper = new PhpDumper($container);
382+
$dumper->dump();
383+
384+
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_csv_env.php', $dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_CsvParameters')));
385+
386+
require self::$fixturesPath.'/php/services_csv_env.php';
387+
$container = new \Symfony_DI_PhpDumper_Test_CsvParameters();
388+
$this->assertSame(array('foo', 'bar'), $container->getParameter('hello'));
389+
}
390+
374391
public function testCustomEnvParameters()
375392
{
376393
$container = new ContainerBuilder();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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_CsvParameters 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+
);
102+
private $dynamicParameters = array();
103+
104+
/**
105+
* Computes a dynamic parameter.
106+
*
107+
* @param string The name of the dynamic parameter to load
108+
*
109+
* @return mixed The value of the dynamic parameter
110+
*
111+
* @throws InvalidArgumentException When the dynamic parameter does not exist
112+
*/
113+
private function getDynamicParameter($name)
114+
{
115+
switch ($name) {
116+
case 'hello': $value = $this->getEnv('csv:foo'); break;
117+
default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name));
118+
}
119+
$this->loadedDynamicParameters[$name] = true;
120+
121+
return $this->dynamicParameters[$name] = $value;
122+
}
123+
124+
/**
125+
* Gets the default parameters.
126+
*
127+
* @return array An array of the default parameters
128+
*/
129+
protected function getDefaultParameters()
130+
{
131+
return array(
132+
'env(foo)' => 'foo,bar',
133+
);
134+
}
135+
}

0 commit comments

Comments
 (0)