From 7afcd6d49e6afed1bc4a94602370eee7859e401e Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 3 Mar 2018 12:13:20 -0500 Subject: [PATCH] Update PConfig class with adapter --- src/Core/PConfig.php | 120 +++++++++++++------------------------------ 1 file changed, 36 insertions(+), 84 deletions(-) diff --git a/src/Core/PConfig.php b/src/Core/PConfig.php index 82d4690838..bfa52f5a36 100644 --- a/src/Core/PConfig.php +++ b/src/Core/PConfig.php @@ -1,20 +1,18 @@ config['system']['config_adapter']) && $a->config['system']['config_adapter'] == 'preload') { + self::$adapter = new Config\PreloadPConfigAdapter($uid); + } else { + self::$adapter = new Config\JITPConfigAdapter($uid); + } + } /** * @brief Loads all configuration values of a user's config family into a cached storage. @@ -39,20 +51,11 @@ class PConfig */ public static function load($uid, $family) { - $a = get_app(); - - $r = dba::select('pconfig', ['v', 'k'], ['cat' => $family, 'uid' => $uid]); - if (DBM::is_result($r)) { - while ($rr = dba::fetch($r)) { - $k = $rr['k']; - $a->config[$uid][$family][$k] = $rr['v']; - self::$in_db[$uid][$family][$k] = true; - } - } else if ($family != 'config') { - // Negative caching - $a->config[$uid][$family] = "!!"; + if (empty(self::$adapter)) { + self::init($uid); } - dba::close($r); + + self::$adapter->load($uid, $family); } /** @@ -72,37 +75,11 @@ class PConfig */ public static function get($uid, $family, $key, $default_value = null, $refresh = false) { - $a = get_app(); - - if (!$refresh) { - // Looking if the whole family isn't set - if (isset($a->config[$uid][$family])) { - if ($a->config[$uid][$family] === '!!') { - return $default_value; - } - } - - if (isset($a->config[$uid][$family][$key])) { - if ($a->config[$uid][$family][$key] === '!!') { - return $default_value; - } - return $a->config[$uid][$family][$key]; - } + if (empty(self::$adapter)) { + self::init($uid); } - $pconfig = dba::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $family, 'k' => $key]); - if (DBM::is_result($pconfig)) { - $val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']); - $a->config[$uid][$family][$key] = $val; - self::$in_db[$uid][$family][$key] = true; - - return $val; - } else { - $a->config[$uid][$family][$key] = '!!'; - self::$in_db[$uid][$family][$key] = false; - - return $default_value; - } + return self::$adapter->get($uid, $family, $key, $default_value, $refresh); } /** @@ -122,31 +99,11 @@ class PConfig */ public static function set($uid, $family, $key, $value) { - $a = get_app(); - - // We store our setting values in a string variable. - // So we have to do the conversion here so that the compare below works. - // The exception are array values. - $dbvalue = (!is_array($value) ? (string)$value : $value); - - $stored = self::get($uid, $family, $key, null, true); - - if (($stored === $dbvalue) && self::$in_db[$uid][$family][$key]) { - return true; + if (empty(self::$adapter)) { + self::init($uid); } - $a->config[$uid][$family][$key] = $dbvalue; - - // manage array value - $dbvalue = (is_array($value) ? serialize($value) : $dbvalue); - - $ret = dba::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $family, 'k' => $key], true); - - if ($ret) { - self::$in_db[$uid][$family][$key] = true; - return $value; - } - return $ret; + return self::$adapter->set($uid, $family, $key, $value); } /** @@ -163,15 +120,10 @@ class PConfig */ public static function delete($uid, $family, $key) { - $a = get_app(); - - if (x($a->config[$uid][$family], $key)) { - unset($a->config[$uid][$family][$key]); - unset(self::$in_db[$uid][$family][$key]); + if (empty(self::$adapter)) { + self::init($uid); } - $ret = dba::delete('pconfig', ['uid' => $uid, 'cat' => $family, 'k' => $key]); - - return $ret; + return self::$adapter->delete($uid, $family, $key); } }