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)
89 // We store our setting values in a string variable.
90 // So we have to do the conversion here so that the compare below works.
91 // The exception are array values.
92 $dbvalue = (!is_array($value) ? (string)$value : $value);
94 $stored = $this->get($cat, $k, null, true);
96 if (!isset($this->in_db[$cat])) {
97 $this->in_db[$cat] = [];
99 if (!isset($this->in_db[$cat][$k])) {
100 $this->in_db[$cat] = false;
103 if (($stored === $dbvalue) && $this->in_db[$cat][$k]) {
107 self::getApp()->setConfigValue($cat, $k, $value);
109 // Assign the just added value to the cache
110 $this->cache[$cat][$k] = $dbvalue;
112 // manage array value
113 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
115 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
118 $this->in_db[$cat][$k] = true;
124 public function delete($cat, $k)
126 if (isset($this->cache[$cat][$k])) {
127 unset($this->cache[$cat][$k]);
128 unset($this->in_db[$cat][$k]);
131 $result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);