Add refresh feature to Preload (P)Config adapters
- Add private methods to manipulat the App config variable
This commit is contained in:
parent
4b9c52aad0
commit
dcd1f18611
|
@ -4,7 +4,9 @@ namespace Friendica\Core\Config;
|
|||
|
||||
use dba;
|
||||
use Exception;
|
||||
use Friendica\App;
|
||||
use Friendica\BaseObject;
|
||||
use Friendica\Database\DBM;
|
||||
|
||||
require_once 'include/dba.php';
|
||||
|
||||
|
@ -30,19 +32,9 @@ class PreloadConfigAdapter extends BaseObject implements IConfigAdapter
|
|||
return;
|
||||
}
|
||||
|
||||
$a = self::getApp();
|
||||
|
||||
$configs = dba::select('config', ['cat', 'v', 'k']);
|
||||
while ($config = dba::fetch($configs)) {
|
||||
$cat = $config['cat'];
|
||||
$k = $config['k'];
|
||||
$value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
|
||||
|
||||
if ($cat === 'config') {
|
||||
$a->config[$k] = $value;
|
||||
} else {
|
||||
$a->config[$cat][$k] = $value;
|
||||
}
|
||||
$this->setPreloadedValue($config['cat'], $config['k'], $config['v']);
|
||||
}
|
||||
dba::close($configs);
|
||||
|
||||
|
@ -51,41 +43,32 @@ class PreloadConfigAdapter extends BaseObject implements IConfigAdapter
|
|||
|
||||
public function get($cat, $k, $default_value = null, $refresh = false)
|
||||
{
|
||||
$a = self::getApp();
|
||||
|
||||
$return = $default_value;
|
||||
|
||||
if ($cat === 'config') {
|
||||
if (isset($a->config[$k])) {
|
||||
$return = $a->config[$k];
|
||||
}
|
||||
} else {
|
||||
if (isset($a->config[$cat][$k])) {
|
||||
$return = $a->config[$cat][$k];
|
||||
if ($refresh) {
|
||||
$config = dba::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
|
||||
if (DBM::is_result($config)) {
|
||||
$this->setPreloadedValue($cat, $k, $config['v']);
|
||||
} else {
|
||||
$this->deletePreloadedValue($cat, $k);
|
||||
}
|
||||
}
|
||||
|
||||
$return = $this->getPreloadedValue($cat, $k, $default_value);
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
public function set($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($cat, $k) === $compare_value) {
|
||||
if ($this->getPreloadedValue($cat, $k) === $compare_value) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($cat === 'config') {
|
||||
$a->config[$k] = $value;
|
||||
} else {
|
||||
$a->config[$cat][$k] = $value;
|
||||
}
|
||||
$this->setPreloadedValue($cat, $k, $value);
|
||||
|
||||
// manage array value
|
||||
$dbvalue = is_array($value) ? serialize($value) : $value;
|
||||
|
@ -99,6 +82,70 @@ class PreloadConfigAdapter extends BaseObject implements IConfigAdapter
|
|||
}
|
||||
|
||||
public function delete($cat, $k)
|
||||
{
|
||||
$this->deletePreloadedValue($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();
|
||||
|
||||
|
@ -111,9 +158,5 @@ class PreloadConfigAdapter extends BaseObject implements IConfigAdapter
|
|||
unset($a->config[$cat][$k]);
|
||||
}
|
||||
}
|
||||
|
||||
$result = dba::delete('config', ['cat' => $cat, 'k' => $k]);
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,9 @@ namespace Friendica\Core\Config;
|
|||
|
||||
use dba;
|
||||
use Exception;
|
||||
use Friendica\App;
|
||||
use Friendica\BaseObject;
|
||||
use Friendica\Database\DBM;
|
||||
|
||||
require_once 'include/dba.php';
|
||||
|
||||
|
@ -30,15 +32,9 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
|
|||
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;
|
||||
$this->setPreloadedValue($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']);
|
||||
}
|
||||
dba::close($pconfigs);
|
||||
|
||||
|
@ -47,37 +43,37 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
|
|||
|
||||
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];
|
||||
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']);
|
||||
} else {
|
||||
$this->deletePreloadedValue($uid, $cat, $k);
|
||||
}
|
||||
}
|
||||
|
||||
$return = $this->getPreloadedValue($uid, $cat, $k, $default_value);
|
||||
|
||||
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) {
|
||||
if ($this->getPreloadedValue($uid, $cat, $k) === $compare_value) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$a->config[$uid][$cat][$k] = $value;
|
||||
$this->setPreloadedValue($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 . ']');
|
||||
}
|
||||
|
@ -87,16 +83,68 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
|
|||
|
||||
public function delete($uid, $cat, $k)
|
||||
{
|
||||
$a = self::getApp();
|
||||
|
||||
if (!isset($a->config[$uid][$cat][$k])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
unset($a->config[$uid][$cat][$k]);
|
||||
$this->deletePreloadedValue($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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user