Skip to content

Commit 5c3648f

Browse files
authored
Merge pull request #5 from codemix/4-upgrade-dotenv
Issue #4 Upgrade to Dotenv 2.5.x
2 parents 555a41d + 0fe5670 commit 5c3648f

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"require": {
1313
"yiisoft/yii2": "*",
14-
"vlucas/phpdotenv": "1.0.*"
14+
"vlucas/phpdotenv": "2.5.*"
1515
},
1616
"autoload": {
1717
"psr-4": {

src/Config.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ public static function bootstrap($directory, $vendor = null, $initEnv = true)
231231
public static function initEnv($directory = null)
232232
{
233233
if ($directory !== null && file_exists($directory . DIRECTORY_SEPARATOR . '.env')) {
234-
\Dotenv::load($directory);
234+
$dotenv = new \Dotenv\Dotenv($directory);
235+
$dotenv->load();
235236
}
236237

237238
// Define main Yii environment variables
@@ -260,17 +261,16 @@ public static function initEnv($directory = null)
260261
*/
261262
public static function env($name, $default = null, $required = false)
262263
{
263-
if ($required) {
264-
\Dotenv::required($name);
265-
}
266264
if (array_key_exists($name, $_ENV)) {
267265
return $_ENV[$name];
268266
}
269267
if (array_key_exists($name, $_SERVER)) {
270268
return $_SERVER[$name];
271269
}
272270
$value = getenv($name);
271+
if ($value === false && $required) {
272+
throw new \Exception("Environment variable '$name' is not set");
273+
}
273274
return $value === false ? $default : $value;
274275
}
275-
276276
}

tests/EnvTest.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,21 @@ public function testCanGetEnvVars()
3535
$this->assertEquals('default', Config::env('TEST3', 'default'));
3636
}
3737

38+
public function testCanRequireEnvVar()
39+
{
40+
try {
41+
$x = Config::env('DOESNOTEXIST', null, true);
42+
$this->fail('No exception thrown for missing env variable');
43+
} catch (\Exception $e) {
44+
$this->assertEquals("Environment variable 'DOESNOTEXIST' is not set", $e->getMessage());
45+
}
46+
}
47+
3848
public function testCanGetEnvVarsFromEnvFile()
3949
{
4050
Config::initEnv(__DIR__ . '/app');
4151

42-
$this->assertEquals('', Config::env('YII_DEBUG'));
52+
$this->assertEquals(0, Config::env('YII_DEBUG'));
4353
$this->assertEquals('dev', Config::env('YII_ENV'));
4454
$this->assertEquals('dotenv1', Config::env('VAR1'));
4555
$this->assertEquals(2, Config::env('VAR2'));
@@ -65,7 +75,7 @@ public function testEnvFileDoesNotOverrideEnvVars()
6575
putenv('VAR2=xyz');
6676
Config::initEnv(__DIR__ . '/app');
6777

68-
$this->assertEquals('', Config::env('YII_DEBUG'));
78+
$this->assertEquals(0, Config::env('YII_DEBUG'));
6979
$this->assertEquals('dev', Config::env('YII_ENV'));
7080
$this->assertEquals(654, Config::env('VAR1'));
7181
$this->assertEquals('xyz', Config::env('VAR2'));

0 commit comments

Comments
 (0)