10000 Nullable environment variable processor by bpolaszek · Pull Request #29767 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Nullable environment variable processor #29767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* added `%env(trim:...)%` processor to trim a string value
* added `%env(default:...)%` processor to fallback to a default value
* added `%env(nullable:...)%` processor to allow empty variables to be processed as null values

4.2.0
-----
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/DependencyInjection/EnvVarProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static function getProvidedTypes()
'int' => 'int',
'json' => 'array',
'key' => 'bool|int|float|string|array',
'nullable' => 'bool|int|float|string|array',
'resolve' => 'string',
'default' => 'bool|int|float|string|array',
'string' => 'string',
Expand Down Expand Up @@ -195,6 +196,10 @@ public function getEnv($prefix, $name, \Closure $getEnv)
return str_getcsv($env);
}

if ('nullable' === $prefix) {
return '' === $env ? null : $env;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the env var is not defined at all? To me, not providing the env var means null.

Currently, you can already get a null behavior for FOO env var by setting a env(FOO) parameter:

parameters:
    env(FOO): null

so null is used as default value if FOO env var is not set.

Don't we just need this allowing an env var not to be set to get null, without defining a parameter as default?

Copy link
Contributor
@ro0NL ro0NL Jan 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we should avoid

env(DEFAULT_NULL): null
%env(DEFAULT_NULL)%

competing with

%env(nullable:RUNTIME_ARBITRARY_NULL)%

to take this further we could also deprecate setting null defaults, in favor of always using nullable: + string values (related to #27808)

Copy link
Contributor
@ogizanagi ogizanagi Jan 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, my point is %env(nullable:FOO)% should be accepted even if FOO is not set and no env(FOO) parameter exists => injected value will be null.

Second point: not sure we need the empty string to be considered null.

Copy link
Contributor
@ro0NL ro0NL Jan 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My worry is RUNTIME_ENV= can only be nulled by removing it or move it to config.

Currently there's only one source where null can come from, and it's: env(DEFAULT): ~

Adding nullable creates 2 scenarios. Hence my suggestion to deprecate env(DEFAULT): ~ in favor of

RUNTIME=
env(DEFAULT): ""

%env(nullable:DEFAULT)%
%env(nullable:RUNTIME)%

IMHO that's the easiest to reason about.

Copy link
Member
@nicolas-grekas nicolas-grekas Jan 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the env var is not defined, we already added the "default" processor in #28976. I think this processor should do one thing, turn empty strings to null - what it does currently - and "default" covers the other use cases.
I also don't agree deprecating env(FOO): ~ is a good idea: we shouldn't deprecate for the sake of it.

Copy link
Member
@nicolas-grekas nicolas-grekas Jan 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that "default" has not been released yet so we can also alter its behavior.
But each processor should have one responsibility - we shouldn't have many processors doing the same in slightly different ways.

}

if ('trim' === $prefix) {
return trim($env);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function testSimpleProcessor()
'int' => ['int'],
'json' => ['array'],
'key' => ['bool', 'int', 'float', 'string', 'array'],
'nullable' => ['bool', 'int', 'float', 'string', 'array'],
'resolve' => ['string'],
'default' => ['bool', 'int', 'float', 'string', 'array'],
'string' => ['string'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,29 @@ public function testGetEnvKeyChained()
];
}));
}

/**
* @dataProvider validNullables
*/
public function testGetEnvNullable($value, $processed)
{
$processor = new EnvVarProcessor(new Container());
$result = $processor->getEnv('nullable', 'foo', function ($name) use ($value) {
$this->assertSame('foo', $name);

return $value;
});
$this->assertSame($processed, $result);
}

public function validNullables()
{
return [
['hello', 'hello'],
['', null],
['null', 'null'],
['Null', 'Null'],
['NULL', 'NULL'],
];
}
}
0