2) Refactor App->config[] into Core\PConfig

This commit is contained in:
Philipp Holzer 2019-02-03 19:04:41 +01:00
parent be6b229534
commit d43a8184f4
No known key found for this signature in database
GPG key ID: 517BE60E2CE5C8A5
7 changed files with 98 additions and 98 deletions

View file

@ -30,7 +30,6 @@ class App
public $module_loaded = false;
public $module_class = null;
public $query_string = '';
public $config = [];
public $page = [];
public $profile;
public $profile_uid;
@ -1217,67 +1216,6 @@ class App
return true;
}
/**
* 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
*
* @return string The value of the config entry
*/
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;
if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
$this->config[$uid] = [];
}
if (!isset($this->config[$uid][$cat]) || !is_array($this->config[$uid][$cat])) {
$this->config[$uid][$cat] = [];
}
$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]);
}
}
/**
* Generates the site's default sender email address
*

View file

@ -45,7 +45,7 @@ class Config extends BaseObject
* @brief Loads all configuration values of family into a cached storage.
*
* All configuration values of the system are stored in global cache
* which is available under the global variable $a->config
* which is available under the global variable self::$config
*
* @param string $family The category of the configuration value
*
@ -132,7 +132,7 @@ class Config extends BaseObject
/**
* @brief Deletes the given key from the system configuration.
*
* Removes the configured value from the stored cache in $a->config
* Removes the configured value from the stored cache in Config::$config
* and removes it from the database.
*
* @param string $family The category of the configuration value

View file

@ -12,7 +12,7 @@ interface IConfigAdapter
* @brief Loads all configuration values into a cached storage.
*
* All configuration values of the system are stored in global cache
* which is available under the global variable $a->config
* which is available under the global variable Config::$config
*
* @param string $cat The category of the configuration values to load
*
@ -60,7 +60,7 @@ interface IConfigAdapter
/**
* @brief Deletes the given key from the system configuration.
*
* Removes the configured value from the stored cache in $a->config
* Removes the configured value from the stored cache in Config::$config
* and removes it from the database.
*
* @param string $cat The category of the configuration value

View file

@ -18,7 +18,7 @@ interface IPConfigAdapter
* @brief Loads all configuration values of a user's config family into a cached storage.
*
* All configuration values of the given user are stored in global cache
* which is available under the global variable $a->config[$uid].
* which is available under the global variable self::$config[$uid].
*
* @param string $uid The user_id
* @param string $cat The category of the configuration value
@ -32,7 +32,7 @@ interface IPConfigAdapter
* ($family) and a key.
*
* Get a particular user's config value from the given category ($family)
* and the $key from a cached storage in $a->config[$uid].
* and the $key from a cached storage in self::$config[$uid].
*
* @param string $uid The user_id
* @param string $cat The category of the configuration value
@ -64,7 +64,7 @@ interface IPConfigAdapter
/**
* @brief Deletes the given key from the users's configuration.
*
* Removes the configured value from the stored cache in $a->config[$uid]
* Removes the configured value from the stored cache in self::$config[$uid]
* and removes it from the database.
*
* @param string $uid The user_id

View file

@ -1,7 +1,7 @@
<?php
namespace Friendica\Core\Config;
use Friendica\BaseObject;
use Friendica\Core\PConfig;
use Friendica\Database\DBA;
/**
@ -11,47 +11,43 @@ use Friendica\Database\DBA;
*
* @author Hypolite Petovan <hypolite@mrpetovan.com>
*/
class JITPConfigAdapter extends BaseObject implements IPConfigAdapter
class JITPConfigAdapter implements IPConfigAdapter
{
private $in_db;
public function load($uid, $cat)
{
$a = self::getApp();
$pconfigs = DBA::select('pconfig', ['v', 'k'], ['cat' => $cat, 'uid' => $uid]);
if (DBA::isResult($pconfigs)) {
while ($pconfig = DBA::fetch($pconfigs)) {
$k = $pconfig['k'];
self::getApp()->setPConfigValue($uid, $cat, $k, $pconfig['v']);
PConfig::setPConfigValue($uid, $cat, $k, $pconfig['v']);
$this->in_db[$uid][$cat][$k] = true;
}
} else if ($cat != 'config') {
// Negative caching
$a->config[$uid][$cat] = "!<unset>!";
PConfig::setPConfigValue($uid, $cat, "!<unset>!");
}
DBA::close($pconfigs);
}
public function get($uid, $cat, $k, $default_value = null, $refresh = false)
{
$a = self::getApp();
if (!$refresh) {
// Looking if the whole family isn't set
if (isset($a->config[$uid][$cat])) {
if ($a->config[$uid][$cat] === '!<unset>!') {
if (PConfig::getPConfigValue($uid, $cat) !== null) {
if (PConfig::getPConfigValue($uid, $cat) === '!<unset>!') {
return $default_value;
}
}
if (isset($a->config[$uid][$cat][$k])) {
if ($a->config[$uid][$cat][$k] === '!<unset>!') {
if (PConfig::getPConfigValue($uid, $cat, $k) !== null) {
if (PConfig::getPConfigValue($uid, $cat, $k) === '!<unset>!') {
return $default_value;
}
return $a->config[$uid][$cat][$k];
return PConfig::getPConfigValue($uid, $cat, $k);
}
}
@ -59,13 +55,13 @@ class JITPConfigAdapter extends BaseObject implements IPConfigAdapter
if (DBA::isResult($pconfig)) {
$val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
self::getApp()->setPConfigValue($uid, $cat, $k, $val);
PConfig::setPConfigValue($uid, $cat, $k, $val);
$this->in_db[$uid][$cat][$k] = true;
return $val;
} else {
self::getApp()->setPConfigValue($uid, $cat, $k, '!<unset>!');
PConfig::setPConfigValue($uid, $cat, $k, '!<unset>!');
$this->in_db[$uid][$cat][$k] = false;
@ -86,7 +82,7 @@ class JITPConfigAdapter extends BaseObject implements IPConfigAdapter
return true;
}
self::getApp()->setPConfigValue($uid, $cat, $k, $value);
PConfig::setPConfigValue($uid, $cat, $k, $value);
// manage array value
$dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
@ -102,7 +98,7 @@ class JITPConfigAdapter extends BaseObject implements IPConfigAdapter
public function delete($uid, $cat, $k)
{
self::getApp()->deletePConfigValue($uid, $cat, $k);
PConfig::deletePConfigValue($uid, $cat, $k);
if (!empty($this->in_db[$uid][$cat][$k])) {
unset($this->in_db[$uid][$cat][$k]);

View file

@ -3,7 +3,7 @@
namespace Friendica\Core\Config;
use Exception;
use Friendica\BaseObject;
use Friendica\Core\PConfig;
use Friendica\Database\DBA;
/**
@ -13,7 +13,7 @@ use Friendica\Database\DBA;
*
* @author Hypolite Petovan <hypolite@mrpetovan.com>
*/
class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
class PreloadPConfigAdapter implements IPConfigAdapter
{
private $config_loaded = false;
@ -34,7 +34,7 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
$pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
while ($pconfig = DBA::fetch($pconfigs)) {
self::getApp()->setPConfigValue($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']);
PConfig::setPConfigValue($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']);
}
DBA::close($pconfigs);
@ -50,13 +50,13 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
if ($refresh) {
$config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
if (DBA::isResult($config)) {
self::getApp()->setPConfigValue($uid, $cat, $k, $config['v']);
PConfig::setPConfigValue($uid, $cat, $k, $config['v']);
} else {
self::getApp()->deletePConfigValue($uid, $cat, $k);
PConfig::deletePConfigValue($uid, $cat, $k);
}
}
$return = self::getApp()->getPConfigValue($uid, $cat, $k, $default_value);
$return = PConfig::getPConfigValue($uid, $cat, $k, $default_value);
return $return;
}
@ -71,11 +71,11 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
// The exception are array values.
$compare_value = !is_array($value) ? (string)$value : $value;
if (self::getApp()->getPConfigValue($uid, $cat, $k) === $compare_value) {
if (PConfig::getPConfigValue($uid, $cat, $k) === $compare_value) {
return true;
}
self::getApp()->setPConfigValue($uid, $cat, $k, $value);
PConfig::setPConfigValue($uid, $cat, $k, $value);
// manage array value
$dbvalue = is_array($value) ? serialize($value) : $value;
@ -94,7 +94,7 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
$this->load($uid, $cat);
}
self::getApp()->deletePConfigValue($uid, $cat, $k);
PConfig::deletePConfigValue($uid, $cat, $k);
$result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);

View file

@ -20,6 +20,8 @@ use Friendica\BaseObject;
*/
class PConfig extends BaseObject
{
private static $config;
/**
* @var \Friendica\Core\Config\IPConfigAdapter
*/
@ -45,7 +47,7 @@ class PConfig extends BaseObject
* @brief Loads all configuration values of a user's config family into a cached storage.
*
* All configuration values of the given user are stored in global cache
* which is available under the global variable $a->config[$uid].
* which is available under the global variable self::$config[$uid].
*
* @param string $uid The user_id
* @param string $family The category of the configuration value
@ -72,7 +74,7 @@ class PConfig extends BaseObject
* ($family) and a key.
*
* Get a particular user's config value from the given category ($family)
* and the $key from a cached storage in $a->config[$uid].
* and the $key from a cached storage in self::$config[$uid].
*
* @param string $uid The user_id
* @param string $family The category of the configuration value
@ -130,7 +132,7 @@ class PConfig extends BaseObject
/**
* @brief Deletes the given key from the users's configuration.
*
* Removes the configured value from the stored cache in $a->config[$uid]
* Removes the configured value from the stored cache in self::$config[$uid]
* and removes it from the database.
*
* @param string $uid The user_id
@ -153,4 +155,68 @@ class PConfig extends BaseObject
return self::$adapter->delete($uid, $family, $key);
}
/**
* 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
*
* @return string The value of the config entry
*/
public static function getPConfigValue($uid, $cat, $k = null, $default = null)
{
$return = $default;
if (isset(self::$config[$uid][$cat][$k])) {
$return = self::$config[$uid][$cat][$k];
} elseif ($k == null && isset(self::$config[$uid][$cat])) {
$return = self::$config[$uid][$cat];
}
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 static 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;
if (!isset(self::$config[$uid]) || !is_array(self::$config[$uid])) {
self::$config[$uid] = [];
}
if (!isset(self::$config[$uid][$cat]) || !is_array(self::$config[$uid][$cat])) {
self::$config[$uid][$cat] = [];
}
self::$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 static function deletePConfigValue($uid, $cat, $k)
{
if (isset(self::$config[$uid][$cat][$k])) {
unset(self::$config[$uid][$cat][$k]);
}
}
}