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

Skip to content

Commit 8f5229f

Browse files
committed
feature #26859 [Dotenv] add a flag to allow env vars override (fmata)
This PR was merged into the 4.2-dev branch. Discussion ---------- [Dotenv] add a flag to allow env vars override | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #26846 | License | MIT | Doc PR | symfony/symfony-docs#9568 I choose to use a new parameter in the constructor instead of `populate()` to not add boilerplate code to them who want allow overriding in their current setup. It's just a parameter to add in `Dotenv` creation instead of change or customize the loading of different .env files. I targeted 4.1 despite the feature freeze because it's a small change but if you don't agree I can change to 4.2. ~~If you accept this PR I will do the doc PR then.~~ doc ready Commits ------- 228b220 [Dotenv] add Dotenv::overload() to allow env vars override
2 parents 4401f2f + 228b220 commit 8f5229f

File tree

3 files changed

+83
-13
lines changed

3 files changed

+83
-13
lines changed

src/Symfony/Component/Dotenv/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.2.0
5+
-----
6+
7+
* added `Dotenv::overload()` and `$overrideExistingVars` as optional parameter of `Dotenv::populate()`
8+
49
3.3.0
510
-----
611

src/Symfony/Component/Dotenv/Dotenv.php

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,33 +47,38 @@ final class Dotenv
4747
*/
4848
public function load(string $path, string ...$paths): void
4949
{
50-
array_unshift($paths, $path);
51-
52-
foreach ($paths as $path) {
53-
if (!is_readable($path) || is_dir($path)) {
54-
throw new PathException($path);
55-
}
50+
$this->doLoad(false, $path, ...$paths);
51+
}
5652

57-
$this->populate($this->parse(file_get_contents($path), $path));
58-
}
53+
/**
54+
* Loads one or several .env files and enables override existing vars.
55+
*
56+
* @param string $path A file to load
57+
* @param ...string $paths A list of additional files to load
58+
*
59+
* @throws FormatException when a file has a syntax error
60+
* @throws PathException when a file does not exist or is not readable
61+
*/
62+
public function overload(string $path, string ...$paths): void
63+
{
64+
$this->doLoad(true, $path, ...$paths);
5965
}
6066

6167
/**
6268
* Sets values as environment variables (via putenv, $_ENV, and $_SERVER).
6369
*
64-
* Note that existing environment variables are not overridden.
65-
*
66-
* @param array $values An array of env variables
70+
* @param array $values An array of env variables
71+
* @param bool $overrideExistingVars true when existing environment variables must be overridden
6772
*/
68-
public function populate(array $values): void
73+
public function populate(array $values, bool $overrideExistingVars = false): void
6974
{
7075
$loadedVars = array_flip(explode(',', getenv('SYMFONY_DOTENV_VARS')));
7176
unset($loadedVars['']);
7277

7378
foreach ($values as $name => $value) {
7479
$notHttpName = 0 !== strpos($name, 'HTTP_');
7580
// don't check existence with getenv() because of thread safety issues
76-
if (!isset($loadedVars[$name]) && (isset($_ENV[$name]) || (isset($_SERVER[$name]) && $notHttpName))) {
81+
if (!isset($loadedVars[$name]) && (!$overrideExistingVars && (isset($_ENV[$name]) || (isset($_SERVER[$name]) && $notHttpName)))) {
7782
continue;
7883
}
7984

@@ -399,4 +404,17 @@ private function createFormatException($message)
399404
{
400405
return new FormatException($message, new FormatExceptionContext($this->data, $this->path, $this->lineno, $this->cursor));
401406
}
407+
408+
private function doLoad(bool $overrideExistingVars, string $path, string ...$paths): void
409+
{
410+
array_unshift($paths, $path);
411+
412+
foreach ($paths as $path) {
413+
if (!is_readable($path) || is_dir($path)) {
414+
throw new PathException($path);
415+
}
416+
417+
$this->populate($this->parse(file_get_contents($path), $path), $overrideExistingVars);
418+
}
419+
}
402420
}

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

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

189+
public function testOverload()
190+
{
191+
unset($_ENV['FOO']);
192+
unset($_ENV['BAR']);
193+
unset($_SERVER['FOO']);
194+
unset($_SERVER['BAR']);
195+
196+
putenv('FOO=initial_foo_value');
197+
putenv('BAR=initial_bar_value');
198+
$_ENV['FOO'] = 'initial_foo_value';
199+
$_ENV['BAR'] = 'initial_bar_value';
200+
201+
@mkdir($tmpdir = sys_get_temp_dir().'/dotenv');
202+
203+
$path1 = tempnam($tmpdir, 'sf-');
204+
$path2 = tempnam($tmpdir, 'sf-');
205+
206+
file_put_contents($path1, 'FOO=BAR');
207+
file_put_contents($path2, 'BAR=BAZ');
208+
209+
(new DotEnv())->overload($path1, $path2);
210+
211+
$foo = getenv('FOO');
212+
$bar = getenv('BAR');
213+
214+
putenv('FOO');
215+
putenv('BAR');
216+
unlink($path1);
217+
unlink($path2);
218+
rmdir($tmpdir);
219+
220+
$this->assertSame('BAR', $foo);
221+
$this->assertSame('BAZ', $bar);
222+
}
223+
189224
/**
190225
* @expectedException \Symfony\Component\Dotenv\Exception\PathException
191226
*/
@@ -228,6 +263,18 @@ public function testHttpVarIsPartiallyOverriden()
228263
$this->assertSame('http_value', $_SERVER['HTTP_TEST_ENV_VAR']);
229264
}
230265

266+
public function testEnvVarIsOverriden()
267+
{
268+
putenv('TEST_ENV_VAR_OVERRIDEN=original_value');
269+
270+
$dotenv = new DotEnv();
271+
$dotenv->populate(array('TEST_ENV_VAR_OVERRIDEN' => 'new_value'), true);
272+
273+
$this->assertSame('new_value', getenv('TEST_ENV_VAR_OVERRIDEN'));
274+
$this->assertSame('new_value', $_ENV['TEST_ENV_VAR_OVERRIDEN']);
275+
$this->assertSame('new_value', $_SERVER['TEST_ENV_VAR_OVERRIDEN']);
276+
}
277+
231278
public function testMemorizingLoadedVarsNamesInSpecialVar()
232279
{
233280
// Special variable not exists

0 commit comments

Comments
 (0)