]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/JITConfigAdapter.php
Merge pull request #6209 from MrPetovan/task/move-config-to-php-array
[friendica.git] / src / Core / Config / JITConfigAdapter.php
1 <?php
2 namespace Friendica\Core\Config;
3
4 use Friendica\BaseObject;
5 use Friendica\Database\DBA;
6
7 /**
8  * JustInTime Configuration Adapter
9  *
10  * Default Config Adapter. Provides the best performance for pages loading few configuration variables.
11  *
12  * @author Hypolite Petovan <hypolite@mrpetovan.com>
13  */
14 class JITConfigAdapter extends BaseObject implements IConfigAdapter
15 {
16         private $cache;
17         private $in_db;
18
19         public function load($cat = "config")
20         {
21                 // We don't preload "system" anymore.
22                 // This reduces the number of database reads a lot.
23                 if ($cat === 'system') {
24                         return;
25                 }
26
27                 $configs = DBA::select('config', ['v', 'k'], ['cat' => $cat]);
28                 while ($config = DBA::fetch($configs)) {
29                         $k = $config['k'];
30
31                         self::getApp()->setConfigValue($cat, $k, $config['v']);
32
33                         if ($cat !== 'config') {
34                                 $this->cache[$cat][$k] = $config['v'];
35                                 $this->in_db[$cat][$k] = true;
36                         }
37                 }
38                 DBA::close($configs);
39         }
40
41         public function get($cat, $k, $default_value = null, $refresh = false)
42         {
43                 $a = self::getApp();
44
45                 if (!$refresh) {
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;
50                                 } else {
51                                         return $this->cache[$cat][$k];
52                                 }
53                         }
54                 }
55
56                 $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
57                 if (DBA::isResult($config)) {
58                         // manage array value
59                         $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
60
61                         // Assign the value from the database to the cache
62                         $this->cache[$cat][$k] = $value;
63                         $this->in_db[$cat][$k] = true;
64                         return $value;
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;
69
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;
75
76                         return $a->config[$k];
77                 }
78
79                 $this->cache[$cat][$k] = '!<unset>!';
80                 $this->in_db[$cat][$k] = false;
81
82                 return $default_value;
83         }
84
85         public function set($cat, $k, $value)
86         {
87                 $a = self::getApp();
88
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);
93
94                 $stored = $this->get($cat, $k, null, true);
95
96                 if (!isset($this->in_db[$cat])) {
97                         $this->in_db[$cat] = [];
98                 }
99                 if (!isset($this->in_db[$cat][$k])) {
100                         $this->in_db[$cat] = false;
101                 }
102
103                 if (($stored === $dbvalue) && $this->in_db[$cat][$k]) {
104                         return true;
105                 }
106
107                 self::getApp()->setConfigValue($cat, $k, $value);
108
109                 // Assign the just added value to the cache
110                 $this->cache[$cat][$k] = $dbvalue;
111
112                 // manage array value
113                 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
114
115                 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
116
117                 if ($result) {
118                         $this->in_db[$cat][$k] = true;
119                 }
120
121                 return $result;
122         }
123
124         public function delete($cat, $k)
125         {
126                 if (isset($this->cache[$cat][$k])) {
127                         unset($this->cache[$cat][$k]);
128                         unset($this->in_db[$cat][$k]);
129                 }
130
131                 $result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);
132
133                 return $result;
134         }
135 }