8000 bug #29129 [Dotenv] add loadEnv(), a smoother alternative to loadForE… · symfony/symfony@664a032 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 664a032

Browse files
bug #29129 [Dotenv] add loadEnv(), a smoother alternative to loadForEnv() (nicolas-grekas)
This PR was merged into the 4.2-dev branch. Discussion ---------- [Dotenv] add loadEnv(), a smoother alternative to loadForEnv() | Q | A | ------------- | --- | Branch? | 4.2 | Bug fix? | yes | New feature? | no | BC breaks? | yes (4.2-only) | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This PR replaces the `loadForEnv()` method introduced in #28533 by a new `loadEnv()` method. - It accepts only one mandatory argument: `$path`, which is the path to the `.env` file. - The 2nd argument is optional and defines the name of the environment variable that defines the Symfony env. This plays better with the current practice of defining the env in `.env` (`loadForEnv()` requires knowing the env before being called, leading to a chicken-n-egg situation that `loadEnv()` avoids.) - the possibility to load several files at once is removed. We don't have a use case for it and those who do can call `loadEnv()` in a loop anyway. In addition to $path (.env), the following files are loaded, the latter taking precedence in this order: .env < env.local < .env.$env < .env.$env.local Note that `loadForEnv()` used to give higher precedence to .env.local vs .env.$env. The new behavior is aligned with [the order used by create-react-app](https://github.com/facebook/create-react-app/blob/master/docusaurus/docs/adding-custom-environment-variables.md#what-other-env-files-can-be-used). It also allows overriding the env in .env.local, which should be convenient for DX. Last but not least, the "test" env has this special behaviors: - `.env.local` file is skipped for the "test" env (same as before and as in create-react-app) - ~vars defined in .env files **override** real env vars (similar to what Rails' dotenv does: you don't want your tests to randomly fail because of some real env vars)~. Commits ------- 0cf9acb [Dotenv] add loadEnv(), a smoother alternative to loadForEnv()
2 parents 99856a9 + 0cf9acb commit 664a032

File tree

2 files changed

+51
-48
lines changed

2 files changed

+51
-48
lines changed

src/Symfony/Component/Dotenv/Dotenv.php

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,34 +48,41 @@ final class Dotenv
4848
*/
4949
public function load(string $path, string ...$extraPaths): void
5050
{
51-
$this->doLoad(false, false, \func_get_args());
51+
$this->doLoad(false, \func_get_args());
5252
}
5353

5454
/**
55-
* Loads one or several .env and the corresponding .env.$env, .env.local and .env.$env.local files if they exist.
55+
* Loads a .env file and the corresponding .env.local, .env.$env and .env.$env.local files if they exist.
5656
*
5757
* .env.local is always ignored in test env because tests should produce the same results for everyone.
5858
*
59-
* @param string $path A file to load
60-
* @param ...string $extraPaths A list of additional files to load
59+
* @param string $path A file to load
60+
* @param string $varName The name of the env vars that defines the app env
61+
* @param string $defaultEnv The app env to use when none is defined
62+
* @param array $testEnvs A list of app envs for which .env.local should be ignored
6163
*
6264
* @throws FormatException when a file has a syntax error
6365
* @throws PathException when a file does not exist or is not readable
64-
*
65-
* @see https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
6666
*/
67-
public function loadForEnv(string $env, string $path, string ...$extraPaths): void
67+
public function loadEnv(string $path, string $varName = 'APP_ENV', string $defaultEnv = 'dev', array $testEnvs = array('test')): void
6868
{
69-
$paths = \func_get_args();
70-
for ($i = 1; $i < \func_num_args(); ++$i) {
71-
$path = $paths[$i];
72-
$pathList = array($path, "$path.$env");
73-
if ('test' !== $env) {
74-
$pathList[] = "$path.local";
75-
}
76-
$pathList[] = "$path.$env.local";
69+
$this->load($path);
70+
71+
if (null === $env = $_SERVER[$varName] ?? $_ENV[$varName] ?? null) {
72+
$this->populate(array($varName => $env = $defaultEnv));
73+
}
7774

78-
$this->doLoad(false, true, $pathList);
75+
if (!\in_array($env, $testEnvs, true) && file_exists($p = "$path.local")) {
76+
$this->load($p);
77+
$env = $_SERVER[$varName] ?? $_ENV[$varName] ?? $env;
78+
}
79+
80+
if (file_exists($p = "$path.$env")) {
81+
$this->load($p);
82+
}
83+
84+
if (file_exists($p = "$path.$env.local")) {
85+
$this->load($p);
7986
}
8087
}
8188

@@ -90,7 +97,7 @@ public function loadForEnv(string $env, string $path, string ...$extraPaths): vo
9097
*/
9198
public function overload(string $path, string ...$extraPaths): void
9299
{
93-
$th 67E6 is->doLoad(true, false, \func_get_args());
100+
$this->doLoad(true, \func_get_args());
94101
}
95102

96103
/**
@@ -435,14 +442,14 @@ private function createFormatException($message)
435442
return new FormatException($message, new FormatExceptionContext($this->data, $this->path, $this->lineno, $this->cursor));
436443
}
437444

438-
private function doLoad(bool $overrideExistingVars, bool $ignoreMissingExtraPaths, array $paths): void
445+
private function doLoad(bool $overrideExistingVars, array $paths): void
439446
{
440-
foreach ($paths as $i => $path) {
441-
if (is_readable($path) && !is_dir($path)) {
442-
$this->populate($this->parse(file_get_contents($path), $path), $overrideExistingVars);
443-
} elseif (!$ignoreMissingExtraPaths || 0 === $i) {
447+
foreach ($paths as $path) {
448+
if (!is_readable($path) || is_dir($path)) {
444449
throw new PathException($path);
445450
}
451+
452+
$this->populate($this->parse(file_get_contents($path), $path), $overrideExistingVars);
446453
}
447454
}
448455
}

src/Symfony/Component/Dotenv/Tests/DotenvTest.php

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public function testLoad()
186186
$this->assertSame('BAZ', $bar);
187187
}
188188

189-
public function testLoadForEnv()
189+
public function testLoadEnv()
190190
{
191191
unset($_ENV['FOO']);
192192
unset($_ENV['BAR']);
@@ -197,50 +197,46 @@ public function testLoadForEnv()
197197

198198
@mkdir($tmpdir = sys_get_temp_dir().'/dotenv');
199199

200-
$path1 = tempnam($tmpdir, 'sf-');
201-
$path2 = tempnam($tmpdir, 'sf-');
202-
203-
file_put_contents($path1, 'FOO=BAR');
204-
file_put_contents($path2, 'BAR=BAZ');
200+
$path = tempnam($tmpdir, 'sf-');
205201

206202
// .env
207203

208-
(new DotEnv())->loadForEnv('dev', $path1, $path2);
209-
204+
file_put_contents($path, 'FOO=BAR');
205+
(new DotEnv())->loadEnv($path, 'TEST_APP_ENV');
210206
$this->assertSame('BAR', getenv('FOO'));
211-
$this->assertSame('BAZ', getenv('BAR'));
212-
213-
// .env.dev
214-
215-
file_put_contents("$path1.dev", 'FOO=devBAR');
216-
(new DotEnv())->loadForEnv('dev', $path1, $path2);
217-
$this->assertSame('devBAR', getenv('FOO'));
207+
$this->assertSame('dev', getenv('TEST_APP_ENV'));
218208

219209
// .env.local
220210

221-
file_put_contents("$path1.local", 'FOO=localBAR');
222-
(new DotEnv())->loadForEnv('dev', $path1, $path2);
211+
file_put_contents("$path.local", 'FOO=localBAR');
212+
(new DotEnv())->loadEnv($path, 'TEST_APP_ENV');
223213
$this->assertSame('localBAR', getenv('FOO'));
224214

225215
// special case for test
226216

227-
file_put_contents("$path1.local", 'FOO=testBAR');
228-
(new DotEnv())->loadForEnv('test', $path1, $path2);
217+
$_SERVER['TEST_APP_ENV'] = 'test';
218+
(new DotEnv())->loadEnv($path, 'TEST_APP_ENV');
229219
$this->assertSame('BAR', getenv('FOO'));
230220

221+
// .env.dev
222+
223+
unset($_SERVER['TEST_APP_ENV']);
224+
file_put_contents("$path.dev", 'FOO=devBAR');
225+
(new DotEnv())->loadEnv($path, 'TEST_APP_ENV');
226+
$this->assertSame('devBAR', getenv('FOO'));
227+
231228
// .env.dev.local
232229

233-
file_put_contents("$path1.dev.local", 'FOO=devlocalBAR');
234-
(new DotEnv())->loadForEnv('dev', $path1, $path2);
230+
file_put_contents("$path.dev.local", 'FOO=devlocalBAR');
231+
(new DotEnv())->loadEnv($path, 'TEST_APP_ENV');
235232
$this->assertSame('devlocalBAR', getenv('FOO'));
236233

237234
putenv('FOO');
238235
putenv('BAR');
239-
unlink($path1);
240-
unlink("$path1.dev");
241-
unlink("$path1.local");
242-
unlink("$path1.dev.local");
243-
unlink($path2);
236+
unlink($path);
237+
unlink("$path.dev");
238+
unlink("$path.local");
239+
unlink("$path.dev.local");
244240
rmdir($tmpdir);
245241
}
246242

0 commit comments

Comments
 (0)
0