add more ways to get environement variables to figure out configuration file to use
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Bsr\Utils\Configuration;
|
namespace Bsr\Utils\Configuration;
|
||||||
|
|
||||||
|
use Bsr\Utils\WhichEnv\WhichEnv;
|
||||||
|
|
||||||
class Configuration {
|
class Configuration {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -71,8 +73,17 @@ class Configuration {
|
|||||||
public static function getConfigFilePath()
|
public static function getConfigFilePath()
|
||||||
{
|
{
|
||||||
if (null === self::$customConfigFilePath) {
|
if (null === self::$customConfigFilePath) {
|
||||||
$envfile = getenv('APPLICATION_ENV') === 'development' ? 'local' : 'global';
|
$envSpecificConfig = 'local';
|
||||||
self::$customConfigFilePath = realpath(dirname(__FILE__) . "/../../../../../../config/configuration.$envfile.php");
|
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;
|
return self::$customConfigFilePath;
|
||||||
}
|
}
|
||||||
|
|||||||
25
src/Utils/WhichEnv/WhichEnv.php
Normal file
25
src/Utils/WhichEnv/WhichEnv.php
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
19
src/Utils/WhichEnv/WhichEnvRuleInterface.php
Normal file
19
src/Utils/WhichEnv/WhichEnvRuleInterface.php
Normal file
@@ -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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user