@@ -48,34 +48,41 @@ final class Dotenv
4848 */
4949public function load (string $ path ,string ...$ extraPaths ):void
5050 {
51- $ this ->doLoad (false ,false , \func_get_args ());
51+ $ this ->doLoad (false ,\func_get_args ());
5252 }
5353
5454/**
55- * Loadsone or several .env and the corresponding .env.$env , .env.local and .env.$env.local files if they exist.
55+ * Loadsa .envfile and the corresponding .env.local , .env.$env and .env.$env.local files if they exist.
5656 *
5757 * .env.local is always ignored in test env because tests should produce the same results for everyone.
5858 *
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
6163 *
6264 * @throws FormatException when a file has a syntax error
6365 * @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
6666 */
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
6868 {
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+ }
7774
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 );
7986 }
8087 }
8188
@@ -90,7 +97,7 @@ public function loadForEnv(string $env, string $path, string ...$extraPaths): vo
9097 */
9198public function overload (string $ path ,string ...$ extraPaths ):void
9299 {
93- $ this ->doLoad (true ,false , \func_get_args ());
100+ $ this ->doLoad (true ,\func_get_args ());
94101 }
95102
96103/**
@@ -434,14 +441,14 @@ private function createFormatException($message)
434441return new FormatException ($ message ,new FormatExceptionContext ($ this ->data ,$ this ->path ,$ this ->lineno ,$ this ->cursor ));
435442 }
436443
437- private function doLoad (bool $ overrideExistingVars ,bool $ ignoreMissingExtraPaths , array $ paths ):void
444+ private function doLoad (bool $ overrideExistingVars ,array $ paths ):void
438445 {
439- foreach ($ pathsas $ 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 ($ pathsas $ path ) {
447+ if (!is_readable ($ path ) ||is_dir ($ path )) {
443448throw new PathException ($ path );
444449 }
450+
451+ $ this ->populate ($ this ->parse (file_get_contents ($ path ),$ path ),$ overrideExistingVars );
445452 }
446453 }
447454}