add more ways to get environement variables to figure out configuration file to use

master
Guillermo Dev 7 years ago
parent 72701b345e
commit f1e289ea26

@ -1,6 +1,8 @@
<?php
namespace Bsr\Utils\Configuration;
use Bsr\Utils\WhichEnv\WhichEnv;
class Configuration {
/**
@ -71,8 +73,17 @@ class Configuration {
public static function getConfigFilePath()
{
if (null === self::$customConfigFilePath) {
$envfile = getenv('APPLICATION_ENV') === 'development' ? 'local' : 'global';
self::$customConfigFilePath = realpath(dirname(__FILE__) . "/../../../../../../config/configuration.$envfile.php");
$envSpecificConfig = 'local';
if ($env = getenv('APPLICATION_ENV')) {
} else if (isset($_SERVER['APPLICATION_ENV'])) {
$env = $_SERVER['APPLICATION_ENV'];
} else if (WhichEnv::has('APPLICATION_ENV')) {
$env = WhichEnv::get('APPLICATION_ENV');
} else {
throw new \RuntimeException('Could not reliably figure out the APPLICATION_ENV, getenv and $_SERVER do not have any APPLICATION_ENV');
}
$envSpecificConfig = $env === 'development' ? 'local' : 'global';
self::$customConfigFilePath = realpath(dirname(__FILE__) . "/../../../../../../config/configuration.$envSpecificConfig.php");
}
return self::$customConfigFilePath;
}

@ -0,0 +1,25 @@
<?php
namespace Bsr\Utils\WhichEnv;
/**
* Allows scripts to pass a custom Environment name
* based on a cutom rule
*/
class WhichEnv {
private $rule;
static public function registerRule(WhichEnvRuleInterface $rule)
{
self::$rule = $rule;
}
static public function has(string $varName)
{
return self::$rule instanceof WhichEnvRuleInterface && self::$rule->hasEnv($varName);
}
static public function get(string $varName)
{
return self::$rule->getEnv($varName);
}
}

@ -0,0 +1,19 @@
<?php
namespace Bsr\Utils\WhichEnv;
/**
*
*/
interface WhichEnvRuleInterface {
/**
* Must return a cutom rule
* @return string
*/
public function getEnv(string $varName);
/**
* Must return a cutom rule
* @return string
*/
public function hasEnv(string $varName);
}
Loading…
Cancel
Save