-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DI][Improvement] Allow to define a default value on environment variables #25129
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
Comments
That should actually be quite easy, as a new processor. The actual syntax would need a swap: You can simplify your example a bit if you want:
|
But what about parameters:
env(resolve:DB_HOSTNAME): db
doctrine:
dbal:
host: '%env(resolve:DB_HOSTNAME)%' This also ask a good question: if we allow |
nope
it's already allowed |
OK, no, in fact it should be reversed: |
OK, thanks for your feedback. I'll try to open a PR soon for this default value improvement |
@vincentchalamon I don't understand this:
Resolve is needed in env vars because they can't interpolate the container parameters. But that's not a problem in your example: parameters:
# use container params here without doing anything special
env(DB_HOSTNAME): '%kernel.project_dir%/db'
doctrine:
dbal:
host: '%env(resolve:DB_HOSTNAME)%' I don't think Symfony needs to change anything about this behavior. Could you please share some realistic config that it's impossible to do with Symfony? Thanks! |
I was thinking about following syntax: parameters:
env(resolve:DB_HOSTNAME): 'foo' But I was wrong, the right syntax is what you wrote. Thanks :-) In this improvement/question issue, the idea is to simplify the configuration and not having to declare a parameter key for the default value: doctrine:
dbal:
host: '%env(resolve:default:DB_HOSTNAME:%kernel.project_dir%/db)%' |
What I said before: this won't work, as |
In this case, you're right (I was also afraid about But I think the default value in parameters:
locale: en
env(DB_HOST): %kernel.project_dir%/db
doctrine:
dbal:
driver: "%env(default:DATABASE_DRIVER:pdo_mysql)%"
host: "%env(resolve:DATABASE_HOSTNAME)%"
port: "%env(int:default:DATABASE_PORT:3306)%"
dbname: "%env(default:DATABASE_NAME:api_platform)%"
user: "%env(default:DATABASE_USER:api_platform)%"
password: "%env(default:DATABASE_PASSWORD:api_platform)%"
charset: "%env(default:DATABASE_CHARSET:utf-8)%"
server_version: "%env(default:DATABASE_SERVER_VERSION:5.7)%"
swiftmailer:
transport: '%env(default:MAILER_TRANSPORT:smtp)%'
host: '%env(default:MAILER_HOST:127.0.0.1)%'
username: '%env(default:MAILER_USER:null)%'
password: '%env(default:MAILER_PASSWORD:null)%' etc. for each environment variable used in my configuration (JWT Token Paths, CORS Allow Origin…). |
you're asking for much more: I guess that by "null", you're expecting |
@vincentchalamon I think that the real problem is that you are using env vars where you don't need to use env vars. Regular config params still work in Symfony. Remove some or all the env vars if you don't need them ... but if you really need them, there's a price to pay for env vars (define default values for them, propagate them to the production servers, etc.) |
OK, understood. Thanks for your feedbacks @nicolas-grekas @javiereguiluz |
Since Symfony 3.4, it's now possible to parse environment variables using
%env()%
tool: https://goo.gl/8Zsed6But in some environment, I don't have the right environment variables set, so the application is not bootable (on Insight analysis for instance). To fix it, I need to define the environment variable on a parameter key:
It should be interesting to parse a default value directly on the
%env()%
tool:The text was updated successfully, but these errors were encountered: