Skip to content

Commit bbf79ba

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

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

src/Symfony/Component/DependencyInjection/EnvVarProcessor.php

Lines changed: 10 additions & 0 deletions
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

@@ -290,6 +291,15 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv)
290291
return trim($env);
291292
}
292293

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

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->assertSame(false, $result);
625+
626+
$result = $processor->getEnv('not', 'foo', function () { return false; });
627+
$this->assertSame(true, $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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy