2 namespace Friendica\Core\Config;
4 use Friendica\BaseObject;
5 use Friendica\Database\DBA;
7 require_once 'include/dba.php';
10 * JustInTime Configuration Adapter
12 * Default Config Adapter. Provides the best performance for pages loading few configuration variables.
14 * @author Hypolite Petovan <mrpetovan@gmail.com>
16 class JITConfigAdapter extends BaseObject implements IConfigAdapter
21 public function load($cat = "config")
23 // We don't preload "system" anymore.
24 // This reduces the number of database reads a lot.
25 if ($cat === 'system') {
29 $configs = DBA::select('config', ['v', 'k'], ['cat' => $cat]);
30 while ($config = DBA::fetch($configs)) {
33 self::getApp()->setConfigValue($cat, $k, $config['v']);
35 if ($cat !== 'config') {
36 $this->cache[$cat][$k] = $config['v'];
37 $this->in_db[$cat][$k] = true;
43 public function get($cat, $k, $default_value = null, $refresh = false)
48 // Do we have the cached value? Then return it
49 if (isset($this->cache[$cat][$k])) {
50 if ($this->cache[$cat][$k] === '!<unset>!') {
51 return $default_value;
53 return $this->cache[$cat][$k];
58 $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
59 if (DBA::isResult($config)) {
61 $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
63 // Assign the value from the database to the cache
64 $this->cache[$cat][$k] = $value;
65 $this->in_db[$cat][$k] = true;
67 } elseif (isset($a->config[$cat][$k])) {
68 // Assign the value (mostly) from config/local.ini.php file to the cache
69 $this->cache[$cat][$k] = $a->config[$cat][$k];
70 $this->in_db[$cat][$k] = false;
72 return $a->config[$cat][$k];
73 } elseif (isset($a->config[$k])) {
74 // Assign the value (mostly) from config/local.ini.php file to the cache
75 $this->cache[$k] = $a->config[$k];
76 $this->in_db[$k] = false;
78 return $a->config[$k];
81 $this->cache[$cat][$k] = '!<unset>!';
82 $this->in_db[$cat][$k] = false;
84 return $default_value;
87 public function set($cat, $k, $value)
91 // We store our setting values in a string variable.
92 // So we have to do the conversion here so that the compare below works.
93 // The exception are array values.
94 $dbvalue = (!is_array($value) ? (string)$value : $value);
96 $stored = $this->get($cat, $k, null, true);
98 if (!isset($this->in_db[$cat])) {
99 $this->in_db[$cat] = [];
101 if (!isset($this->in_db[$cat][$k])) {
102 $this->in_db[$cat] = false;
105 if (($stored === $dbvalue) && $this->in_db[$cat][$k]) {
109 self::getApp()->setConfigValue($cat, $k, $value);
111 // Assign the just added value to the cache
112 $this->cache[$cat][$k] = $dbvalue;
114 // manage array value
115 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
117 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
120 $this->in_db[$cat][$k] = true;
126 public function delete($cat, $k)
128 if (isset($this->cache[$cat][$k])) {
129 unset($this->cache[$cat][$k]);
130 unset($this->in_db[$cat][$k]);
133 $result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);