Skip to content

Commit

Permalink
allow project specific .env
Browse files Browse the repository at this point in the history
  • Loading branch information
goncalomartins committed May 29, 2017
1 parent 3cbf250 commit e905010
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 42 deletions.
101 changes: 61 additions & 40 deletions subsystems/configuration/Configuration/Lib/DotEnv.php
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function boot ($rootDir, $urlDepth = 0, callable $onStartUp = null)

// Initialize some settings from environment variables

$dotenv = new Dotenv ("$rootDir/.env");
$dotenv = new Dotenv ("$rootDir/project.env","$rootDir/.env");
try {
$dotenv->load ();
}
Expand Down
2 changes: 1 addition & 1 deletion subsystems/web-server/WebServer/WebBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function boot ($rootDir, $urlDepth = 0, callable $onStartUp = null)

// Initialize some settings from environment variables

$dotenv = new Dotenv ("$rootDir/.env");
$dotenv = new Dotenv ("$rootDir/project.env","$rootDir/.env");
try {
$dotenv->load ();
}
Expand Down

0 comments on commit e905010

Please sign in to comment.