X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FCore%2FConfig%2FPreloadConfigAdapter.php;h=96331e7a2cd6dd978b1e69cd6dfd078edaf21628;hb=4facd1dfdba93ede48ca40b5e146424e6701118b;hp=f87b47f16ca619b3d9b95b57d8f10003d154bb8d;hpb=27d94023eef0263a3ce9750f79a73ac941a25304;p=friendica.git diff --git a/src/Core/Config/PreloadConfigAdapter.php b/src/Core/Config/PreloadConfigAdapter.php index f87b47f16c..96331e7a2c 100644 --- a/src/Core/Config/PreloadConfigAdapter.php +++ b/src/Core/Config/PreloadConfigAdapter.php @@ -1,90 +1,123 @@ - - */ -class PreloadConfigAdapter extends BaseObject implements IConfigAdapter -{ - private $config_loaded = false; - - public function __construct() - { - $this->load(); - } - - public function load($family = 'config') - { - if ($this->config_loaded) { - return; - } - - $configs = dba::select('config', ['cat', 'v', 'k']); - while ($config = dba::fetch($configs)) { - self::getApp()->setConfigValue($config['cat'], $config['k'], $config['v']); - } - dba::close($configs); - - $this->config_loaded = true; - } - - public function get($cat, $k, $default_value = null, $refresh = false) - { - if ($refresh) { - $config = dba::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]); - if (DBM::is_result($config)) { - self::getApp()->setConfigValue($cat, $k, $config['v']); - } - } - - $return = self::getApp()->getConfigValue($cat, $k, $default_value); - - return $return; - } - - public function set($cat, $k, $value) - { - // We store our setting values as strings. - // So we have to do the conversion here so that the compare below works. - // The exception are array values. - $compare_value = !is_array($value) ? (string)$value : $value; - - if (self::getApp()->getConfigValue($cat, $k) === $compare_value) { - return true; - } - - self::getApp()->setConfigValue($cat, $k, $value); - - // manage array value - $dbvalue = is_array($value) ? serialize($value) : $value; - - $result = dba::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true); - if (!$result) { - throw new Exception('Unable to store config value in [' . $cat . '][' . $k . ']'); - } - - return true; - } - - public function delete($cat, $k) - { - self::getApp()->deleteConfigValue($cat, $k); - - $result = dba::delete('config', ['cat' => $cat, 'k' => $k]); - - return $result; - } -} + + */ +class PreloadConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter +{ + private $config_loaded = false; + + /** + * @var IConfigCache The config cache of this driver + */ + private $configCache; + + /** + * @param IConfigCache $configCache The config cache of this driver + */ + public function __construct(IConfigCache $configCache) + { + $this->configCache = $configCache; + $this->connected = DBA::connected(); + $this->load(); + } + + /** + * {@inheritdoc} + */ + public function load($family = 'config') + { + if (!$this->isConnected()) { + return; + } + + if ($this->config_loaded) { + return; + } + + $configs = DBA::select('config', ['cat', 'v', 'k']); + while ($config = DBA::fetch($configs)) { + $this->configCache->set($config['cat'], $config['k'], $config['v']); + } + DBA::close($configs); + + $this->config_loaded = true; + } + + /** + * {@inheritdoc} + */ + public function get($cat, $k, $default_value = null, $refresh = false) + { + if (!$this->isConnected()) { + return $default_value; + } + + if ($refresh) { + $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]); + if (DBA::isResult($config)) { + $this->configCache->set($cat, $k, $config['v']); + } + } + + $return = $this->configCache->get($cat, $k, $default_value); + + return $return; + } + + /** + * {@inheritdoc} + */ + public function set($cat, $k, $value) + { + if (!$this->isConnected()) { + return false; + } + + // We store our setting values as strings. + // So we have to do the conversion here so that the compare below works. + // The exception are array values. + $compare_value = !is_array($value) ? (string)$value : $value; + + if ($this->configCache->get($cat, $k) === $compare_value) { + return true; + } + + $this->configCache->set($cat, $k, $value); + + // manage array value + $dbvalue = is_array($value) ? serialize($value) : $value; + + $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true); + if (!$result) { + throw new Exception('Unable to store config value in [' . $cat . '][' . $k . ']'); + } + + return true; + } + + /** + * {@inheritdoc} + */ + public function delete($cat, $k) + { + if (!$this->isConnected()) { + return false; + } + + $this->configCache->delete($cat, $k); + + $result = DBA::delete('config', ['cat' => $cat, 'k' => $k]); + + return $result; + } +}