]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/JITConfigAdapter.php
Fix tests (#5400)
[friendica.git] / src / Core / Config / JITConfigAdapter.php
1 <?php
2 namespace Friendica\Core\Config;
3
4 use dba;
5 use Friendica\BaseObject;
6 use Friendica\Database\DBM;
7
8 require_once 'include/dba.php';
9
10 /**
11  * JustInTime Configuration Adapter
12  *
13  * Default Config Adapter. Provides the best performance for pages loading few configuration variables.
14  *
15  * @author Hypolite Petovan <mrpetovan@gmail.com>
16  */
17 class JITConfigAdapter extends BaseObject implements IConfigAdapter
18 {
19         private $cache;
20         private $in_db;
21
22         public function load($cat = "config")
23         {
24                 // We don't preload "system" anymore.
25                 // This reduces the number of database reads a lot.
26                 if ($cat === 'system') {
27                         return;
28                 }
29
30                 $configs = dba::select('config', ['v', 'k'], ['cat' => $cat]);
31                 while ($config = dba::fetch($configs)) {
32                         $k = $config['k'];
33
34                         self::getApp()->setConfigValue($cat, $k, $config['v']);
35
36                         if ($cat !== 'config') {
37                                 $this->cache[$cat][$k] = $config['v'];
38                                 $this->in_db[$cat][$k] = true;
39                         }
40                 }
41                 dba::close($configs);
42         }
43
44         public function get($cat, $k, $default_value = null, $refresh = false)
45         {
46                 $a = self::getApp();
47
48                 if (!$refresh) {
49                         // Do we have the cached value? Then return it
50                         if (isset($this->cache[$cat][$k])) {
51                                 if ($this->cache[$cat][$k] === '!<unset>!') {
52                                         return $default_value;
53                                 } else {
54                                         return $this->cache[$cat][$k];
55                                 }
56                         }
57                 }
58
59                 $config = dba::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
60                 if (DBM::is_result($config)) {
61                         // manage array value
62                         $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
63
64                         // Assign the value from the database to the cache
65                         $this->cache[$cat][$k] = $value;
66                         $this->in_db[$cat][$k] = true;
67                         return $value;
68                 } elseif (isset($a->config[$cat][$k])) {
69                         // Assign the value (mostly) from config/local.ini.php file to the cache
70                         $this->cache[$cat][$k] = $a->config[$cat][$k];
71                         $this->in_db[$cat][$k] = false;
72
73                         return $a->config[$cat][$k];
74                 } elseif (isset($a->config[$k])) {
75                         // Assign the value (mostly) from config/local.ini.php file to the cache
76                         $this->cache[$k] = $a->config[$k];
77                         $this->in_db[$k] = false;
78
79                         return $a->config[$k];
80                 }
81
82                 $this->cache[$cat][$k] = '!<unset>!';
83                 $this->in_db[$cat][$k] = false;
84
85                 return $default_value;
86         }
87
88         public function set($cat, $k, $value)
89         {
90                 $a = self::getApp();
91
92                 // We store our setting values in a string variable.
93                 // So we have to do the conversion here so that the compare below works.
94                 // The exception are array values.
95                 $dbvalue = (!is_array($value) ? (string)$value : $value);
96
97                 $stored = $this->get($cat, $k, null, true);
98
99                 if (!isset($this->in_db[$cat])) {
100                         $this->in_db[$cat] = [];
101                 }
102                 if (!isset($this->in_db[$cat][$k])) {
103                         $this->in_db[$cat] = false;
104                 }
105
106                 if (($stored === $dbvalue) && $this->in_db[$cat][$k]) {
107                         return true;
108                 }
109
110                 self::getApp()->setConfigValue($cat, $k, $value);
111
112                 // Assign the just added value to the cache
113                 $this->cache[$cat][$k] = $dbvalue;
114
115                 // manage array value
116                 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
117
118                 $result = dba::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
119
120                 if ($result) {
121                         $this->in_db[$cat][$k] = true;
122                 }
123
124                 return $result;
125         }
126
127         public function delete($cat, $k)
128         {
129                 if (isset($this->cache[$cat][$k])) {
130                         unset($this->cache[$cat][$k]);
131                         unset($this->in_db[$cat][$k]);
132                 }
133
134                 $result = dba::delete('config', ['cat' => $cat, 'k' => $k]);
135
136                 return $result;
137         }
138 }