Skip to content

Commit e905010

Browse files
allow project specific .env
1 parent 3cbf250 commit e905010

File tree

3 files changed

+63
-42
lines changed

3 files changed

+63
-42
lines changed
Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,70 @@
11
<?php
2+
23
namespace Electro\Configuration\Lib;
34

45
use Electro\Exceptions\Fatal\ConfigException;
56

67
class DotEnv
78
{
8-
/** @var string */
9-
private $envFile;
10-
11-
public function __construct ($file)
12-
{
13-
$this->envFile = $file;
14-
}
15-
16-
public function load ()
17-
{
18-
if (file_exists ($this->envFile)) {
19-
$ini = @parse_ini_file ($this->envFile, false, INI_SCANNER_TYPED);
20-
if ($ini)
21-
$this->loadConfig ($ini);
22-
else {
23-
$e = error_get_last ();
24-
throw new ConfigException(isset($e['message']) ? $e['message'] : "Can't load file $this->envFile");
25-
}
26-
}
27-
}
28-
29-
public function loadConfig (array $config)
30-
{
31-
global $__ENV; // Used by env() to override the value of getenv()
32-
$o = [];
33-
foreach ($config as $k => $v) {
34-
$r = "REDIRECT_$k";
35-
if (isset($_SERVER[$r]))
36-
$e = $_SERVER[$r];
37-
else $e = getenv ($k);
38-
39-
// Environment variables override .env variables
40-
if ($e !== false)
41-
$v = $e;
42-
else $v = trim ($v); // bevause INI_SCANNER_RAW mode keeps irrelevant whitespace on values
43-
44-
$o[$k] = $v;
45-
}
46-
$__ENV = $o;
47-
}
9+
10+
/** @var array */
11+
private $envFiles;
12+
13+
public function __construct(...$files)
14+
{
15+
$this->envFiles = $files;
16+
}
17+
18+
public function load()
19+
{
20+
global $__ENV; // Used by env() to override the value of getenv()
21+
$merged = [];
22+
foreach ($this->envFiles as $file)
23+
{
24+
if (file_exists($file))
25+
{
26+
$ini = @parse_ini_file($file, false, INI_SCANNER_TYPED);
27+
if ($ini)
28+
{
29+
$partial = $this->buildConfig($ini);
30+
$merged = array_merge($merged,$partial);
31+
32+
}
33+
else
34+
{
35+
$e = error_get_last();
36+
throw new ConfigException(isset($e['message']) ? $e['message'] : "Can't load file $file");
37+
}
38+
}
39+
}
40+
$__ENV = $merged;
41+
}
42+
43+
public function loadConfig(array $config)
44+
{
45+
global $__ENV; // Used by env() to override the value of getenv()
46+
$__ENV = $this->buildConfig($config);
47+
}
48+
private function buildConfig(array $config)
49+
{
50+
$o = [];
51+
foreach ($config as $k => $v)
52+
{
53+
$r = "REDIRECT_$k";
54+
if (isset($_SERVER[$r]))
55+
$e = $_SERVER[$r];
56+
else
57+
$e = getenv($k);
58+
59+
// Environment variables override .env variables
60+
if ($e !== false)
61+
$v = $e;
62+
else
63+
$v = trim($v); // bevause INI_SCANNER_RAW mode keeps irrelevant whitespace on values
64+
65+
$o[$k] = $v;
66+
}
67+
return $o;
68+
}
4869

4970
}

subsystems/console/ConsoleApplication/ConsoleBootloader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function boot ($rootDir, $urlDepth = 0, callable $onStartUp = null)
5454

5555
// Initialize some settings from environment variables
5656

57-
$dotenv = new Dotenv ("$rootDir/.env");
57+
$dotenv = new Dotenv ("$rootDir/project.env","$rootDir/.env");
5858
try {
5959
$dotenv->load ();
6060
}

subsystems/web-server/WebServer/WebBootloader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function boot ($rootDir, $urlDepth = 0, callable $onStartUp = null)
4444

4545
// Initialize some settings from environment variables
4646

47-
$dotenv = new Dotenv ("$rootDir/.env");
47+
$dotenv = new Dotenv ("$rootDir/project.env","$rootDir/.env");
4848
try {
4949
$dotenv->load ();
5050
}

0 commit comments

Comments
 (0)