helhum / dotenv-connector

Makes values from a .env file available as environment variables for composer based projects
MIT License
153 stars 17 forks source link

Are multiple .env files possible OOTB? #38

Closed nostadt closed 3 years ago

nostadt commented 3 years ago

I am currently trying to implement a hierarchy where first .env and then .env.local should be loaded. I don't get it to work though and from what I see it's not supported by this package. Is this true? Should I write my own Adapter or what is the best practice go to?

Best regards and thanks for your effort you put into the public packages!

Alex

helhum commented 3 years ago

Thanks for reaching out.

However it is hard to help without knowing what exactly you need, what you tried and what exact alternatives you are now considering.

Please post a composer.json config with a verbose description what you expect and what actually happens

nostadt commented 3 years ago

I've a TYPO3 instance with version 10 and helhum/dotenv-connector (v3.0.0) required. In my project root .env file exists.

This file contains currently a MySQL config and the TYPO3_CONTEXT.

The dot env config In the composer.json file looks like this:

    "helhum/dotenv-connector": {
      "adapter": "Helhum\\DotEnvConnector\\Adapter\\SymfonyDotEnv",
      "env-file": ".env"
    },

What I want to achieve is:

  1. Set TYPO3_CONTEXT via .env.local (works with your package)
  2. Have a versionized .env file.
  3. Have a local, not versionized .env.local file

What I have tried is to use the symfony package directly and load the files accordingly via AdditionalConfiguration.php.

(function() {
    $root = __DIR__ . '/../../';
    $dotenv = new \Symfony\Component\Dotenv\Dotenv();
    $dotenv->usePutenv(true);
    $dotenv->load($root . '.env');
    if (file_exists($root . '.env.local')) {
        $dotenv->overload($root . '.env.local');
    }
})();

While it reaches my goal with a local env file, it does not apply the TYPO3_CONTEXT.

What I have found is a method exposeToEnvironment which will be used in a composer plugin and I am pretty sure this is the reason why the TYPO3_CONTEXT will be applied with your package.

    public function exposeToEnvironment(string $dotEnvFile): void
    {
        if (!getenv('APP_ENV') && file_exists($dotEnvFile)) {
            $dotEnv = new Dotenv();
            $dotEnv->usePutenv();
            $dotEnv->load($dotEnvFile);
            $dotEnv->overload($dotEnvFile.'.local');
        }
    }

After adding $dotEnv->overload($dotEnvFile.'.local'); all my goals are achieved.

Yet, before implementing the wheel again I would like to know whether there is a config option by your package I have missed.

nostadt commented 3 years ago

Okay, I figured out one can include own files using composer. I have created ./env-loader.php and include this in the autoloader section of my composer file, which does the magic for me and it's early enough apparently.

helhum commented 3 years ago

Did you read and understand the section about adapters in the readme?

From what I read from your description, I'm not completely sure.

From what I grasp, the following configuration should do exactly what you want, without writing a single line of code anywhere.

    "helhum/dotenv-connector": {
      "adapter": "Helhum\\DotEnvConnector\\Adapter\\SymfonyLoadEnv"
    },
helhum commented 3 years ago

Please also refer to the Symfony docs on how you can use multiple env files and how they override each other with this configuration.