2 namespace Friendica\Core\Config;
4 use Friendica\BaseObject;
5 use Friendica\Database\DBA;
8 * JustInTime Configuration Adapter
10 * Default Config Adapter. Provides the best performance for pages loading few configuration variables.
12 * @author Hypolite Petovan <hypolite@mrpetovan.com>
14 class JITConfigAdapter extends BaseObject implements IConfigAdapter
19 public function load($cat = "config")
21 // We don't preload "system" anymore.
22 // This reduces the number of database reads a lot.
23 if ($cat === 'system') {
27 $configs = DBA::select('config', ['v', 'k'], ['cat' => $cat]);
28 while ($config = DBA::fetch($configs)) {
31 self::getApp()->setConfigValue($cat, $k, $config['v']);
33 if ($cat !== 'config') {
34 $this->cache[$cat][$k] = $config['v'];
35 $this->in_db[$cat][$k] = true;
41 public function get($cat, $k, $default_value = null, $refresh = false)
46 // Do we have the cached value? Then return it
47 if (isset($this->cache[$cat][$k])) {
48 if ($this->cache[$cat][$k] === '!<unset>!') {
49 return $default_value;
51 return $this->cache[$cat][$k];
56 $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
57 if (DBA::isResult($config)) {
59 $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
61 // Assign the value from the database to the cache
62 $this->cache[$cat][$k] = $value;
63 $this->in_db[$cat][$k] = true;
65 } elseif (isset($a->config[$cat][$k])) {
66 // Assign the value (mostly) from config/local.config.php file to the cache
67 $this->cache[$cat][$k] = $a->config[$cat][$k];
68 $this->in_db[$cat][$k] = false;
70 return $a->config[$cat][$k];
71 } elseif (isset($a->config[$k])) {
72 // Assign the value (mostly) from config/local.config.php file to the cache
73 $this->cache[$k] = $a->config[$k];
74 $this->in_db[$k] = false;
76 return $a->config[$k];
79 $this->cache[$cat][$k] = '!<unset>!';
80 $this->in_db[$cat][$k] = false;
82 return $default_value;
85 public function set($cat, $k, $value)
87 // We store our setting values in a string variable.
88 // So we have to do the conversion here so that the compare below works.
89 // The exception are array values.
90 $dbvalue = (!is_array($value) ? (string)$value : $value);
92 $stored = $this->get($cat, $k, null, true);
94 if (!isset($this->in_db[$cat])) {
95 $this->in_db[$cat] = [];
97 if (!isset($this->in_db[$cat][$k])) {
98 $this->in_db[$cat] = false;
101 if (($stored === $dbvalue) && $this->in_db[$cat][$k]) {
105 self::getApp()->setConfigValue($cat, $k, $value);
107 // Assign the just added value to the cache
108 $this->cache[$cat][$k] = $dbvalue;
110 // manage array value
111 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
113 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
116 $this->in_db[$cat][$k] = true;
122 public function delete($cat, $k)
124 if (isset($this->cache[$cat][$k])) {
125 unset($this->cache[$cat][$k]);
126 unset($this->in_db[$cat][$k]);
129 $result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);