8000 Rename to catch, and use parameters · symfony/symfony@16461d6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 16461d6

Browse files
committed
Rename to catch, and use parameters
1 parent e643fa8 commit 16461d6

File tree

6 files changed

+37
-27
lines changed

6 files changed

+37
-27
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.3.0
5+
-----
6+
7+
* added `%env(default:...)%` processor to fallback to a default value
8+
49
4.2.0
510
-----
611

@@ -10,7 +15,6 @@ CHANGELOG
1015
* added `ServiceLocatorArgument` and `!service_locator` config tag for creating optimized service-locators
1116
* added support for autoconfiguring bindings
1217
* added `%env(key:...)%` processor to fetch a specific key from an array
13-
* added `%env(fallback:...)%` processor to fallback to a default value
1418
* deprecated `ServiceSubscriberInterface`, use the same interface from the `Symfony\Contracts\Service` namespace instead
1519
* deprecated `ResettableContainerInterface`, use `Symfony\Contracts\Service\ResetInterface` instead
1620

src/Symfony/Component/DependencyInjection/EnvVarProcessor.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static function getProvidedTypes()
4444
'json' => 'array',
4545
'key' => 'bool|int|float|string|array',
4646
'resolve' => 'string',
47-
'fallback' => 'string',
47+
'default' => 'bool|int|float|string|array',
4848
'string' => 'string',
4949
);
5050
}
@@ -58,7 +58,7 @@ public function getEnv($prefix, $name, \Closure $getEnv)
5858

5959
if ('key' === $prefix) {
6060
if (false === $i) {
61-
throw new RuntimeException(sprintf('Invalid configuration: env var "key:%s" does not contain a key specifier.', $name));
61+
throw new RuntimeException(sprintf('Invalid env "key:%s": a key specifier should be provided.', $name));
6262
}
6363

6464
$next = substr($name, $i + 1);
@@ -68,27 +68,30 @@ public function getEnv($prefix, $name, \Closure $getEnv)
6868
if (!\is_array($array)) {
6969
throw new RuntimeException(sprintf('Resolved value of "%s" did not result in an array value.', $next));
7070
}
71-
if (!array_key_exists($key, $array)) {
72-
throw new RuntimeException(sprintf('Key "%s" not found in "%s" (resolved from "%s")', $key, json_encode($array), $next));
71+
72+
if (!isset($array[$key]) && !array_key_exists($key, $array)) {
73+
throw new EnvNotFoundException(sprintf('Key "%s" not found in "%s" (resolved from "%s").', $key, json_encode($array), $next));
7374
}
7475

7576
return $array[$key];
7677
}
7778

78-
if ('fallback' === $prefix) {
79+
if ('default' === $prefix) {
7980
if (false === $i) {
80-
throw new RuntimeException(sprintf('Invalid configuration: env var "fallback:%s" does not contain a default value.', $name));
81+
throw new RuntimeException(sprintf('Invalid env "default:%s": a fallback parameter should be provided.', $name));
8182
}
8283

8384
$next = substr($name, $i + 1);
8485
$default = substr($name, 0, $i);
8586

87+
if (!$this->container->hasParameter($default)) {
88+
throw new RuntimeException(sprintf('Invalid env fallback in "default:%s": parameter "%s" not found.', $name, $default));
89+
}
90+
8691
try {
8792
return $getEnv($next);
88-
} catch (RuntimeException $e) {
89-
return $default;
9093
} catch (EnvNotFoundException $e) {
91-
return $default;
94+
return $this->container->getParameter($default);
9295
}
9396
}
9497

@@ -97,7 +100,7 @@ public function getEnv($prefix, $name, \Closure $getEnv)
97100
throw new RuntimeException(sprintf('Invalid file name: env var "%s" is non-scalar.', $name));
98101
}
99102
if (!file_exists($file)) {
100-
throw new RuntimeException(sprintf('Env "file:%s" not found: %s does not exist.', $name, $file));
103+
throw new EnvNotFoundException(sprintf('File "%s" not found (resolved from "%s").', $file, $name));
101104
}
102105

103106
return file_get_contents($file);
@@ -113,7 +116,7 @@ public function getEnv($prefix, $name, \Closure $getEnv)
113116
$env = $_SERVER[$name];
114117
} elseif (false === ($env = getenv($name)) || null === $env) { // null is a possible value because of thread safety issues
115118
if (!$this->container->hasParameter("env($name)")) {
116-
throw new EnvNotFoundException($name);
119+
throw new EnvNotFoundException(sprintf('Environment variable not found: "%s".', $name));
117120
}
118121

119122
if (null === $env = $this->container->getParameter("env($name)")) {

src/Symfony/Component/DependencyInjection/Exception/EnvNotFoundException.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,4 @@
1818
*/
1919
class EnvNotFoundException extends InvalidArgumentException
2020
{
21-
public function __construct(string $name)
22-
{
23-
parent::__construct(sprintf('Environment variable not found: "%s".', $name));
24-
}
2521
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +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'),
43+
'default' => array('bool', 'int', 'float', 'string', 'array'),
4444
'string' => array('string'),
4545
);
4646

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

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

442-
public function testDumpedFallbackEnvParameters()
442+
public function testDumpedDefaultEnvParameters()
443443
{
444444
$container = new ContainerBuilder();
445+
$container->setParameter('fallback_param', 'baz');
446+
$container->setParameter('fallback_env', '%env(foobar)%');
447+
$container->setParameter('env(foobar)', 'foobaz');
445448
$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)%');
449+
$container->setParameter('hello', '%env(default:fallback_param:bar)%');
450+
$container->setParameter('hello-bar', '%env(default:fallback_env:key:baz:json:foo)%');
448451
$container->compile();
449452

450453
$dumper = new PhpDumper($container);
451454
$dumper->dump();
452455

453-
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_fallback_env.php', $dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_FallbackParameters')));
456+
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_default_env.php', $dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_DefaultParameters')));
454457

455-
require self::$fixturesPath.'/php/services_fallback_env.php';
456-
$container = new \Symfony_DI_PhpDumper_Test_FallbackParameters();
458+
require self::$fixturesPath.'/php/services_default_env.php';
459+
$container = new \Symfony_DI_PhpDumper_Test_DefaultParameters();
457460
$this->assertSame('baz', $container->getParameter('hello'));
458-
$this->assertSame('baz', $container->getParameter('hello-bar'));
461+
$this->assertSame('foobaz', $container->getParameter('hello-bar'));
459462
}
460463

461464
public function testDumpedJsonEnvParameters()

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_fallback_env.php renamed to src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_default_env.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
* @final since Symfony 3.3
1616
*/
17-
class Symfony_DI_PhpDumper_Test_FallbackParameters extends Container
17+
class Symfony_DI_PhpDumper_Test_DefaultParameters extends Container
1818
{
1919
private $parameters;
2020
private $targetDirs = array();
@@ -86,6 +86,7 @@ public function getParameterBag()
8686
}
8787

8888
private $loadedDynamicParameters = array(
89+
'fallback_env' => false,
8990
'hello' => false,
9091
'hello-bar' => false,
9192
);
@@ -103,8 +104,9 @@ public function getParameterBag()
103104
private function getDynamicParameter($name)
104105
{
105106
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;
107+
case 'fallback_env': $value = $this->getEnv('foobar'); break;
108+
case 'hello': $value = $this->getEnv('default:fallback_param:bar'); break;
109+
case 'hello-bar': $value = $this->getEnv('default:fallback_env:key:baz:json:foo'); break;
108110
default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name));
109111
}
110112
$this->loadedDynamicParameters[$name] = true;
@@ -120,6 +122,8 @@ private function getDynamicParameter($name)
120122
protected function getDefaultParameters()
121123
{
122124
return array(
125+
'fallback_param' => 'baz',
126+
'env(foobar)' => 'foobaz',
123127
'env(foo)' => '{"foo": "bar"}',
124128
);
125129
}

0 commit comments

Comments
 (0)
0