-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Dotenv] add a flag to allow env vars override #26859
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,33 +47,38 @@ final class Dotenv | |
*/ | ||
public function load(string $path, string ...$paths): void | ||
{ | ||
array_unshift($paths, $path); | ||
|
||
foreach ($paths as $path) { | ||
if (!is_readable($path) || is_dir($path)) { | ||
throw new PathException($path); | ||
} | ||
$this->doLoad(false, $path, ...$paths); | ||
} | ||
|
||
$this->populate($this->parse(file_get_contents($path), $path)); | ||
} | ||
/** | ||
* Loads one or several .env files and enables override existing vars. | ||
* | ||
* @param string $path A file to load | ||
* @param ...string $paths A list of additional files to load | ||
* | ||
* @throws FormatException when a file has a syntax error | ||
* @throws PathException when a file does not exist or is not readable | ||
*/ | ||
public function overload(string $path, string ...$paths): void | ||
{ | ||
$this->doLoad(true, $path, ...$paths); | ||
} | ||
|
||
/** | ||
* Sets values as environment variables (via putenv, $_ENV, and $_SERVER). | ||
* | ||
* Note that existing environment variables are not overridden. | ||
* | ||
* @param array $values An array of env variables | ||
* @param array $values An array of env variables | ||
* @param bool $overrideExistingVars true when existing environment variables must be overridden | ||
*/ | ||
public function populate(array $values): void | ||
public function populate(array $values, bool $overrideExistingVars = false): void | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be reverted IMHO: it makes no sense to provide two public interfaces to do the same thing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree but I think the same thing of |
||
{ | ||
$loadedVars = array_flip(explode(',', getenv('SYMFONY_DOTENV_VARS'))); | ||
unset($loadedVars['']); | ||
|
||
foreach ($values as $name => $value) { | ||
$notHttpName = 0 !== strpos($name, 'HTTP_'); | ||
// don't check existence with getenv() because of thread safety issues | ||
if (!isset($loadedVars[$name]) && (isset($_ENV[$name]) || (isset($_SERVER[$name]) && $notHttpName))) { | ||
if (!isset($loadedVars[$name]) && (!$overrideExistingVars && (isset($_ENV[$name]) || (isset($_SERVER[$name]) && $notHttpName)))) { | ||
continue; | ||
} | ||
|
||
|
@@ -399,4 +404,17 @@ private function createFormatException($message) | |
{ | ||
return new FormatException($message, new FormatExceptionContext($this->data, $this->path, $this->lineno, $this->cursor)); | ||
} | ||
|
||
private function doLoad(bool $overrideExistingVars, string $path, string ...$paths): void | ||
{ | ||
array_unshift($paths, $path); | ||
|
||
foreach ($paths as $path) { | ||
if (!is_readable($path) || is_dir($path)) { | ||
throw new PathException($path); | ||
} | ||
|
||
$this->populate($this->parse(file_get_contents($path), $path), $overrideExistingVars); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of creating a PHP array (with the variadic argument) to spread it again (here) to create another PHP array (variadic argument in the private method), it might make sense to pass an array to the private method instead of making it variadic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, we don't need a variadic argument in the private method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stof & @fabpot new PR #28359 created :)