Unique configuration/blueprints based on environment name

This commit is contained in:
Andy Miller
2014-10-29 15:09:43 -06:00
parent e81980b6c0
commit 588bd0168c
2 changed files with 36 additions and 21 deletions

View File

@@ -81,13 +81,15 @@ class Config extends Data
protected $pluginLookup;
protected $finder;
protected $environment;
protected $messages = [];
public function __construct(array $items = array(), Grav $grav = null)
public function __construct(array $items = array(), Grav $grav = null, $environment = null)
{
$this->grav = $grav ?: Grav::instance();
$this->finder = new ConfigFinder;
$this->environment = $environment ?: 'localhost';
$this->messages[] = 'Environment Name: ' . $this->environment;
if (isset($items['@class'])) {
if ($items['@class'] != get_class($this)) {
@@ -209,18 +211,10 @@ class Config extends Data
protected function autoDetectEnvironmentConfig($items)
{
$address = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : php_uname('n');
$environment = $this->environment;
$env_stream = 'user://'.$environment.'/config';
// check for localhost variations
if ($address == '::1' || $address == '127.0.0.1') {
$address = 'localhost';
}
$this->messages[] = 'Environment Name: ' . $address;
$env_stream = 'user://'.$address.'/config';
if (file_exists(USER_DIR.$address.'/config')) {
if (file_exists(USER_DIR.$environment.'/config')) {
array_unshift($items['streams']['schemes']['config']['prefixes'][''], $env_stream);
}
@@ -231,8 +225,8 @@ class Config extends Data
{
$checksum = md5(json_encode($blueprints));
$filename = $filename
? CACHE_DIR . 'compiled/blueprints/' . $filename .'.php'
: CACHE_DIR . 'compiled/blueprints/' . $checksum .'.php';
? CACHE_DIR . 'compiled/blueprints/' . $filename . '-' . $this->environment . '.php'
: CACHE_DIR . 'compiled/blueprints/' . $checksum . '-' . $this->environment . '.php';
$file = PhpFile::instance($filename);
$cache = $file->exists() ? $file->content() : null;
$blueprintFiles = $this->finder->locateBlueprintFiles($blueprints, $plugins);
@@ -278,8 +272,8 @@ class Config extends Data
{
$checksum = md5(json_encode($configs));
$filename = $filename
? CACHE_DIR . 'compiled/config/' . $filename .'.php'
: CACHE_DIR . 'compiled/config/' . $checksum .'.php';
? CACHE_DIR . 'compiled/config/' . $filename . '-' . $this->environment . '.php'
: CACHE_DIR . 'compiled/config/' . $checksum . '-' . $this->environment . '.php';
$file = PhpFile::instance($filename);
$cache = $file->exists() ? $file->content() : null;
$configFiles = $this->finder->locateConfigFiles($configs, $plugins);

View File

@@ -16,6 +16,8 @@ use RocketTheme\Toolbox\Blueprints\Blueprints;
*/
class ConfigServiceProvider implements ServiceProviderInterface
{
private $environment;
public function register(Container $container)
{
$self = $this;
@@ -31,11 +33,12 @@ class ConfigServiceProvider implements ServiceProviderInterface
public function loadMasterConfig(Container $container)
{
$file = CACHE_DIR . 'compiled/config/master.php';
$environment = $this->getEnvironment();
$file = CACHE_DIR . 'compiled/config/master-'.$environment.'.php';
$data = is_file($file) ? (array) include $file : [];
if ($data) {
try {
$config = new Config($data, $container);
$config = new Config($data, $container, $environment);
} catch (\Exception $e) {
}
}
@@ -43,7 +46,7 @@ class ConfigServiceProvider implements ServiceProviderInterface
if (!isset($config)) {
$file = GRAV_ROOT . '/setup.php';
$data = is_file($file) ? (array) include $file : [];
$config = new Config($data, $container);
$config = new Config($data, $container, $environment);
}
return $config;
@@ -51,9 +54,27 @@ class ConfigServiceProvider implements ServiceProviderInterface
public function loadMasterBlueprints(Container $container)
{
$file = CACHE_DIR . 'compiled/blueprints/master.php';
$environment = $this->getEnvironment();
$file = CACHE_DIR . 'compiled/blueprints/master-'.$environment.'.php';
$data = is_file($file) ? (array) include $file : [];
return new Blueprints($data, $container);
}
public function getEnvironment()
{
if (!$this->environment) {
$address = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '::1';
// check for localhost variations
if ($address == '::1' || $address == '127.0.0.1') {
$hostname = 'localhost';
} else {
$hostname = gethostname();
}
$this->environment = $hostname;
}
return $this->environment;
}
}