8000 feat: negated env var processor · symfony/symfony@bf08da7 · GitHub
[go: up one dir, main page]

Skip to content

Commit bf08da7

Browse files
committed
feat: negated env var processor
1 parent c757845 commit bf08da7

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

src/Symfony/Component/DependencyInjection/EnvVarProcessor.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public static function getProvidedTypes()
5555
'string' => 'string',
5656
'trim' => 'string',
5757
'require' => 'bool|int|float|string|array',
58+
'not' => 'bool',
5859
];
5960
}
6061

@@ -79,7 +80,7 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv)
7980
}
8081

8182
if (!isset($array[$key]) && !\array_key_exists($key, $array)) {
82-
throw new EnvNotFoundException(sprintf('Key "%s" not found in %s (resolved from "%s").', $key, json_encode($array), $next));
83+
throw new EnvNotFoundException(sprintf('Key "%s" not found in "%s" (resolved from "%s").', $key, json_encode($array), $next));
8384
}
8485

8586
return $array[$key];
@@ -290,6 +291,14 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv)
290291
return trim($env);
291292
}
292293

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+
293302
throw new RuntimeException(sprintf('Unsupported env var prefix "%s" for env name "%s".', $prefix, $name));
294303
}
295304
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function testSimpleProcessor()
4747
'string' => ['string'],
4848
'trim' => ['string'],
4949
'require' => ['bool', 'int', 'float', 'string', 'array'],
50+
'not' => ['bool'],
5051
];
5152

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

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,4 +610,33 @@ public function testGetEnvInvalidPrefixWithDefault()
610610
return null;
611611
});
612612
}
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+
}
613642
}

0 commit comments

Comments
 (0)
0