3 namespace Friendica\Core\Config;
6 use Friendica\BaseObject;
7 use Friendica\Database\dba;
8 use Friendica\Database\DBM;
10 require_once 'include/dba.php';
13 * Preload Configuration Adapter
15 * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
17 * @author Hypolite Petovan <mrpetovan@gmail.com>
19 class PreloadConfigAdapter extends BaseObject implements IConfigAdapter
21 private $config_loaded = false;
23 public function __construct()
28 public function load($family = 'config')
30 if ($this->config_loaded) {
34 $configs = dba::select('config', ['cat', 'v', 'k']);
35 while ($config = dba::fetch($configs)) {
36 self::getApp()->setConfigValue($config['cat'], $config['k'], $config['v']);
40 $this->config_loaded = true;
43 public function get($cat, $k, $default_value = null, $refresh = false)
46 $config = dba::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
47 if (DBM::is_result($config)) {
48 self::getApp()->setConfigValue($cat, $k, $config['v']);
52 $return = self::getApp()->getConfigValue($cat, $k, $default_value);
57 public function set($cat, $k, $value)
59 // We store our setting values as strings.
60 // So we have to do the conversion here so that the compare below works.
61 // The exception are array values.
62 $compare_value = !is_array($value) ? (string)$value : $value;
64 if (self::getApp()->getConfigValue($cat, $k) === $compare_value) {
68 self::getApp()->setConfigValue($cat, $k, $value);
71 $dbvalue = is_array($value) ? serialize($value) : $value;
73 $result = dba::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
75 throw new Exception('Unable to store config value in [' . $cat . '][' . $k . ']');
81 public function delete($cat, $k)
83 self::getApp()->deleteConfigValue($cat, $k);
85 $result = dba::delete('config', ['cat' => $cat, 'k' => $k]);