Skip to content

[FrameworkBundle] forced use of "fake" environment #529

@akomm

Description

@akomm

Why are this the only options:

// Load cached env vars if the .env.local.php file exists
// Run "composer dump-env prod" to create it (requires symfony/flex >=1.2)
if (is_array($env = @include dirname(__DIR__).'/.env.local.php')) {
    $_SERVER += $env;
    $_ENV += $env;
} elseif (!class_exists(Dotenv::class)) {
    throw new RuntimeException('Please run "composer require symfony/dotenv" to load the ".env" files configuring the application.');
} else {
    // load all the .env files
    (new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
}

Why not for example this?

if (!($_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? null)) {
    if (is_array($env = @include dirname(__DIR__).'/.env.local.php')) {
        $_SERVER += $env;
        $_ENV += $env;
    } elseif (!class_exists(Dotenv::class)) {
        throw new RuntimeException('Please run "composer require symfony/dotenv" to load the ".env" files configuring the application.');
    } else {
        // load all the .env files
        (new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
    }
}

Is there a reason not allowing "native" environment to be used I missed? I know, this is all not optimized for docker, because docker is not the only way of doing things, but:

Docker is not the problem! Docker only makes the design flaw visible. The same applies, when you run without docker on a "user per app" basis and the user has environment set to run the app and there might be some task executed that has to run with the same user and share the environment. The environment is here isolated to the user the app runs on. No collisions at all. Now if you do not have this setup, the above would still work with DotEnv or dumped .env.local.php.

My strategy using docker would be then:

  • remove symfony/dotenv
  • use .env.local as .env before the flex change
  • use .env as .env.dist before the flex change
  • instead of "overriding" only some vars from .env in .env.local, have them all defined, like before in .env

This would allow to load native environment als also have a single environment "object". But would also allow the other way of usage you intended for also good reasons.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions