Skip to content

Commit

Permalink
added USE_ENV_ARRAY constant #3
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Jul 17, 2017
1 parent ce405ce commit 93e933e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Env.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Env
const CONVERT_NULL = 2;
const CONVERT_INT = 4;
const STRIP_QUOTES = 8;
const USE_ENV_ARRAY = 16;

public static $options = 15; //All flags enabled
public static $default = null; //Default value if not exists
Expand Down Expand Up @@ -34,7 +35,11 @@ public static function init()
*/
public static function get($name)
{
$value = getenv($name);
if (self::$options & self::USE_ENV_ARRAY) {
$value = isset($_ENV[$name]) ? $_ENV[$name] : false;
} else {
$value = getenv($name);
}

if ($value === false) {
return self::$default;
Expand Down
14 changes: 14 additions & 0 deletions tests/ConversionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,19 @@ public function testEnv()
$this->assertSame(123, env('FOO'));

$this->assertFalse(Env::init());

//Switch to $_ENV
Env::$options |= Env::USE_ENV_ARRAY;

$this->assertNull(env('FOO'));

$_ENV['FOO'] = 456;

$this->assertSame(456, env('FOO'));

//Switch to getenv again
Env::$options ^= Env::USE_ENV_ARRAY;

$this->assertSame(123, env('FOO'));
}
}

0 comments on commit 93e933e

Please sign in to comment.