[DI] Add a "default" EnvProcessor#28976
Conversation
|
why not stick with |
|
@ro0NL because sometime the env variable exist, but the processing doesn't. See my examples. |
|
understood. Im a bit skeptical about making values part of the prefix string, but i cant think of anything better either. |
|
That won't work as the |
| 4.3.0 | ||
| ----- | ||
|
|
||
| * added `%env(catch:...)%` processor to catch exception and fallback to a default value |
There was a problem hiding this comment.
it must be (catch:...:DEFAULT_KEY) right
There was a problem hiding this comment.
to be consistent with added %env(key:...)% processor
src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterEnvVarProcessorsPassTest.php
Outdated
Show resolved
Hide resolved
| } | ||
|
|
||
| $next = substr($name, 0, $i); | ||
| $default = substr($name, $i + 1); |
There was a problem hiding this comment.
perhaps check to be non empty, im also worried it causes confusion as the last segment will always be used. If you forget it, another part will be implicitly used.
There was a problem hiding this comment.
refactored the logic to default:fallback:origin in order to catch only NotFoundException and let RuntimeException bubble
src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php
Outdated
Show resolved
Hide resolved
|
Thank your for the review @ro0NL , changed the logic in the PR to trap only the |
| return $getEnv($next); | ||
| } catch (EnvNotFoundException $e) { | ||
| if (!$this->container->hasParameter($default)) { | ||
| throw new EnvNotFoundException(sprintf('Parameter "%s" not found. (fallback from not found "%s")', $default, $next), 0, $e); |
There was a problem hiding this comment.
sprintf('Invalid env fallback in "default:%s": parameter "%s" not found.', $name, $default)?
There was a problem hiding this comment.
should it be a runtime exception btw?
There was a problem hiding this comment.
fix message.
what's about default:my_fallback:default:optionnal_parameter_injected_by_third_party:foo?
src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterEnvVarProcessorsPassTest.php
Outdated
Show resolved
Hide resolved
b7e1db3 to
16461d6
Compare
| @@ -18,8 +18,4 @@ | |||
| */ | |||
| class EnvNotFoundException extends InvalidArgumentException | |||
There was a problem hiding this comment.
while at it.. extends RuntimeException?
There was a problem hiding this comment.
If the variable is not found or a a key contain a typo, it make sens to be a logic exception.
|
@jderusse your examples are great as always ... but would this feature require any changes in Symfony apps? (both when using it as a stand-alone component and when using it as part of a full-stack Symfony app). Thanks! |
|
@javiereguiluz Same implementation thant #28975 😉 |
nicolas-grekas
left a comment
There was a problem hiding this comment.
please submit a doc PR or doc issue at least
289f283 to
aee4e33
Compare
|
Thank you @jderusse. |
This PR was squashed before being merged into the 4.3-dev branch (closes #28976). Discussion ---------- [DI] Add a "default" EnvProcessor | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | TODO This PR add a new fallback env processor in order to return a default value when the primary processor is not able to fetch a value (env variable, file or key does not exists) ``` # default_host: localhost host: '%env(default:default_host:OPTIONAL_ENV_VARIABLE)%" default_secret: this secret is not secret secret: '%env(default:default_secret:file:THIS_FILE_ONLY_EXIST_IN_PRODUCTION)%" default_charset: utf8 charset: '%env(default:default_charset:key:charset:json:DATABASE_CONFIG)%" ``` Commits ------- aee4e33 [DI] Add a \"default\" EnvProcessor
This PR was merged into the master branch. Discussion ---------- Add env default processor This PR documents symfony/symfony#28976 Commits ------- 8d3afcc Add env default processor
|
Hey guys, awesome work! thanks for this change. But IMO, the syntax is a little bit illegible, if it would be great to use the syntax in shell I can do a PR for that, wdyt? |
… "default" one (nicolas-grekas) This PR was merged into the 4.3-dev branch. Discussion ---------- [DI] replace "nullable" env processor by improving the "default" one | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Neither `nullable` nor `default` are released yet. I propose to replace the `nullable` processor (see #29767) with an improved `default` one (from #28976). `%env(default::FOO)%` now defaults to `null` when the env var doesn't exist or compares to false". ping @jderusse @bpolaszek Commits ------- c50aad2 [DI] replace "nullable" env processor by improving the "default" one
|
IMHO and my devops team Let me know in a new pull request with this rename and creating a new |
This PR add a new fallback env processor in order to return a default value when the primary processor is not able to fetch a value (env variable, file or key does not exists)