-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3cbf250
commit e905010
Showing
3 changed files
with
63 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,70 @@ | ||
<?php | ||
|
||
namespace Electro\Configuration\Lib; | ||
|
||
use Electro\Exceptions\Fatal\ConfigException; | ||
|
||
class DotEnv | ||
{ | ||
/** @var string */ | ||
private $envFile; | ||
|
||
public function __construct ($file) | ||
{ | ||
$this->envFile = $file; | ||
} | ||
|
||
public function load () | ||
{ | ||
if (file_exists ($this->envFile)) { | ||
$ini = @parse_ini_file ($this->envFile, false, INI_SCANNER_TYPED); | ||
if ($ini) | ||
$this->loadConfig ($ini); | ||
else { | ||
$e = error_get_last (); | ||
throw new ConfigException(isset($e['message']) ? $e['message'] : "Can't load file $this->envFile"); | ||
} | ||
} | ||
} | ||
|
||
public function loadConfig (array $config) | ||
{ | ||
global $__ENV; // Used by env() to override the value of getenv() | ||
$o = []; | ||
foreach ($config as $k => $v) { | ||
$r = "REDIRECT_$k"; | ||
if (isset($_SERVER[$r])) | ||
$e = $_SERVER[$r]; | ||
else $e = getenv ($k); | ||
|
||
// Environment variables override .env variables | ||
if ($e !== false) | ||
$v = $e; | ||
else $v = trim ($v); // bevause INI_SCANNER_RAW mode keeps irrelevant whitespace on values | ||
|
||
$o[$k] = $v; | ||
} | ||
$__ENV = $o; | ||
} | ||
|
||
/** @var array */ | ||
private $envFiles; | ||
|
||
public function __construct(...$files) | ||
{ | ||
$this->envFiles = $files; | ||
} | ||
|
||
public function load() | ||
{ | ||
global $__ENV; // Used by env() to override the value of getenv() | ||
$merged = []; | ||
foreach ($this->envFiles as $file) | ||
{ | ||
if (file_exists($file)) | ||
{ | ||
$ini = @parse_ini_file($file, false, INI_SCANNER_TYPED); | ||
if ($ini) | ||
{ | ||
$partial = $this->buildConfig($ini); | ||
$merged = array_merge($merged,$partial); | ||
|
||
} | ||
else | ||
{ | ||
$e = error_get_last(); | ||
throw new ConfigException(isset($e['message']) ? $e['message'] : "Can't load file $file"); | ||
} | ||
} | ||
} | ||
$__ENV = $merged; | ||
} | ||
|
||
public function loadConfig(array $config) | ||
{ | ||
global $__ENV; // Used by env() to override the value of getenv() | ||
$__ENV = $this->buildConfig($config); | ||
} | ||
private function buildConfig(array $config) | ||
{ | ||
$o = []; | ||
foreach ($config as $k => $v) | ||
{ | ||
$r = "REDIRECT_$k"; | ||
if (isset($_SERVER[$r])) | ||
$e = $_SERVER[$r]; | ||
else | ||
$e = getenv($k); | ||
|
||
// Environment variables override .env variables | ||
if ($e !== false) | ||
$v = $e; | ||
else | ||
$v = trim($v); // bevause INI_SCANNER_RAW mode keeps irrelevant whitespace on values | ||
|
||
$o[$k] = $v; | ||
} | ||
return $o; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters