X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FCore%2FConfig%2FPreloadPConfigAdapter.php;h=af97815adef38d159ab6ba44f27766d10ed7ca9d;hb=01b7a3976ff1a00700c429db43fcbdec7158abca;hp=002094a51692f00b85dd2ff5f7fa5bfaed5f2355;hpb=d760d339895d40e9de86ab98471d233179502a8a;p=friendica.git diff --git a/src/Core/Config/PreloadPConfigAdapter.php b/src/Core/Config/PreloadPConfigAdapter.php index 002094a516..af97815ade 100644 --- a/src/Core/Config/PreloadPConfigAdapter.php +++ b/src/Core/Config/PreloadPConfigAdapter.php @@ -1,102 +1,125 @@ - - */ -class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter -{ - private $config_loaded = false; - - public function __construct($uid) - { - $this->load($uid, 'config'); - } - - public function load($uid, $family) - { - if ($this->config_loaded) { - return; - } - - $a = self::getApp(); - - $pconfigs = dba::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]); - while ($pconfig = dba::fetch($pconfigs)) { - $cat = $pconfig['cat']; - $k = $pconfig['k']; - $value = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']); - - $a->config[$uid][$cat][$k] = $value; - } - dba::close($pconfigs); - - $this->config_loaded = true; - } - - public function get($uid, $cat, $k, $default_value = null, $refresh = false) - { - $a = self::getApp(); - - $return = $default_value; - - if (isset($a->config[$uid][$cat][$k])) { - $return = $a->config[$uid][$cat][$k]; - } - - return $return; - } - - public function set($uid, $cat, $k, $value) - { - $a = self::getApp(); - - // 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->get($uid, $cat, $k) === $compare_value) { - return true; - } - - $a->config[$uid][$cat][$k] = $value; - - // manage array value - $dbvalue = is_array($value) ? serialize($value) : $value; - - $result = dba::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true); - - if (!$result) { - throw new Exception('Unable to store config value in [' . $uid . '][' . $cat . '][' . $k . ']'); - } - - return true; - } - - public function delete($uid, $cat, $k) - { - $a = self::getApp(); - - if (!isset($a->config[$uid][$cat][$k])) { - return true; - } - - unset($a->config[$uid][$cat][$k]); - - $result = dba::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]); - - return $result; - } -} + + */ +class PreloadPConfigAdapter implements IPConfigAdapter +{ + private $config_loaded = false; + + /** + * The config cache of this adapter + * @var IPConfigCache + */ + private $configCache; + + /** + * @param IPConfigCache $configCache The config cache of this adapter + * @param int $uid The UID of the current user + */ + public function __construct(IPConfigCache $configCache, $uid = null) + { + $this->configCache = $configCache; + if (isset($uid)) { + $this->load($uid, 'config'); + } + } + + /** + * {@inheritdoc} + */ + public function load($uid, $family) + { + if ($this->config_loaded) { + return; + } + + if (empty($uid)) { + return; + } + + $pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]); + while ($pconfig = DBA::fetch($pconfigs)) { + $this->configCache->setP($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']); + } + DBA::close($pconfigs); + + $this->config_loaded = true; + } + + /** + * {@inheritdoc} + */ + public function get($uid, $cat, $k, $default_value = null, $refresh = false) + { + if (!$this->config_loaded) { + $this->load($uid, $cat); + } + + if ($refresh) { + $config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]); + if (DBA::isResult($config)) { + $this->configCache->setP($uid, $cat, $k, $config['v']); + } else { + $this->configCache->deleteP($uid, $cat, $k); + } + } + + return $this->configCache->getP($uid, $cat, $k, $default_value);; + } + + /** + * {@inheritdoc} + */ + public function set($uid, $cat, $k, $value) + { + if (!$this->config_loaded) { + $this->load($uid, $cat); + } + // 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->getP($uid, $cat, $k) === $compare_value) { + return true; + } + + $this->configCache->setP($uid, $cat, $k, $value); + + // manage array value + $dbvalue = is_array($value) ? serialize($value) : $value; + + $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true); + if (!$result) { + throw new Exception('Unable to store config value in [' . $uid . '][' . $cat . '][' . $k . ']'); + } + + return true; + } + + /** + * {@inheritdoc} + */ + public function delete($uid, $cat, $k) + { + if (!$this->config_loaded) { + $this->load($uid, $cat); + } + + $this->configCache->deleteP($uid, $cat, $k); + + $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]); + + return $result; + } +}