-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Cast env vars to null or bool when referencing them using #[Autowire(env: '...')]
depending on the signature of the corresponding parameter
#53971
Conversation
21386a2
to
6b838ba
Compare
I know this is not a "good" practice, but i have use multiple times the following method to "feature-flag" some services. SERVICE_SUPER_KEY=my_super_key [Autowire(env: 'SERVICE_SUPER_KEY')] bool $superServiceEnabled With your PR that would create the opposite problem of your example :| 🤷 |
@smnandre your use case could be covered if the code for the bool processor were doing this in EnvVarProcessor: filter_var($env, \FILTER_VALIDATE_BOOL, \FILTER_NULL_ON_FAILURE) ?? filter_var($env, \FILTER_VALIDATE_INT, \FILTER_NULL_ON_FAILURE) ?? filter_var($env, \FILTER_VALIDATE_FLOAT, \FILTER_NULL_ON_FAILURE) ?? true Worth considering? |
Similarly, we could also add |
As i said, this usage was a bit hacky, so we can "ignore it", but that will deserve some warning / upgrade mentions, no ? |
It makes sense to me but the changelog should explain a bit more the change and the new behavior. |
src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php
Outdated
Show resolved
Hide resolved
#[Autowire(env: '...')]
depending on the signature of the corresponding parameter
6b838ba
to
83c82e0
Compare
…them using `#[Autowire(env: '...')]` depending on the signature of the corresponding parameter
83c82e0
to
5890327
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I finished the PR, improving the logic a bit and adding support for default::
when a parameter defaults to null
.
#[Autowire(env: '...')]
depending on the signature of the corresponding parameter#[Autowire(env: '...')]
depending on the signature of the corresponding parameter
Thank you @ruudk. |
See #53918
This PR automatically adds
bool
Environment Variable Processors on#[Autowire(env: 'KEY')] bool $key
arguments.The idea behind this, is to prevent mistakes being made. If you omit the
bool
env var processor, passingKEY=false
will become true-ish and thus mark$key
astrue
.With the
bool
env processor,KEY=false
becomesfalse
.It also automatically adds the
default::
prefix if the default value of an argument isnull
.