Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0cf9acb

Browse files
[Dotenv] add loadEnv(), a smoother alternative to loadForEnv()
1 parent8d277ce commit0cf9acb

File tree

2 files changed

+51
-48
lines changed

2 files changed

+51
-48
lines changed

‎src/Symfony/Component/Dotenv/Dotenv.php‎

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,34 +48,41 @@ final class Dotenv
4848
*/
4949
publicfunctionload(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.envfileand 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-
publicfunctionloadForEnv(string$env,string$path,string...$extraPaths):void
67+
publicfunctionloadEnv(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
*/
9198
publicfunctionoverload(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)
434441
returnnewFormatException($message,newFormatExceptionContext($this->data,$this->path,$this->lineno,$this->cursor));
435442
}
436443

437-
privatefunctiondoLoad(bool$overrideExistingVars,bool$ignoreMissingExtraPaths,array$paths):void
444+
privatefunctiondoLoad(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)) {
443448
thrownewPathException($path);
444449
}
450+
451+
$this->populate($this->parse(file_get_contents($path),$path),$overrideExistingVars);
445452
}
446453
}
447454
}

‎src/Symfony/Component/Dotenv/Tests/DotenvTest.php‎

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public function testLoad()
186186
$this->assertSame('BAZ',$bar);
187187
}
188188

189-
publicfunctiontestLoadForEnv()
189+
publicfunctiontestLoadEnv()
190190
{
191191
unset($_ENV['FOO']);
192192
unset($_ENV['BAR']);
@@ -197,50 +197,46 @@ public function testLoadForEnv()
197197

198198
@mkdir($tmpdir =sys_get_temp_dir().'/dotenv');
199199

200-
$path1 =tempnam($tmpdir,'sf-');
201-
$path2 =tempnam($tmpdir,'sf-');
202-
203-
file_put_contents($path1,'FOO=BAR');
204-
file_put_contents($path2,'BAR=BAZ');
200+
$path =tempnam($tmpdir,'sf-');
205201

206202
// .env
207203

208-
(newDotEnv())->loadForEnv('dev',$path1,$path2);
209-
204+
file_put_contents($path,'FOO=BAR');
205+
(newDotEnv())->loadEnv($path,'TEST_APP_ENV');
210206
$this->assertSame('BAR',getenv('FOO'));
211-
$this->assertSame('BAZ',getenv('BAR'));
212-
213-
// .env.dev
214-
215-
file_put_contents("$path1.dev",'FOO=devBAR');
216-
(newDotEnv())->loadForEnv('dev',$path1,$path2);
217-
$this->assertSame('devBAR',getenv('FOO'));
207+
$this->assertSame('dev',getenv('TEST_APP_ENV'));
218208

219209
// .env.local
220210

221-
file_put_contents("$path1.local",'FOO=localBAR');
222-
(newDotEnv())->loadForEnv('dev',$path1,$path2);
211+
file_put_contents("$path.local",'FOO=localBAR');
212+
(newDotEnv())->loadEnv($path,'TEST_APP_ENV');
223213
$this->assertSame('localBAR',getenv('FOO'));
224214

225215
// special case for test
226216

227-
file_put_contents("$path1.local",'FOO=testBAR');
228-
(newDotEnv())->loadForEnv('test',$path1,$path2);
217+
$_SERVER['TEST_APP_ENV'] ='test';
218+
(newDotEnv())->loadEnv($path,'TEST_APP_ENV');
229219
$this->assertSame('BAR',getenv('FOO'));
230220

221+
// .env.dev
222+
223+
unset($_SERVER['TEST_APP_ENV']);
224+
file_put_contents("$path.dev",'FOO=devBAR');
225+
(newDotEnv())->loadEnv($path,'TEST_APP_ENV');
226+
$this->assertSame('devBAR',getenv('FOO'));
227+
231228
// .env.dev.local
232229

233-
file_put_contents("$path1.dev.local",'FOO=devlocalBAR');
234-
(newDotEnv())->loadForEnv('dev',$path1,$path2);
230+
file_put_contents("$path.dev.local",'FOO=devlocalBAR');
231+
(newDotEnv())->loadEnv($path,'TEST_APP_ENV');
235232
$this->assertSame('devlocalBAR',getenv('FOO'));
236233

237234
putenv('FOO');
238235
putenv('BAR');
239-
unlink($path1);
240-
unlink("$path1.dev");
241-
unlink("$path1.local");
242-
unlink("$path1.dev.local");
243-
unlink($path2);
236+
unlink($path);
237+
unlink("$path.dev");
238+
unlink("$path.local");
239+
unlink("$path.dev.local");
244240
rmdir($tmpdir);
245241
}
246242

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp