8000 [Dotenv] load .env.dist when it exists and .env is not found by nicolas-grekas · Pull Request #29171 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Dotenv] load .env.dist when it exists and .env is not found #29171

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

Merged
merged 1 commit into from
Nov 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Dotenv] load .env.dist when it exists and .env is not found
  • Loading branch information
nicolas-grekas committed Nov 11, 2018
commit 841185bb9f549117138b6b66993a15b48ef72c94
1 change: 1 addition & 0 deletions src/Symfony/Component/Dotenv/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* added `Dotenv::overload()` and `$overrideExistingVars` as optional parameter of `Dotenv::populate()`
* added `Dotenv::loadEnv()` to load a .env file and its corresponding .env.local, .env.$env and .env.$env.local files if they exist

3.3.0
-----
Expand Down
8000
7 changes: 6 additions & 1 deletion src/Symfony/Component/Dotenv/Dotenv.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function load(string $path, string ...$extraPaths): void
* Loads a .env file and the corresponding .env.local, .env.$env and .env.$env.local files if they exist.
*
* .env.local is always ignored in test env because tests should produce the same results for everyone.
* .env.dist is loaded when it exists and .env is not found.
*
* @param string $path A file to load
* @param string $varName The name of the env vars that defines the app env
Expand All @@ -66,7 +67,11 @@ public function load(string $path, string ...$extraPaths): void
*/
public function loadEnv(string $path, string $varName = 'APP_ENV', string $defaultEnv = 'dev', array $testEnvs = array('test')): void
{
$this->load($path);
if (file_exists($path) || !file_exists($p = "$path.dist")) {
$this->load($path);
} else {
$this->load($p);
}

if (null === $env = $_SERVER[$varName] ?? $_ENV[$varName] ?? null) {
$this->populate(array($varName => $env = $defaultEnv));
Expand Down
11 changes: 9 additions & 2 deletions src/Symfony/Component/Dotenv/Tests/DotenvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,18 @@ public function testLoadEnv()
(new DotEnv())->loadEnv($path, 'TEST_APP_ENV');
$this->assertSame('devlocalBAR', getenv('FOO'));

// .env.dist

unlink($path);
file_put_contents("$path.dist", 'BAR=distBAR');
(new DotEnv())->loadEnv($path, 'TEST_APP_ENV');
$this->assertSame('distBAR', getenv('BAR'));

putenv('FOO');
putenv('BAR');
unlink($path);
unlink("$path.dev");
unlink("$path.dist");
unlink("$path.local");
unlink("$path.dev");
unlink("$path.dev.local");
rmdir($tmpdir);
}
Expand Down
0