tempestphp / tempest-framework

The PHP framework that gets out of your way 🌊
https://tempestphp.com
MIT License
995 stars 72 forks source link

Don't die on missing .env #130

Closed aidan-casey closed 8 months ago

aidan-casey commented 8 months ago

It is entirely valid to load environment variables from the system and will commonly done with things like Docker. I'm not sure we should be preventing the application from loading without the .env file.

alibori commented 8 months ago

Hi @aidan-casey !

what you are proposing is to remove the die block from the catch?

try {
    $dotenv = Dotenv::createUnsafeImmutable($root);
    $dotenv->load();
} catch (InvalidPathException) {
    die("Missing .env file in {$root}" . PHP_EOL);
}
aidan-casey commented 8 months ago

Yup!

sanderdlm commented 8 months ago

This is supported out of the box by the package you're using. You can do

$dotenv = Dotenv::createUnsafeImmutable($root);
$dotenv->loadSafe();

See https://github.com/vlucas/phpdotenv README.

To suppress the exception that is thrown when there is no .env file, you can use loadSafe

aidan-casey commented 8 months ago

I thought I recalled there being a method for this. Thank you!

brendt commented 8 months ago

Just as a sidenote: what a confusing API 🙃

createUnsafeImmutable
loadSafe

We're creating unsafe immutable… what exactly? And then we're safe loading the unsafe stuff? 🤯