Skip to content

Navigation Menu

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 bbf79ba

Browse filesBrowse files
committed
feat: negated env var processor
1 parent c757845 commit bbf79ba
Copy full SHA for bbf79ba

File tree

3 files changed

+40
-0
lines changed
Filter options

3 files changed

+40
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/EnvVarProcessor.php
+10Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterEnvVarProcessorsPassTest.php
+1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php
+29Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.