From 87f2d185541eeb2475c16c844b172c5fa89a6527 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Tue, 6 Mar 2018 20:04:04 -0500 Subject: [PATCH] Move *ConfigValue functions to App --- src/App.php | 112 ++++++++++++++++++++++ src/Core/Config.php | 4 +- src/Core/Config/JITConfigAdapter.php | 22 ++--- src/Core/Config/JITPConfigAdapter.php | 22 +++-- src/Core/Config/PreloadConfigAdapter.php | 86 ++--------------- src/Core/Config/PreloadPConfigAdapter.php | 76 ++------------- 6 files changed, 150 insertions(+), 172 deletions(-) diff --git a/src/App.php b/src/App.php index 0acbfe6f41..2330bc1185 100644 --- a/src/App.php +++ b/src/App.php @@ -944,4 +944,116 @@ class App return true; } + + /** + * @param string $cat Config category + * @param string $k Config key + * @param mixed $default Default value if it isn't set + */ + public function getConfigValue($cat, $k, $default = null) + { + $return = $default; + + if ($cat === 'config') { + if (isset($this->config[$k])) { + $return = $this->config[$k]; + } + } else { + if (isset($this->config[$cat][$k])) { + $return = $this->config[$cat][$k]; + } + } + + return $return; + } + + /** + * Sets a value in the config cache. Accepts raw output from the config table + * + * @param string $cat Config category + * @param string $k Config key + * @param mixed $v Value to set + */ + public function setConfigValue($cat, $k, $v) + { + // Only arrays are serialized in database, so we have to unserialize sparingly + $value = is_string($v) && preg_match("|^a:[0-9]+:{.*}$|s", $v) ? unserialize($v) : $v; + + if ($cat === 'config') { + $this->config[$k] = $value; + } else { + $this->config[$cat][$k] = $value; + } + } + + /** + * Deletes a value from the config cache + * + * @param string $cat Config category + * @param string $k Config key + */ + public function deleteConfigValue($cat, $k) + { + if ($cat === 'config') { + if (isset($this->config[$k])) { + unset($this->config[$k]); + } + } else { + if (isset($this->config[$cat][$k])) { + unset($this->config[$cat][$k]); + } + } + } + + + /** + * Retrieves a value from the user config cache + * + * @param int $uid User Id + * @param string $cat Config category + * @param string $k Config key + * @param mixed $default Default value if key isn't set + */ + public function getPConfigValue($uid, $cat, $k, $default = null) + { + $return = $default; + + if (isset($this->config[$uid][$cat][$k])) { + $return = $this->config[$uid][$cat][$k]; + } + + return $return; + } + + /** + * Sets a value in the user config cache + * + * Accepts raw output from the pconfig table + * + * @param int $uid User Id + * @param string $cat Config category + * @param string $k Config key + * @param mixed $v Value to set + */ + public function setPConfigValue($uid, $cat, $k, $v) + { + // Only arrays are serialized in database, so we have to unserialize sparingly + $value = is_string($v) && preg_match("|^a:[0-9]+:{.*}$|s", $v) ? unserialize($v) : $v; + + $this->config[$uid][$cat][$k] = $value; + } + + /** + * Deletes a value from the user config cache + * + * @param int $uid User Id + * @param string $cat Config category + * @param string $k Config key + */ + public function deletePConfigValue($uid, $cat, $k) + { + if (isset($this->config[$uid][$cat][$k])) { + unset($this->config[$uid][$cat][$k]); + } + } } diff --git a/src/Core/Config.php b/src/Core/Config.php index bff5d81ff0..3c1d3245fd 100644 --- a/src/Core/Config.php +++ b/src/Core/Config.php @@ -29,9 +29,7 @@ class Config extends BaseObject public static function init() { - $a = self::getApp(); - - if (isset($a->config['system']['config_adapter']) && $a->config['system']['config_adapter'] == 'preload') { + if (self::getApp()->getConfigValue('system', 'config_adapter') == 'preload') { self::$adapter = new Config\PreloadConfigAdapter(); } else { self::$adapter = new Config\JITConfigAdapter(); diff --git a/src/Core/Config/JITConfigAdapter.php b/src/Core/Config/JITConfigAdapter.php index 1bc3bf5a8f..0e7731690b 100644 --- a/src/Core/Config/JITConfigAdapter.php +++ b/src/Core/Config/JITConfigAdapter.php @@ -8,7 +8,7 @@ use Friendica\Database\DBM; require_once 'include/dba.php'; /** - * JustInTime ConfigAdapter + * JustInTime Configuration Adapter * * Default Config Adapter. Provides the best performance for pages loading few configuration variables. * @@ -27,17 +27,15 @@ class JITConfigAdapter extends BaseObject implements IConfigAdapter return; } - $a = self::getApp(); - $configs = dba::select('config', ['v', 'k'], ['cat' => $cat]); while ($config = dba::fetch($configs)) { $k = $config['k']; - if ($cat === 'config') { - $a->config[$k] = $config['v']; - } else { - $a->config[$cat][$k] = $config['v']; - self::$cache[$cat][$k] = $config['v']; - self::$in_db[$cat][$k] = true; + + self::getApp()->setConfigValue($cat, $k, $config['v']); + + if ($cat !== 'config') { + $this->cache[$cat][$k] = $config['v']; + $this->in_db[$cat][$k] = true; } } dba::close($configs); @@ -96,11 +94,7 @@ class JITConfigAdapter extends BaseObject implements IConfigAdapter return true; } - if ($cat === 'config') { - $a->config[$k] = $dbvalue; - } elseif ($cat != 'system') { - $a->config[$cat][$k] = $dbvalue; - } + self::getApp()->setConfigValue($cat, $k, $value); // Assign the just added value to the cache $this->cache[$cat][$k] = $dbvalue; diff --git a/src/Core/Config/JITPConfigAdapter.php b/src/Core/Config/JITPConfigAdapter.php index 27f2ed8622..ce9c5b6462 100644 --- a/src/Core/Config/JITPConfigAdapter.php +++ b/src/Core/Config/JITPConfigAdapter.php @@ -8,7 +8,7 @@ use Friendica\Database\DBM; require_once 'include/dba.php'; /** - * JustInTime PConfigAdapter + * JustInTime User Configuration Adapter * * Default PConfig Adapter. Provides the best performance for pages loading few configuration variables. * @@ -26,7 +26,9 @@ class JITPConfigAdapter extends BaseObject implements IPConfigAdapter if (DBM::is_result($pconfigs)) { while ($pconfig = dba::fetch($pconfigs)) { $k = $pconfig['k']; - $a->config[$uid][$cat][$k] = $pconfig['v']; + + self::getApp()->setPConfigValue($uid, $cat, $k, $pconfig['v']); + $this->in_db[$uid][$cat][$k] = true; } } else if ($cat != 'config') { @@ -59,12 +61,15 @@ class JITPConfigAdapter extends BaseObject implements IPConfigAdapter $pconfig = dba::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]); if (DBM::is_result($pconfig)) { $val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']); - $a->config[$uid][$cat][$k] = $val; + + self::getApp()->setPConfigValue($uid, $cat, $k, $val); + $this->in_db[$uid][$cat][$k] = true; return $val; } else { - $a->config[$uid][$cat][$k] = '!!'; + self::getApp()->setPConfigValue($uid, $cat, $k, '!!'); + $this->in_db[$uid][$cat][$k] = false; return $default_value; @@ -73,8 +78,6 @@ class JITPConfigAdapter extends BaseObject implements IPConfigAdapter public function set($uid, $cat, $k, $value) { - $a = self::getApp(); - // 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. @@ -86,7 +89,7 @@ class JITPConfigAdapter extends BaseObject implements IPConfigAdapter return true; } - $a->config[$uid][$cat][$k] = $dbvalue; + self::getApp()->setPConfigValue($uid, $cat, $k, $value); // manage array value $dbvalue = (is_array($value) ? serialize($value) : $dbvalue); @@ -103,10 +106,9 @@ class JITPConfigAdapter extends BaseObject implements IPConfigAdapter public function delete($uid, $cat, $k) { - $a = self::getApp(); + self::getApp()->deletePConfigValue($uid, $cat, $k); - if (!empty($a->config[$uid][$cat][$k])) { - unset($a->config[$uid][$cat][$k]); + if (!empty($this->in_db[$uid][$cat][$k])) { unset($this->in_db[$uid][$cat][$k]); } diff --git a/src/Core/Config/PreloadConfigAdapter.php b/src/Core/Config/PreloadConfigAdapter.php index 6d4350042d..f87b47f16c 100644 --- a/src/Core/Config/PreloadConfigAdapter.php +++ b/src/Core/Config/PreloadConfigAdapter.php @@ -11,9 +11,9 @@ use Friendica\Database\DBM; require_once 'include/dba.php'; /** - * Preload ConfigAdapter + * Preload Configuration Adapter * - * Minimize the number of database queries to retrieve configuration values at the cost of memory. + * Minimizes the number of database queries to retrieve configuration values at the cost of memory. * * @author Hypolite Petovan */ @@ -34,7 +34,7 @@ class PreloadConfigAdapter extends BaseObject implements IConfigAdapter $configs = dba::select('config', ['cat', 'v', 'k']); while ($config = dba::fetch($configs)) { - $this->setPreloadedValue($config['cat'], $config['k'], $config['v']); + self::getApp()->setConfigValue($config['cat'], $config['k'], $config['v']); } dba::close($configs); @@ -46,11 +46,11 @@ class PreloadConfigAdapter extends BaseObject implements IConfigAdapter if ($refresh) { $config = dba::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]); if (DBM::is_result($config)) { - $this->setPreloadedValue($cat, $k, $config['v']); + self::getApp()->setConfigValue($cat, $k, $config['v']); } } - $return = $this->getPreloadedValue($cat, $k, $default_value); + $return = self::getApp()->getConfigValue($cat, $k, $default_value); return $return; } @@ -62,11 +62,11 @@ class PreloadConfigAdapter extends BaseObject implements IConfigAdapter // The exception are array values. $compare_value = !is_array($value) ? (string)$value : $value; - if ($this->getPreloadedValue($cat, $k) === $compare_value) { + if (self::getApp()->getConfigValue($cat, $k) === $compare_value) { return true; } - $this->setPreloadedValue($cat, $k, $value); + self::getApp()->setConfigValue($cat, $k, $value); // manage array value $dbvalue = is_array($value) ? serialize($value) : $value; @@ -81,80 +81,10 @@ class PreloadConfigAdapter extends BaseObject implements IConfigAdapter public function delete($cat, $k) { - $this->deletePreloadedValue($cat, $k); + self::getApp()->deleteConfigValue($cat, $k); $result = dba::delete('config', ['cat' => $cat, 'k' => $k]); return $result; } - - /** - * Retrieves a preloaded value from the App config cache - * - * @param string $cat - * @param string $k - * @param mixed $default - */ - private function getPreloadedValue($cat, $k, $default = null) - { - $a = self::getApp(); - - $return = $default; - - if ($cat === 'config') { - if (isset($a->config[$k])) { - $return = $a->config[$k]; - } - } else { - if (isset($a->config[$cat][$k])) { - $return = $a->config[$cat][$k]; - } - } - - return $return; - } - - /** - * Sets a preloaded value in the App config cache - * - * Accepts raw output from the config table - * - * @param string $cat - * @param string $k - * @param mixed $v - */ - private function setPreloadedValue($cat, $k, $v) - { - $a = self::getApp(); - - // Only arrays are serialized in database, so we have to unserialize sparingly - $value = is_string($v) && preg_match("|^a:[0-9]+:{.*}$|s", $v) ? unserialize($v) : $v; - - if ($cat === 'config') { - $a->config[$k] = $value; - } else { - $a->config[$cat][$k] = $value; - } - } - - /** - * Deletes a preloaded value from the App config cache - * - * @param string $cat - * @param string $k - */ - private function deletePreloadedValue($cat, $k) - { - $a = self::getApp(); - - if ($cat === 'config') { - if (isset($a->config[$k])) { - unset($a->config[$k]); - } - } else { - if (isset($a->config[$cat][$k])) { - unset($a->config[$cat][$k]); - } - } - } } diff --git a/src/Core/Config/PreloadPConfigAdapter.php b/src/Core/Config/PreloadPConfigAdapter.php index d6a44e07c0..d235410339 100644 --- a/src/Core/Config/PreloadPConfigAdapter.php +++ b/src/Core/Config/PreloadPConfigAdapter.php @@ -11,9 +11,9 @@ use Friendica\Database\DBM; require_once 'include/dba.php'; /** - * Preload PConfigAdapter + * Preload User Configuration Adapter * - * Minimize the number of database queries to retrieve configuration values at the cost of memory. + * Minimizes the number of database queries to retrieve configuration values at the cost of memory. * * @author Hypolite Petovan */ @@ -34,7 +34,7 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter $pconfigs = dba::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]); while ($pconfig = dba::fetch($pconfigs)) { - $this->setPreloadedValue($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']); + self::getApp()->setPConfigValue($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']); } dba::close($pconfigs); @@ -46,13 +46,13 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter if ($refresh) { $config = dba::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]); if (DBM::is_result($config)) { - $this->setPreloadedValue($uid, $cat, $k, $config['v']); + self::getApp()->setPConfigValue($uid, $cat, $k, $config['v']); } else { - $this->deletePreloadedValue($uid, $cat, $k); + self::getApp()->deletePConfigValue($uid, $cat, $k); } } - $return = $this->getPreloadedValue($uid, $cat, $k, $default_value); + $return = self::getApp()->getPConfigValue($uid, $cat, $k, $default_value); return $return; } @@ -64,11 +64,11 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter // The exception are array values. $compare_value = !is_array($value) ? (string)$value : $value; - if ($this->getPreloadedValue($uid, $cat, $k) === $compare_value) { + if (self::getApp()->getPConfigValue($uid, $cat, $k) === $compare_value) { return true; } - $this->setPreloadedValue($uid, $cat, $k, $value); + self::getApp()->setPConfigValue($uid, $cat, $k, $value); // manage array value $dbvalue = is_array($value) ? serialize($value) : $value; @@ -83,68 +83,10 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter public function delete($uid, $cat, $k) { - $this->deletePreloadedValue($uid, $cat, $k); + self::getApp()->deletePConfigValue($uid, $cat, $k); $result = dba::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]); return $result; } - - - /** - * Retrieves a preloaded value from the App user config cache - * - * @param int $uid - * @param string $cat - * @param string $k - * @param mixed $default - */ - private function getPreloadedValue($uid, $cat, $k, $default = null) - { - $a = self::getApp(); - - $return = $default; - - if (isset($a->config[$uid][$cat][$k])) { - $return = $a->config[$uid][$cat][$k]; - } - - return $return; - } - - /** - * Sets a preloaded value in the App user config cache - * - * Accepts raw output from the pconfig table - * - * @param int $uid - * @param string $cat - * @param string $k - * @param mixed $v - */ - private function setPreloadedValue($uid, $cat, $k, $v) - { - $a = self::getApp(); - - // Only arrays are serialized in database, so we have to unserialize sparingly - $value = is_string($v) && preg_match("|^a:[0-9]+:{.*}$|s", $v) ? unserialize($v) : $v; - - $a->config[$uid][$cat][$k] = $value; - } - - /** - * Deletes a preloaded value from the App user config cache - * - * @param int $uid - * @param string $cat - * @param string $k - */ - private function deletePreloadedValue($uid, $cat, $k) - { - $a = self::getApp(); - - if (isset($a->config[$uid][$cat][$k])) { - unset($a->config[$uid][$cat][$k]); - } - } }