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

Appearance settings

Commit ce5a996

Browse filesBrowse files
committed
refactor: allow any boolish value
1 parent 0205893 commit ce5a996
Copy full SHA for ce5a996

File tree

3 files changed

+22
-41
lines changed
Filter options

3 files changed

+22
-41
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/EnvVarProcessor.php
+5-11Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public static function getProvidedTypes()
4141
return [
4242
'base64' => 'string',
4343
'bool' => 'bool',
44+
'not' => 'bool',
4445
'const' => 'bool|int|float|string|array',
4546
'csv' => 'array',
4647
'file' => 'string',
@@ -55,7 +56,6 @@ public static function getProvidedTypes()
5556
'string' => 'string',
5657
'trim' => 'string',
5758
'require' => 'bool|int|float|string|array',
58-
'not' => 'bool',
5959
];
6060
}
6161

@@ -192,8 +192,10 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv)
192192
return (string) $env;
193193
}
194194

195-
if ('bool' === $prefix) {
196-
return (bool) (filter_var($env, \FILTER_VALIDATE_BOOLEAN) ?: filter_var($env, \FILTER_VALIDATE_INT) ?: filter_var($env, \FILTER_VALIDATE_FLOAT));
195+
if (in_array($prefix, ['bool', 'not'], true)) {
196+
$env = (bool) (filter_var($env, \FILTER_VALIDATE_BOOLEAN) ?: filter_var($env, \FILTER_VALIDATE_INT) ?: filter_var($env, \FILTER_VALIDATE_FLOAT));
197+
198+
return 'not' === $prefix ? !$env : $env;
197199
}
198200

199201
if ('int' === $prefix) {
@@ -291,14 +293,6 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv)
291293
return trim($env);
292294
}
293295

294-
if ('not' === $prefix) {
295-
if (false === is_bool($env)) {
296-
throw new RuntimeException(sprintf('Env var `%s` has not been resolved to a boolean type. Please prefix with `bool:` first.', $name));
297-
}
298-
299-
return !$env;
300-
}
301-
302296
throw new RuntimeException(sprintf('Unsupported env var prefix "%s" for env name "%s".', $prefix, $name));
303297
}
304298
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterEnvVarProcessorsPassTest.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function testSimpleProcessor()
3333
'foo' => ['string'],
3434
'base64' => ['string'],
3535
'bool' => ['bool'],
36+
'not' => ['bool'],
3637
'const' => ['bool', 'int', 'float', 'string', 'array'],
3738
'csv' => ['array'],
3839
'file' => ['string'],
@@ -47,7 +48,6 @@ public function testSimpleProcessor()
4748
'string' => ['string'],
4849
'trim' => ['string'],
4950
'require' => ['bool', 'int', 'float', 'string', 'array'],
50-
'not' => ['bool'],
5151
];
5252

5353
$this->assertSame($expected, $container->getParameterBag()->getProvidedTypes());

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php
+16-29Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ public function testGetEnvBool($value, $processed)
6464
$this->assertSame($processed, $result);
6565
}
6666

67+
/**
68+
* @dataProvider validBools
69+
*/
70+
public function testGetEnvNot($value, $processed)
71+
{
72+
$processor = new EnvVarProcessor(new Container());
73+
74+
$result = $processor->getEnv('not', 'foo', function ($name) use ($value) {
75+
$this->assertSame('foo', $name);
76+
77+
return $value;
78+
});
79+
80+
$this->assertSame(!$processed, $result);
81+
}
82+
6783
public function validBools()
6884
{
6985
return [
@@ -610,33 +626,4 @@ public function testGetEnvInvalidPrefixWithDefault()
610626
return null;
611627
});
612628
}
613-
614-
public function testGetEnvNot()
615-
{
616-
$processor = new EnvVarProcessor(new Container());
617-
618-
$result = $processor->getEnv('not', 'foo', function ($name) {
619-
$this->assertSame('foo', $name);
620-
621-
return true;
622-
});
623-
624-
$this->assertFalse($result);
625-
626-
$result = $processor->getEnv('not', 'foo', function () { return false; });
627-
$this->assertTrue($result);
628-
}
629-
630-
public function testGetEnvNotWithInvalidType()
631-
{
632-
$processor = new EnvVarProcessor(new Container());
633-
634-
$this->expectException(RuntimeException::class);
635-
$this->expectExceptionMessage('Env var `foo` has not been resolved to a boolean type. Please prefix with `bool:` first.');
636-
$processor->getEnv('not', 'foo', function ($name) {
637-
$this->assertSame('foo', $name);
638-
639-
return 'bar';
640-
});
641-
}
642629
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.