Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e2af3b9

Browse files
[Dotenv] add loadEnv(), a smoother alternative to loadForEnv()
1 parent 8d277ce commit e2af3b9

File tree

2 files changed

+59
-55
lines changed

2 files changed

+59
-55
lines changed

src/Symfony/Component/Dotenv/Dotenv.php

Lines changed: 36 additions & 28 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-
$this->doLoad(true, false, \func_get_args());
100+
$this->doLoad(true, \func_get_args());
94101
}
95102

96103
/**
@@ -101,7 +108,8 @@ public function overload(string $path, string ...$extraPaths): void
101108
*/
102109
public function populate(array $values, bool $overrideExistingVars = false): void
103110
{
104-
$loadedVars = array_flip(explode(',', getenv('SYMFONY_DOTENV_VARS')));
111+
$updateLoadedVars = false;
112+
$loadedVars = array_flip(explode(',', $_SERVER['SYMFONY_DOTENV_VARS'] ?? $_ENV['SYMFONY_DOTENV_VARS'] ?? ''));
105113
unset($loadedVars['']);
106114

107115
foreach ($values as $name => $value) {
@@ -117,14 +125,14 @@ public function populate(array $values, bool $overrideExistingVars = false): voi
117125
$_SERVER[$name] = $value;
118126
}
119127

120-
$loadedVars[$name] = true;
128+
if (!isset($loadedVars[$name])) {
129+
$loadedVars[$name] = $updateLoadedVars = true;
130+
}
121131
}
122132

123-
if ($loadedVars) {
133+
if ($updateLoadedVars) {
124134
$loadedVars = implode(',', array_keys($loadedVars));
125-
putenv("SYMFONY_DOTENV_VARS=$loadedVars");
126-
$_ENV['SYMFONY_DOTENV_VARS'] = $loadedVars;
127-
$_SERVER['SYMFONY_DOTENV_VARS'] = $loadedVars;
135+
putenv('SYMFONY_DOTENV_VARS='.$_ENV['SYMFONY_DOTENV_VARS'] = $_SERVER['SYMFONY_DOTENV_VARS'] = $loadedVars);
128136
}
129137
}
130138

@@ -434,14 +442,14 @@ private function createFormatException($message)
434442
return new FormatException($message, new FormatExceptionContext($this->data, $this->path, $this->lineno, $this->cursor));
435443
}
436444

437-
private function doLoad(bool $overrideExistingVars, bool $ignoreMissingExtraPaths, array $paths): void
445+
private function doLoad(bool $overrideExistingVars, array $paths): void
438446
{
439-
foreach ($paths as $i => $path) {
440-
if (is_readable($path) && !is_dir($path)) {
441-
$this->populate($this->parse(file_get_contents($path), $path), $overrideExistingVars);
442-
} elseif (!$ignoreMissingExtraPaths || 0 === $i) {
447+
foreach ($paths as $path) {
448+
if (!is_readable($path) || is_dir($path)) {
443449
throw new PathException($path);
444450
}
451+
452+
$this->populate($this->parse(file_get_contents($path), $path), $overrideExistingVars);
445453
}
446454
}
447455
}

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

Lines changed: 23 additions & 27 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

@@ -373,7 +369,7 @@ public function testMemorizingLoadedVarsNamesInSpecialVar()
373369

374370
public function testOverridingEnvVarsWithNamesMemorizedInSpecialVar()
375371
{
376-
putenv('SYMFONY_DOTENV_VARS=FOO,BAR,BAZ');
372+
putenv('SYMFONY_DOTENV_VARS='.$_SERVER['SYMFONY_DOTENV_VARS'] = 'FOO,BAR,BAZ');
377373

378374
putenv('FOO=foo');
379375
putenv('BAR=bar');

0 commit comments

Comments
 (0)