]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Adapter/JITConfigAdapter.php
Config FollowUp
[friendica.git] / src / Core / Config / Adapter / JITConfigAdapter.php
1 <?php
2 namespace Friendica\Core\Config\Adapter;
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 $in_db;
16
17         /**
18          * {@inheritdoc}
19          */
20         public function load($cat = "config")
21         {
22                 $return = [];
23
24                 if (!$this->isConnected()) {
25                         return $return;
26                 }
27
28                 // We don't preload "system" anymore.
29                 // This reduces the number of database reads a lot.
30                 if ($cat === 'system') {
31                         return $return;
32                 }
33
34                 $configs = DBA::select('config', ['v', 'k'], ['cat' => $cat]);
35                 while ($config = DBA::fetch($configs)) {
36                         $key = $config['k'];
37
38                         $return[$key] = $config['v'];
39                         $this->in_db[$cat][$key] = true;
40                 }
41                 DBA::close($configs);
42
43                 return [$cat => $config];
44         }
45
46         /**
47          * {@inheritdoc}
48          */
49         public function get($cat, $key)
50         {
51                 if (!$this->isConnected()) {
52                         return '!<unset>!';
53                 }
54
55                 $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
56                 if (DBA::isResult($config)) {
57                         // manage array value
58                         $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
59
60                         $this->in_db[$cat][$key] = true;
61                         return $value;
62                 } else {
63
64                         $this->in_db[$cat][$key] = false;
65                         return '!<unset>!';
66                 }
67         }
68
69         /**
70          * {@inheritdoc}
71          */
72         public function set($cat, $key, $value)
73         {
74                 if (!$this->isConnected()) {
75                         return false;
76                 }
77
78                 // We store our setting values in a string variable.
79                 // So we have to do the conversion here so that the compare below works.
80                 // The exception are array values.
81                 $dbvalue = (!is_array($value) ? (string)$value : $value);
82
83                 $stored = $this->get($cat, $key);
84
85                 if (!isset($this->in_db[$cat])) {
86                         $this->in_db[$cat] = [];
87                 }
88                 if (!isset($this->in_db[$cat][$key])) {
89                         $this->in_db[$cat][$key] = false;
90                 }
91
92                 if (($stored === $dbvalue) && $this->in_db[$cat][$key]) {
93                         return true;
94                 }
95
96                 // manage array value
97                 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
98
99                 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
100
101                 $this->in_db[$cat][$key] = $result;
102
103                 return $result;
104         }
105
106         /**
107          * {@inheritdoc}
108          */
109         public function delete($cat, $key)
110         {
111                 if (!$this->isConnected()) {
112                         return false;
113                 }
114
115                 if (isset($this->cache[$cat][$key])) {
116                         unset($this->in_db[$cat][$key]);
117                 }
118
119                 $result = DBA::delete('config', ['cat' => $cat, 'k' => $key]);
120
121                 return $result;
122         }
123 }