Skip to content

Commit 800267d

Browse files
Merge branch '6.4' into 7.0
* 6.4: Add missing return types and enforce return types on all methods [ErrorHandler] Do not use ContextAwareNormalizerInterface anymore [6.4] Fix various `@psalm-return` annotations [WebProfilerBundle] Split and refactor all the JavaScript code [HttpClient] Allow custom working directory in TestHttpServer Remove ExpectDeprecationTrait where it is not used Remove BC layer for HttpFoundation < 6.1 [Routing] Prevent duplicated methods and schemes in Route [DependencyInjection] Add `defined` prefix for env var processor 🐛 (kernel) when configuring the container add services with php ext deprecate using date and time types with date objects with not-matching timezones
2 parents dd3fe6e + 49a3f10 commit 800267d

File tree

5 files changed

+29
-1
lines changed

5 files changed

+29
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Deprecate `ContainerAwareInterface` and `ContainerAwareTrait`, use dependency injection instead
8+
* Add `defined` env var processor that returns `true` for defined and neither null nor empty env vars
89

910
6.3
1011
---

EnvVarProcessor.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public static function getProvidedTypes(): array
5656
'require' => 'bool|int|float|string|array',
5757
'enum' => \BackedEnum::class,
5858
'shuffle' => 'array',
59+
'defined' => 'bool',
5960
];
6061
}
6162

@@ -103,6 +104,14 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
103104
return $backedEnumClassName::tryFrom($backedEnumValue) ?? throw new RuntimeException(sprintf('Enum value "%s" is not backed by "%s".', $backedEnumValue, $backedEnumClassName));
104105
}
105106

107+
if ('defined' === $prefix) {
108+
try {
109+
return '' !== ($getEnv($name) ?? '');
110+
} catch (EnvNotFoundException) {
111+
return false;
112+
}
113+
}
114+
106115
if ('default' === $prefix) {
107116
if (false === $i) {
108117
throw new RuntimeException(sprintf('Invalid env "default:%s": a fallback parameter should be provided.', $name));

ParameterBag/ParameterBag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public function resolve()
176176
* @param TValue $value
177177
* @param array $resolving An array of keys that are being resolved (used internally to detect circular references)
178178
*
179-
* @return (TValue is scalar ? array|scalar : array<array|scalar>)
179+
* @psalm-return (TValue is scalar ? array|scalar : array<array|scalar>)
180180
*
181181
* @throws ParameterNotFoundException if a placeholder references a parameter that does not exist
182182
* @throws ParameterCircularReferenceException if a circular reference if detected

Tests/Compiler/RegisterEnvVarProcessorsPassTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function testSimpleProcessor()
5050
'require' => ['bool', 'int', 'float', 'string', 'array'],
5151
'enum' => [\BackedEnum::class],
5252
'shuffle' => ['array'],
53+
'defined' => ['bool'],
5354
];
5455

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

Tests/EnvVarProcessorTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,4 +925,21 @@ public function testGetEnvCastsNull($expected, string $prefix)
925925
});
926926
}));
927927
}
928+
929+
/**
930+
* @dataProvider provideGetEnvDefined
931+
*/
932+
public function testGetEnvDefined(bool $expected, callable $callback)
933+
{
934+
$this->assertSame($expected, (new EnvVarProcessor(new Container()))->getEnv('defined', 'NO_SOMETHING', $callback));
935+
}
936+
937+
public static function provideGetEnvDefined(): iterable
938+
{
939+
yield 'Defined' => [true, fn () => 'foo'];
940+
yield 'Falsy but defined' => [true, fn () => '0'];
941+
yield 'Empty string' => [false, fn () => ''];
942+
yield 'Null' => [false, fn () => null];
943+
yield 'Env var not defined' => [false, fn () => throw new EnvNotFoundException()];
944+
}
928945
}

0 commit comments

Comments
 (0)