]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/JITConfigAdapter.php
Merge pull request #4849 from M-arcus/installation-script
[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 the .htconfig.php 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                 }
75
76                 $this->cache[$cat][$k] = '!<unset>!';
77                 $this->in_db[$cat][$k] = false;
78
79                 return $default_value;
80         }
81
82         public function set($cat, $k, $value)
83         {
84                 $a = self::getApp();
85
86                 // We store our setting values in a string variable.
87                 // So we have to do the conversion here so that the compare below works.
88                 // The exception are array values.
89                 $dbvalue = (!is_array($value) ? (string)$value : $value);
90
91                 $stored = $this->get($cat, $k, null, true);
92
93                 if (($stored === $dbvalue) && $this->in_db[$cat][$k]) {
94                         return true;
95                 }
96
97                 self::getApp()->setConfigValue($cat, $k, $value);
98
99                 // Assign the just added value to the cache
100                 $this->cache[$cat][$k] = $dbvalue;
101
102                 // manage array value
103                 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
104
105                 $result = dba::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
106
107                 if ($result) {
108                         $this->in_db[$cat][$k] = true;
109                         return $value;
110                 }
111
112                 return $result;
113         }
114
115         public function delete($cat, $k)
116         {
117                 if (isset($this->cache[$cat][$k])) {
118                         unset($this->cache[$cat][$k]);
119                         unset($this->in_db[$cat][$k]);
120                 }
121
122                 $result = dba::delete('config', ['cat' => $cat, 'k' => $k]);
123
124                 return $result;
125         }
126 }