-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DotEnv] Add a new loadForEnv() method mimicking Ruby's dotenv behavior #28533
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
* Manages .env files. | ||
* | ||
* @author Fabien Potencier <[email protected]> | ||
* @author Kévin Dunglas <[email protected]> | ||
*/ | ||
final class Dotenv | ||
{ | ||
|
@@ -39,29 +40,57 @@ final class Dotenv | |
/** | ||
* Loads one or several .env files. | ||
* | ||
* @param string $path A file to load | ||
* @param ...string $paths A list of additional files to load | ||
* @param string $path A file to load | ||
* @param ...string $extraPaths 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 load(string $path, string ...$paths): void | ||
public function load(string $path, string ...$extraPaths): void | ||
{ | ||
$this->doLoad(false, $path, $paths); | ||
$this->doLoad(false, false, \func_get_args()); | ||
} | ||
|
||
/** | ||
* Loads one or several .env and the corresponding env.$env, env.local 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. | ||
dunglas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @param string $path A file to load | ||
* @param ...string $extraPaths 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 | ||
* | ||
* @see https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use | ||
*/ | ||
public function loadForEnv(string $env, string $path, string ...$extraPaths): void | ||
{ | ||
$paths = \func_get_args(); | ||
for ($i = 1; $i < \func_num_args(); ++$i) { | ||
$path = $paths[$i]; | ||
$pathList = array($path, "$path.$env"); | ||
if ('test' !== $env) { | ||
nicolas-grekas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$pathList[] = "$path.local"; | ||
} | ||
$pathList[] = "$path.$env.local"; | ||
|
||
$this->doLoad(false, true, $pathList); | ||
} | ||
} | ||
|
||
/** | ||
* 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 | ||
* @param string $path A file to load | ||
* @param ...string $extraPaths 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 | ||
public function overload(string $path, string ...$extraPaths): void | ||
{ | ||
$this->doLoad(true, $path, $paths); | ||
$this->doLoad(true, false, \func_get_args()); | ||
} | ||
|
||
/** | ||
|
@@ -405,16 +434,14 @@ 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, array $paths): void | ||
private function doLoad(bool $overrideExistingVars, bool $ignoreMissingExtraPaths, array $paths): void | ||
{ | ||
array_unshift($paths, $path); | ||
|
||
foreach ($paths as $path) { | ||
if (!is_readable($path) || is_dir($path)) { | ||
foreach ($paths as $i => $path) { | ||
if (is_readable($path) && !is_dir($path)) { | ||
$this->populate($this->parse(file_get_contents($path), $path), $overrideExistingVars); | ||
} elseif (!$ignoreMissingExtraPaths || 0 === $i) { | ||
throw new PathException($path); | ||
} | ||
|
||
$this->populate($this->parse(file_get_contents($path), $path), $overrideExistingVars); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
this seems to be missing the dots in front of the files like .env.$env