@@ -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
{
93
- $ this ->doLoad (true , false , \func_get_args ());
100
+ $ this ->doLoad (true , \func_get_args ());
94
101
}
95
102
96
103
/**
@@ -101,7 +108,8 @@ public function overload(string $path, string ...$extraPaths): void
101
108
*/
102
109
public function populate (array $ values , bool $ overrideExistingVars = false ): void
103
110
{
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 ' ] ?? '' ));
105
113
unset($ loadedVars ['' ]);
106
114
107
115
foreach ($ values as $ name => $ value ) {
@@ -117,14 +125,14 @@ public function populate(array $values, bool $overrideExistingVars = false): voi
117
125
$ _SERVER [$ name ] = $ value ;
118
126
}
119
127
120
- $ loadedVars [$ name ] = true ;
128
+ if (!isset ($ loadedVars [$ name ])) {
129
+ $ loadedVars [$ name ] = $ updateLoadedVars = true ;
130
+ }
121
131
}
122
132
123
- if ($ loadedVars ) {
133
+ if ($ updateLoadedVars ) {
124
134
$ 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 );
128
136
}
129
137
}
130
138
@@ -434,14 +442,14 @@ private function createFormatException($message)
434
442
return new FormatException ($ message , new FormatExceptionContext ($ this ->data , $ this ->path , $ this ->lineno , $ this ->cursor ));
435
443
}
436
444
437
- private function doLoad (bool $ overrideExistingVars , bool $ ignoreMissingExtraPaths , array $ paths ): void
445
+ private function doLoad (bool $ overrideExistingVars , array $ paths ): void
438
446
{
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 )) {
443
449
throw new PathException ($ path );
444
450
}
451
+
452
+ $ this ->populate ($ this ->parse (file_get_contents ($ path ), $ path ), $ overrideExistingVars );
445
453
}
446
454
}
447
455
}
0 commit comments