From 588bd0168c4e6655b21be4ac44844e15e10f4835 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Wed, 29 Oct 2014 15:09:43 -0600 Subject: [PATCH] Unique configuration/blueprints based on environment name --- system/src/Grav/Common/Config/Config.php | 28 +++++++----------- .../Common/Service/ConfigServiceProvider.php | 29 ++++++++++++++++--- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/system/src/Grav/Common/Config/Config.php b/system/src/Grav/Common/Config/Config.php index 9e6dc1b93..0cf369dd0 100644 --- a/system/src/Grav/Common/Config/Config.php +++ b/system/src/Grav/Common/Config/Config.php @@ -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); diff --git a/system/src/Grav/Common/Service/ConfigServiceProvider.php b/system/src/Grav/Common/Service/ConfigServiceProvider.php index 978356c22..a2885876f 100644 --- a/system/src/Grav/Common/Service/ConfigServiceProvider.php +++ b/system/src/Grav/Common/Service/ConfigServiceProvider.php @@ -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; + } }