Introduce Config\Cache Source indicators (File, DB, Server Env)
This commit is contained in:
parent
50a0927e7e
commit
0f3e4255ca
6 changed files with 56 additions and 45 deletions
|
@ -30,11 +30,26 @@ use ParagonIE\HiddenString\HiddenString;
|
||||||
*/
|
*/
|
||||||
class Cache
|
class Cache
|
||||||
{
|
{
|
||||||
|
/** @var int Indicates that the cache entry is set by file - Low Priority */
|
||||||
|
const SOURCE_FILE = 0;
|
||||||
|
/** @var int Indicates that the cache entry is set by the DB config table - Middle Priority */
|
||||||
|
const SOURCE_DB = 1;
|
||||||
|
/** @var int Indicates that the cache entry is set by a server environment variable - High Priority */
|
||||||
|
const SOURCE_ENV = 3;
|
||||||
|
|
||||||
|
/** @var int Default value for a config source */
|
||||||
|
const SOURCE_DEFAULT = self::SOURCE_FILE;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
private $config;
|
private $config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int[][]
|
||||||
|
*/
|
||||||
|
private $source = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
|
@ -43,11 +58,12 @@ class Cache
|
||||||
/**
|
/**
|
||||||
* @param array $config A initial config array
|
* @param array $config A initial config array
|
||||||
* @param bool $hidePasswordOutput True, if cache variables should take extra care of password values
|
* @param bool $hidePasswordOutput True, if cache variables should take extra care of password values
|
||||||
|
* @param int $source Sets a source of the initial config values
|
||||||
*/
|
*/
|
||||||
public function __construct(array $config = [], bool $hidePasswordOutput = true)
|
public function __construct(array $config = [], bool $hidePasswordOutput = true, $source = self::SOURCE_DEFAULT)
|
||||||
{
|
{
|
||||||
$this->hidePasswordOutput = $hidePasswordOutput;
|
$this->hidePasswordOutput = $hidePasswordOutput;
|
||||||
$this->load($config);
|
$this->load($config, $source);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -55,9 +71,9 @@ class Cache
|
||||||
* Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
|
* Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
|
||||||
*
|
*
|
||||||
* @param array $config
|
* @param array $config
|
||||||
* @param bool $overwrite Force value overwrite if the config key already exists
|
* @param int $source Indicates the source of the config entry
|
||||||
*/
|
*/
|
||||||
public function load(array $config, bool $overwrite = false)
|
public function load(array $config, int $source = self::SOURCE_DEFAULT)
|
||||||
{
|
{
|
||||||
$categories = array_keys($config);
|
$categories = array_keys($config);
|
||||||
|
|
||||||
|
@ -68,11 +84,7 @@ class Cache
|
||||||
foreach ($keys as $key) {
|
foreach ($keys as $key) {
|
||||||
$value = $config[$category][$key];
|
$value = $config[$category][$key];
|
||||||
if (isset($value)) {
|
if (isset($value)) {
|
||||||
if ($overwrite) {
|
$this->set($category, $key, $value, $source);
|
||||||
$this->set($category, $key, $value);
|
|
||||||
} else {
|
|
||||||
$this->setDefault($category, $key, $value);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -91,49 +103,45 @@ class Cache
|
||||||
{
|
{
|
||||||
if (isset($this->config[$cat][$key])) {
|
if (isset($this->config[$cat][$key])) {
|
||||||
return $this->config[$cat][$key];
|
return $this->config[$cat][$key];
|
||||||
} elseif (!isset($key) && isset($this->config[$cat])) {
|
} else if (!isset($key) && isset($this->config[$cat])) {
|
||||||
return $this->config[$cat];
|
return $this->config[$cat];
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets a default value in the config cache. Ignores already existing keys.
|
|
||||||
*
|
|
||||||
* @param string $cat Config category
|
|
||||||
* @param string $key Config key
|
|
||||||
* @param mixed $value Default value to set
|
|
||||||
*/
|
|
||||||
private function setDefault(string $cat, string $key, $value)
|
|
||||||
{
|
|
||||||
if (!isset($this->config[$cat][$key])) {
|
|
||||||
$this->set($cat, $key, $value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets a value in the config cache. Accepts raw output from the config table
|
* Sets a value in the config cache. Accepts raw output from the config table
|
||||||
*
|
*
|
||||||
* @param string $cat Config category
|
* @param string $cat Config category
|
||||||
* @param string $key Config key
|
* @param string $key Config key
|
||||||
* @param mixed $value Value to set
|
* @param mixed $value Value to set
|
||||||
|
* @param int $source The source of the current config key
|
||||||
*
|
*
|
||||||
* @return bool True, if the value is set
|
* @return bool True, if the value is set
|
||||||
*/
|
*/
|
||||||
public function set(string $cat, string $key, $value)
|
public function set(string $cat, string $key, $value, $source = self::SOURCE_DEFAULT)
|
||||||
{
|
{
|
||||||
if (!isset($this->config[$cat])) {
|
if (!isset($this->config[$cat])) {
|
||||||
$this->config[$cat] = [];
|
$this->config[$cat] = [];
|
||||||
|
$this->source[$cat] = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($this->source[$cat][$key]) &&
|
||||||
|
$source < $this->source[$cat][$key]) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->hidePasswordOutput &&
|
if ($this->hidePasswordOutput &&
|
||||||
$key == 'password' &&
|
$key == 'password' &&
|
||||||
is_string($value)) {
|
is_string($value)) {
|
||||||
$this->config[$cat][$key] = new HiddenString((string)$value);
|
$this->config[$cat][$key] = new HiddenString((string)$value);
|
||||||
} else {
|
} else {
|
||||||
$this->config[$cat][$key] = $value;
|
$this->config[$cat][$key] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->source[$cat][$key] = $source;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,8 +157,10 @@ class Cache
|
||||||
{
|
{
|
||||||
if (isset($this->config[$cat][$key])) {
|
if (isset($this->config[$cat][$key])) {
|
||||||
unset($this->config[$cat][$key]);
|
unset($this->config[$cat][$key]);
|
||||||
|
unset($this->source[$cat][$key]);
|
||||||
if (count($this->config[$cat]) == 0) {
|
if (count($this->config[$cat]) == 0) {
|
||||||
unset($this->config[$cat]);
|
unset($this->config[$cat]);
|
||||||
|
unset($this->source[$cat]);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -70,7 +70,7 @@ class JitConfig extends BaseConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
// load the whole category out of the DB into the cache
|
// load the whole category out of the DB into the cache
|
||||||
$this->configCache->load($config, true);
|
$this->configCache->load($config, Cache::SOURCE_DB);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -69,7 +69,7 @@ class PreloadConfig extends BaseConfig
|
||||||
$this->config_loaded = true;
|
$this->config_loaded = true;
|
||||||
|
|
||||||
// load the whole category out of the DB into the cache
|
// load the whole category out of the DB into the cache
|
||||||
$this->configCache->load($config, true);
|
$this->configCache->load($config, Cache::SOURCE_DB);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -104,12 +104,12 @@ class ConfigFileLoader
|
||||||
public function setupCache(Cache $config, $raw = false)
|
public function setupCache(Cache $config, $raw = false)
|
||||||
{
|
{
|
||||||
// Load static config files first, the order is important
|
// Load static config files first, the order is important
|
||||||
$config->load($this->loadStaticConfig('defaults'));
|
$config->load($this->loadStaticConfig('defaults'), Cache::SOURCE_FILE);
|
||||||
$config->load($this->loadStaticConfig('settings'));
|
$config->load($this->loadStaticConfig('settings'), Cache::SOURCE_FILE);
|
||||||
|
|
||||||
// try to load the legacy config first
|
// try to load the legacy config first
|
||||||
$config->load($this->loadLegacyConfig('htpreconfig'), true);
|
$config->load($this->loadLegacyConfig('htpreconfig'), Cache::SOURCE_FILE);
|
||||||
$config->load($this->loadLegacyConfig('htconfig'), true);
|
$config->load($this->loadLegacyConfig('htconfig'), Cache::SOURCE_FILE);
|
||||||
|
|
||||||
// Now load every other config you find inside the 'config/' directory
|
// Now load every other config you find inside the 'config/' directory
|
||||||
$this->loadCoreConfig($config);
|
$this->loadCoreConfig($config);
|
||||||
|
@ -157,12 +157,12 @@ class ConfigFileLoader
|
||||||
{
|
{
|
||||||
// try to load legacy ini-files first
|
// try to load legacy ini-files first
|
||||||
foreach ($this->getConfigFiles(true) as $configFile) {
|
foreach ($this->getConfigFiles(true) as $configFile) {
|
||||||
$config->load($this->loadINIConfigFile($configFile), true);
|
$config->load($this->loadINIConfigFile($configFile), Cache::SOURCE_FILE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// try to load supported config at last to overwrite it
|
// try to load supported config at last to overwrite it
|
||||||
foreach ($this->getConfigFiles() as $configFile) {
|
foreach ($this->getConfigFiles() as $configFile) {
|
||||||
$config->load($this->loadConfigFile($configFile), true);
|
$config->load($this->loadConfigFile($configFile), Cache::SOURCE_FILE);
|
||||||
}
|
}
|
||||||
|
|
||||||
return [];
|
return [];
|
||||||
|
|
|
@ -83,13 +83,14 @@ class CacheTest extends MockedTest
|
||||||
];
|
];
|
||||||
|
|
||||||
$configCache = new Cache();
|
$configCache = new Cache();
|
||||||
$configCache->load($data);
|
$configCache->load($data, Cache::SOURCE_DB);
|
||||||
$configCache->load($override);
|
// doesn't override - Low Priority due Config file
|
||||||
|
$configCache->load($override, Cache::SOURCE_FILE);
|
||||||
|
|
||||||
$this->assertConfigValues($data, $configCache);
|
$this->assertConfigValues($data, $configCache);
|
||||||
|
|
||||||
// override the value
|
// override the value - High Prio due Server Env
|
||||||
$configCache->load($override, true);
|
$configCache->load($override, Cache::SOURCE_ENV);
|
||||||
|
|
||||||
$this->assertEquals($override['system']['test'], $configCache->get('system', 'test'));
|
$this->assertEquals($override['system']['test'], $configCache->get('system', 'test'));
|
||||||
$this->assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
|
$this->assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
|
||||||
|
|
|
@ -350,7 +350,7 @@ abstract class ConfigTest extends MockedTest
|
||||||
*/
|
*/
|
||||||
public function testGetWithRefresh($data)
|
public function testGetWithRefresh($data)
|
||||||
{
|
{
|
||||||
$this->configCache->load(['test' => ['it' => 'now']]);
|
$this->configCache->load(['test' => ['it' => 'now']], Cache::SOURCE_FILE);
|
||||||
|
|
||||||
$this->testedConfig = $this->getInstance();
|
$this->testedConfig = $this->getInstance();
|
||||||
$this->assertInstanceOf(Cache::class, $this->testedConfig->getCache());
|
$this->assertInstanceOf(Cache::class, $this->testedConfig->getCache());
|
||||||
|
@ -375,7 +375,7 @@ abstract class ConfigTest extends MockedTest
|
||||||
*/
|
*/
|
||||||
public function testDeleteWithoutDB($data)
|
public function testDeleteWithoutDB($data)
|
||||||
{
|
{
|
||||||
$this->configCache->load(['test' => ['it' => $data]]);
|
$this->configCache->load(['test' => ['it' => $data]], Cache::SOURCE_FILE);
|
||||||
|
|
||||||
$this->testedConfig = $this->getInstance();
|
$this->testedConfig = $this->getInstance();
|
||||||
$this->assertInstanceOf(Cache::class, $this->testedConfig->getCache());
|
$this->assertInstanceOf(Cache::class, $this->testedConfig->getCache());
|
||||||
|
@ -395,7 +395,7 @@ abstract class ConfigTest extends MockedTest
|
||||||
*/
|
*/
|
||||||
public function testDeleteWithDB()
|
public function testDeleteWithDB()
|
||||||
{
|
{
|
||||||
$this->configCache->load(['test' => ['it' => 'now', 'quarter' => 'true']]);
|
$this->configCache->load(['test' => ['it' => 'now', 'quarter' => 'true']], Cache::SOURCE_FILE);
|
||||||
|
|
||||||
$this->configModel->shouldReceive('delete')
|
$this->configModel->shouldReceive('delete')
|
||||||
->with('test', 'it')
|
->with('test', 'it')
|
||||||
|
|
Loading…
Reference in a new issue