@@ -48,34 +48,41 @@ final class Dotenv
48
48
*/
49
49
public function load (string $ path , string ...$ extraPaths ): void
50
50
{
51
- $ this ->doLoad (false , false , \func_get_args ());
51
+ $ this ->doLoad (false , \func_get_args ());
52
52
}
53
53
54
54
/**
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.
56
56
*
57
57
* .env.local is always ignored in test env because tests should produce the same results for everyone.
58
58
*
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
61
63
*
62
64
* @throws FormatException when a file has a syntax error
63
65
* @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
66
66
*/
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
68
68
{
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
+ }
77
74
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 );
79
86
}
80
87
}
81
88
@@ -90,7 +97,7 @@ public function loadForEnv(string $env, string $path, string ...$extraPaths): vo
90
97
*/
91
98
public function overload (string $ path , string ...$ extraPaths ): void
92
99
{
E294
93
- $ this ->doLoad (true , false , \func_get_args ());
100
+ $ this ->doLoad (true , \func_get_args ());
94
101
}
95
102
96
103
/**
@@ -434,14 +441,14 @@ private function createFormatException($message)
434
441
return new FormatException ($ message , new FormatExceptionContext ($ this ->data , $ this ->path , $ this ->lineno , $ this ->cursor ));
435
442
}
436
443
437
- private function doLoad (bool $ overrideExistingVars , bool $ ignoreMissingExtraPaths , array $ paths ): void
444
+ private function doLoad (bool $ overrideExistingVars , array $ paths ): void
438
445
{
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 ) {
446
+ foreach ($ paths as $ path ) {
447
+ if (!is_readable ($ path ) || is_dir ($ path )) {
443
448
throw new PathException ($ path );
444
449
}
450
+
451
+ $ this ->populate ($ this ->parse (file_get_contents ($ path ), $ path ), $ overrideExistingVars );
445
452
}
446
453
}
447
454
}
0 commit comments