21
21
* Manages .env files.
22
22
*
23
23
* @author Fabien Potencier <[email protected] >
24
+ * @author Kévin Dunglas <[email protected] >
24
25
*/
25
26
final class Dotenv
26
27
{
@@ -39,29 +40,57 @@ final class Dotenv
39
40
/**
40
41
* Loads one or several .env files.
41
42
*
42
- * @param string $path A file to load
43
- * @param ...string $paths A list of additional files to load
43
+ * @param string $path A file to load
44
+ * @param ...string $extraPaths A list of additional files to load
44
45
*
45
46
* @throws FormatException when a file has a syntax error
46
47
* @throws PathException when a file does not exist or is not readable
47
48
*/
48
- public function load (string $ path , string ...$ paths ): void
49
+ public function load (string $ path , string ...$ extraPaths ): void
49
50
{
50
- $ this ->doLoad (false , $ path , $ paths );
51
+ $ this ->doLoad (false , false , \func_get_args ());
52
+ }
53
+
54
+ /**
55
+ * Loads one or several .env and the corresponding env.$env, env.local and env.$env.local files if they exist.
56
+ *
57
+ * .env.local is always ignored in test env because tests should produce the same results for everyone.
58
+ *
59
+ * @param string $path A file to load
60
+ * @param ...string $extraPaths A list of additional files to load
61
+ *
62
+ * @throws FormatException when a file has a syntax error
63
+ * @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
+ */
67
+ public function loadForEnv (string $ env , string $ path , string ...$ extraPaths ): void
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 " ;
77
+
78
+ $ this ->doLoad (false , true , $ pathList );
79
+ }
51
80
}
52
81
53
82
/**
54
83
* Loads one or several .env files and enables override existing vars.
55
84
*
56
- * @param string $path A file to load
57
- * @param ...string $paths A list of additional files to load
85
+ * @param string $path A file to load
86
+ * @param ...string $extraPaths A list of additional files to load
58
87
*
59
88
* @throws FormatException when a file has a syntax error
60
89
* @throws PathException when a file does not exist or is not readable
61
90
*/
62
- public function overload (string $ path , string ...$ paths ): void
91
+ public function overload (string $ path , string ...$ extraPaths ): void
63
92
{
64
- $ this ->doLoad (true , $ path , $ paths );
93
+ $ this ->doLoad (true , false , \func_get_args () );
65
94
}
66
95
67
96
/**
@@ -405,16 +434,14 @@ private function createFormatException($message)
405
434
return new FormatException ($ message , new FormatExceptionContext ($ this ->data , $ this ->path , $ this ->lineno , $ this ->cursor ));
406
435
}
407
436
408
- private function doLoad (bool $ overrideExistingVars , string $ path , array $ paths ): void
437
+ private function doLoad (bool $ overrideExistingVars , bool $ ignoreMissingExtraPaths , array $ paths ): void
409
438
{
410
- array_unshift ($ paths, $ path );
411
-
412
- foreach ( $ paths as $ path ) {
413
- if (! is_readable ( $ path ) || is_dir ( $ path ) ) {
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 ) {
414
443
throw new PathException ($ path );
415
444
}
416
-
417
- $ this ->populate ($ this ->parse (file_get_contents ($ path ), $ path ), $ overrideExistingVars );
418
445
}
419
446
}
420
447
}
0 commit comments