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

Skip to content

Commit e643fa8

Browse files
committed
Add a fallback EnvProcessor
1 parent 8c24c35 commit e643fa8

File tree

5 files changed

+165
-0
lines changed

5 files changed

+165
-0
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CHANGELOG
1010
* added `ServiceLocatorArgument` and `!service_locator` config tag for creating optimized service-locators
1111
* added support for autoconfiguring bindings
1212
* added `%env(key:...)%` processor to fetch a specific key from an array
13+
* added `%env(fallback:...)%` processor to fallback to a default value
1314
* deprecated `ServiceSubscriberInterface`, use the same interface from the `Symfony\Contracts\Service` namespace instead
1415
* deprecated `ResettableContainerInterface`, use `Symfony\Contracts\Service\ResetInterface` instead
1516

src/Symfony/Component/DependencyInjection/EnvVarProcessor.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public static function getProvidedTypes()
4444
'json' => 'array',
4545
'key' => 'bool|int|float|string|array',
4646
'resolve' => 'string',
47+
'fallback' => 'string',
4748
'string' => 'string',
4849
);
4950
}
@@ -74,6 +75,23 @@ public function getEnv($prefix, $name, \Closure $getEnv)
7475
return $array[$key];
7576
}
7677

78+
if ('fallback' === $prefix) {
79+
if (false === $i) {
80+
throw new RuntimeException(sprintf('Invalid configuration: env var "fallback:%s" does not contain a default value.', $name));
81+
}
82+
83+
$next = substr($name, $i + 1);
84+
$default = substr($name, 0, $i);
85+
86+
try {
87+
return $getEnv($next);
88+
} catch (RuntimeException $e) {
89+
return $default;
90+
} catch (EnvNotFoundException $e) {
91+
return $default;
92+
}
93+
}
94+
7795
if ('file' === $prefix) {
7896
if (!is_scalar($file = $getEnv($name))) {
7997
throw new RuntimeException(sprintf('Invalid file name: env var "%s" is non-scalar.', $name));

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function testSimpleProcessor()
4040
'json' => array('array'),
4141
'key' => array('bool', 'int', 'float', 'string', 'array'),
4242
'resolve' => array('string'),
43+
'fallback' => array('string'),
4344
'string' => array('string'),
4445
);
4546

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,25 @@ public function testDumpedCsvEnvParameters()
439439
$this->assertSame(array('foo', 'bar'), $container->getParameter('hello'));
440440
}
441441

442+
public function testDumpedFallbackEnvParameters()
443+
{
444+
$container = new ContainerBuilder();
445+
$container->setParameter('env(foo)', '{"foo": "bar"}');
446+
$container->setParameter('hello', '%env(fallback:baz:bar)%');
447+
$container->setParameter('hello-bar', '%env(fallback:baz:key:baz:json:foo)%');
448+
$container->compile();
449+
450+
$dumper = new PhpDumper($container);
451+
$dumper->dump();
452+
453+
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_fallback_env.php', $dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_FallbackParameters')));
454+
455+
require self::$fixturesPath.'/php/services_fallback_env.php';
456+
$container = new \Symfony_DI_PhpDumper_Test_FallbackParameters();
457+
$this->assertSame('baz', $container->getParameter('hello'));
458+
$this->assertSame('baz', $container->getParameter('hello-bar'));
459+
}
460+
442461
public function testDumpedJsonEnvParameters()
443462
{
444463
$container = new ContainerBuilder();
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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_FallbackParameters extends Container
18+
{
19+
private $parameters;
20+
private $targetDirs = array();
21+
22+
public function __construct()
23+
{
24+
$this->parameters = $this->getDefaultParameters();
25+
26+
$this->services = $this->privates = array();
27+
28+
$this->aliases = array();
29+
}
30+
31+
public function compile()
32+
{
33+
throw new LogicException('You cannot compile a dumped container that was already compiled.');
34+
}
35+
36+
public function isCompiled()
37+
{
38+
return true;
39+
}
40+
41+
public function getRemovedIds()
42+
{
43+
return array(
44+
'Psr\\Container\\ContainerInterface' => true,
45+
'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true,
46+
);
47+
}
48+
49+
public function getParameter($name)
50+
{
51+
$name = (string) $name;
52+
53+
if (!(isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || array_key_exists($name, $this->parameters))) {
54+
throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
55+
}
56+
if (isset($this->loadedDynamicParameters[$name])) {
57+
return $this->loadedDynamicParameters[$name] ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
58+
}
59+
60+
return $this->parameters[$name];
61+
}
62+
63+
public function hasParameter($name)
64+
{
65+
$name = (string) $name;
66+
67+
return isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || array_key_exists($name, $this->parameters);
68+
}
69+
70+
public function setParameter($name, $value)
71+
{
72+
throw new LogicException('Impossible to call set() on a frozen ParameterBag.');
73+
}
74+
75+
public function getParameterBag()
76+
{
77+
if (null === $this->parameterBag) {
78+
$parameters = $this->parameters;
79+
foreach ($this->loadedDynamicParameters as $name => $loaded) {
80+
$parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
81+
}
82+
$this->parameterBag = new FrozenParameterBag($parameters);
83+
}
84+
85+
return $this->parameterBag;
86+
}
87+
88+
private $loadedDynamicParameters = array(
89+
'hello' => false,
90+
'hello-bar' => false,
91+
);
92+
private $dynamicParameters = array();
93+
94+
/**
95+
* Computes a dynamic parameter.
96+
*
97+
* @param string The name of the dynamic parameter to load
98+
*
99+
* @return mixed The value of the dynamic parameter
100+
*
101+
* @throws InvalidArgumentException When the dynamic parameter does not exist
102+
*/
103+
private function getDynamicParameter($name)
104+
{
105+
switch ($name) {
106+
case 'hello': $value = $this->getEnv('fallback:baz:bar'); break;
107+
case 'hello-bar': $value = $this->getEnv('fallback:baz:key:baz:json:foo'); break;
108+
default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name));
109+
}
110+
$this->loadedDynamicParameters[$name] = true;
111+
112+
return $this->dynamicParameters[$name] = $value;
113+
}
114+
115+
/**
116+
* Gets the default parameters.
117+
*
118+
* @return array An array of the default parameters
119+
*/
120+
protected function getDefaultParameters()
121+
{
122+
return array(
123+
'env(foo)' => '{"foo": "bar"}',
124+
);
125+
}
126+
}

0 commit comments

Comments
 (0)