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