Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

[Proposal] Dotenv implementation #292

Closed
jasonhebert opened this issue Nov 3, 2016 · 5 comments
Closed

[Proposal] Dotenv implementation #292

jasonhebert opened this issue Nov 3, 2016 · 5 comments

Comments

@jasonhebert
Copy link

Description:

I would like to suggest making available within the bootstrapping process the instance of Dotenv used to load environment variables and provide a mechanism to add and enforce validations on the .env file using the Dotenv builtin validators.

Currently the environment variables are loaded within the bootstrap() method of the Illuminate/Foundation/Bootstrap/DetectEnvironment.php class using (new Dotenv($app->environmentPath(), $app->environmentFile()))->load();. The object instance is thrown away and doesn't give developers an easy opportunity to enforce environment variable validations such as required(), notEmpty(), isInteger() or allowedValues().

@Garbee
Copy link

Garbee commented Nov 3, 2016

Just set the requirements in your AppServiceProvider@register like so:

(new Dotenv(base_path('.env')))->required([
            'SERVICES_STRIPE_PUBLIC',
            'SERVICES_STRIPE_SECRET',
            'SERVICES_EASYPOST_SECRET',
            'MAIL_DRIVER',
            'MAIL_HOST',
            'MAIL_PASSWORD',
            'MAIL_USERNAME',
        ]);

@jasonhebert
Copy link
Author

That's all fine @Garbee but it's a bit hacky to have to create a new instance of Dotenv when one was just thrown away.

@Garbee
Copy link

Garbee commented Nov 4, 2016

What would a good solution look like to the problem look like to you?

@jasonhebert
Copy link
Author

jasonhebert commented Nov 4, 2016

What would be nice would be to have that initial instance of Dotenv registered and made available as a singleton so that the instance could be used anywhere in the app without having to reload the .env file each time you need to do a validation. This would give you the ability to do, as an example, things like:

Dotenv::required([
    'MAIL_DRIVER',
    'MAIL_HOST',
    'MAIL_PASSWORD',
    'MAIL_USERNAME',
]);

Dotenv::required([
    'SERVICES_STRIPE_PUBLIC',
    'SERVICES_STRIPE_SECRET',
    'SERVICES_EASYPOST_SECRET',
])->notEmpty();

@sisve
Copy link

sisve commented Nov 14, 2016

You should use cached configurations in production, and dotenv will never be loaded in Laravel 5.2 (and newer). This means that some kind of environment-validation function should only execute when the config is cached; and not on normal requests.

Relevant comments:
vlucas/phpdotenv#76 (comment)
vlucas/phpdotenv#76 (comment)

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

No branches or pull requests

4 participants