8000 [DI][Improvement] Allow to define a default value on environment variables · Issue #25129 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[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

Closed
vincentchalamon opened this issue Nov 23, 2017 · 12 comments
Closed

Comments

@vincentchalamon
Copy link
Contributor
Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? no
Symfony version 3.4

Since Symfony 3.4, it's now possible to parse environment variables using %env()% tool: https://goo.gl/8Zsed6

But 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:

parameters:
    db_hostname: '%env(DB_HOSTNAME)%'
    env(DB_HOSTNAME): db

doctrine:
    dbal:
        host: '%db_hostname%'

It should be interesting to parse a default value directly on the %env()% tool:

doctrine:
    dbal:
        host: '%env(default:db:DB_HOSTNAME)%'
@nicolas-grekas
Copy link
Member

That should actually be quite easy, as a new processor. The actual syntax would need a swap:
%env(default:FOO:defaultvalue)%, so that the default value could contain a colon.
BUT, this would actually be of limited use, since the allowed characters between env(...) is quite limited (and it shouldn't be widened.)

You can simplify your example a bit if you want:

parameters:
    env(DB_HOSTNAME): db

doctrine:
    dbal:
        host: '%env(DB_HOSTNAME)%'

@vincentchalamon
Copy link
Contributor Author

But what about resolve (for instance)? Should I declare exactly the same in parameters?:

parameters:
    env(resolve:DB_HOSTNAME): db

doctrine:
    dbal:
        host: '%env(resolve:DB_HOSTNAME)%'

This also ask a good question: if we allow default keyword, we should also allow multiple keywords usage, like: %env(default:resolve:DB_HOSTNAME:defaultvalue)%

@nicolas-grekas
Copy link
Member

Should I declare exactly the same in parameter

nope

should also allow multiple keywords usage

it's already allowed

@nicolas-grekas
Copy link
Member

should also allow multiple keywords usage

OK, no, in fact it should be reversed: resolve:default:DB_HOSTNAME:defaultvalue

@vincentchalamon
Copy link
Contributor Author

OK, thanks for your feedback. I'll try to open a PR soon for this default value improvement

@javiereguiluz
Copy link
Member

@vincentchalamon I don't understand this:

But what about resolve (for instance)? Should I declare exactly the same in parameters?:

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!

@vincentchalamon
Copy link
Contributor Author

@javiereguiluz

But what about resolve (for instance)? Should I declare exactly the same in parameters?:

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)%'

@nicolas-grekas
Copy link
Member

host: '%env(resolve:default:DB_HOSTNAME:%kernel.project_dir%/db)%'

What I said before: this won't work, as / is not allowed between env().
Which means I'm personally not sure it's a good idea.

@vincentchalamon
Copy link
Contributor Author

In this case, you're right (I was also afraid about % or other characters not allowed in env()), and I should declare a parameter as default value of my environment variable.

But I think the default value in env() could be useful for more simple variables to don't declare them all as parameters just for a default value. For instance, in my current project, I should declare 18 parameters as default values for my 18 environment variables. So the ideal configuration should be like (using env(default…) idea):

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…).

@nicolas-grekas
Copy link
Member
nicolas-grekas commented Nov 23, 2017

you're asking for much more: I guess that by "null", you're expecting null, not "null".
Basically this is asking to have a runtime parser for env vars.
Solving the simple case will create a situation where ppl will expect / to be allowed, and as such generate negative feedback for sure (because it doesn't work). Better teach ppl the proper way, that is feature full...

@javiereguiluz
Copy link
Member

@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.)

@vincentchalamon
Copy link
Contributor Author

OK, understood. Thanks for your feedbacks @nicolas-grekas @javiereguiluz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
0