]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/JITConfigAdapter.php
Merge pull request #6598 from annando/worker-index
[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 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         }
30
31         /**
32          * {@inheritdoc}
33          */
34         public function load($cat = "config")
35         {
36                 // We don't preload "system" anymore.
37                 // This reduces the number of database reads a lot.
38                 if ($cat === 'system') {
39                         return;
40                 }
41
42                 $configs = DBA::select('config', ['v', 'k'], ['cat' => $cat]);
43                 while ($config = DBA::fetch($configs)) {
44                         $k = $config['k'];
45
46                         $this->configCache->set($cat, $k, $config['v']);
47
48                         if ($cat !== 'config') {
49                                 $this->cache[$cat][$k] = $config['v'];
50                                 $this->in_db[$cat][$k] = true;
51                         }
52                 }
53                 DBA::close($configs);
54         }
55
56         /**
57          * {@inheritdoc}
58          */
59         public function get($cat, $k, $default_value = null, $refresh = false)
60         {
61                 if (!$refresh) {
62                         // Do we have the cached value? Then return it
63                         if (isset($this->cache[$cat][$k])) {
64                                 if ($this->cache[$cat][$k] === '!<unset>!') {
65                                         return $default_value;
66                                 } else {
67                                         return $this->cache[$cat][$k];
68                                 }
69                         }
70                 }
71
72                 $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
73                 if (DBA::isResult($config)) {
74                         // manage array value
75                         $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
76
77                         // Assign the value from the database to the cache
78                         $this->cache[$cat][$k] = $value;
79                         $this->in_db[$cat][$k] = true;
80                         return $value;
81                 } elseif ($this->configCache->get($cat, $k) !== null) {
82                         // Assign the value (mostly) from config/local.config.php file to the cache
83                         $this->cache[$cat][$k] = $this->configCache->get($cat, $k);
84                         $this->in_db[$cat][$k] = false;
85
86                         return $this->configCache->get($cat, $k);
87                 } elseif ($this->configCache->get('config', $k) !== null) {
88                         // Assign the value (mostly) from config/local.config.php file to the cache
89                         $this->cache[$k] = $this->configCache->get('config', $k);
90                         $this->in_db[$k] = false;
91
92                         return $this->configCache->get('config', $k);
93                 }
94
95                 $this->cache[$cat][$k] = '!<unset>!';
96                 $this->in_db[$cat][$k] = false;
97
98                 return $default_value;
99         }
100
101         /**
102          * {@inheritdoc}
103          */
104         public function set($cat, $k, $value)
105         {
106                 // We store our setting values in a string variable.
107                 // So we have to do the conversion here so that the compare below works.
108                 // The exception are array values.
109                 $dbvalue = (!is_array($value) ? (string)$value : $value);
110
111                 $stored = $this->get($cat, $k, null, true);
112
113                 if (!isset($this->in_db[$cat])) {
114                         $this->in_db[$cat] = [];
115                 }
116                 if (!isset($this->in_db[$cat][$k])) {
117                         $this->in_db[$cat] = false;
118                 }
119
120                 if (($stored === $dbvalue) && $this->in_db[$cat][$k]) {
121                         return true;
122                 }
123
124                 $this->configCache->set($cat, $k, $value);
125
126                 // Assign the just added value to the cache
127                 $this->cache[$cat][$k] = $dbvalue;
128
129                 // manage array value
130                 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
131
132                 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
133
134                 if ($result) {
135                         $this->in_db[$cat][$k] = true;
136                 }
137
138                 return $result;
139         }
140
141         /**
142          * {@inheritdoc}
143          */
144         public function delete($cat, $k)
145         {
146                 if (isset($this->cache[$cat][$k])) {
147                         unset($this->cache[$cat][$k]);
148                         unset($this->in_db[$cat][$k]);
149                 }
150
151                 $result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);
152
153                 return $result;
154         }
155 }