Skip to content

Commit

Permalink
Ability of loading .ENV files added
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitexus committed Oct 15, 2020
1 parent d148015 commit 1b35b3b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -32,3 +32,6 @@ composer.lock
/debian/php-vitexsoftware-ease-core.debhelper.log
/debian/php-vitexsoftware-ease-core.substvars
.phpdoc
/debian/php-vitexsoftware-ease-core-doc/
/debian/php-vitexsoftware-ease-core-doc.debhelper.log
/debian/php-vitexsoftware-ease-core-doc.substvars
19 changes: 18 additions & 1 deletion src/Ease/Shared.php
Expand Up @@ -197,7 +197,24 @@ public function loadConfig($configFile, $defineConstants = false) {
'Config file ' . (realpath($configFile) ? realpath($configFile) : $configFile) . ' does not exist'
);
}
$configuration = json_decode(file_get_contents($configFile), true);

switch (pathinfo($configFile, PATHINFO_EXTENSION)) {
case 'json':
$configuration = json_decode(file_get_contents($configFile), true);
break;
case 'ENV':
foreach (file($configFile) as $cfgRow) {
if (strchr($cfgRow, '=')) {
list($key, $value) = explode('=', trim($cfgRow));
$configuration[$key] = $value;
}
}
break;
default:
throw new Exception('unsupported config type: ' . $configFile);
break;
}

foreach ($configuration as $configKey => $configValue) {
if ($defineConstants && (strtoupper($configKey) == $configKey) && (!defined($configKey))) {
define($configKey, $configValue);
Expand Down
7 changes: 7 additions & 0 deletions tests/.ENV
@@ -0,0 +1,7 @@
KEY=VALUE

FOO=BAR

debug=true


4 changes: 4 additions & 0 deletions tests/src/Ease/SharedTest.php
Expand Up @@ -97,13 +97,17 @@ public function testLogger() {
* @covers Ease\Shared::loadConfig
*/
public function testLoadConfig() {
$this->object->loadConfig('tests/.ENV', true);
$this->assertEquals(['KEY' => 'VALUE','FOO' => 'BAR','debug' => 'true','test'=>true], $this->object->configuration);
$this->object->loadConfig('tests/configtest.json', true);
$this->assertArrayHasKey('opt', $this->object->configuration);
$this->assertTrue(defined('KEY'));
$this->expectException('Ease\Exception');
$this->object->loadConfig('unexistent.json');
$this->assertEquals('optvalue', $this->object->getConfigValue('opt'));
$this->assertEquals('keyvalue', $this->object->getConfigValue('KEY'));
$this->expectException('Exception');
$this->object->loadConfig('tests/Bootstrap.php', true);
}

/**
Expand Down

0 comments on commit 1b35b3b

Please sign in to comment.