]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Adapter/JITConfigAdapter.php
negative set
[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                         $value = $config['v'];
38
39                         if (isset($value) && $value !== '') {
40                                 $return[$key] = $value;
41                                 $this->in_db[$cat][$key] = true;
42                         }
43                 }
44                 DBA::close($configs);
45
46                 return [$cat => $config];
47         }
48
49         /**
50          * {@inheritdoc}
51          */
52         public function get($cat, $key)
53         {
54                 if (!$this->isConnected()) {
55                         return '!<unset>!';
56                 }
57
58                 $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
59                 if (DBA::isResult($config)) {
60                         // manage array value
61                         $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
62
63                         if (isset($value) && $value !== '') {
64                                 $this->in_db[$cat][$key] = true;
65                                 return $value;
66                         }
67                 }
68
69                 $this->in_db[$cat][$key] = false;
70                 return '!<unset>!';
71         }
72
73         /**
74          * {@inheritdoc}
75          */
76         public function set($cat, $key, $value)
77         {
78                 if (!$this->isConnected()) {
79                         return false;
80                 }
81
82                 // We store our setting values in a string variable.
83                 // So we have to do the conversion here so that the compare below works.
84                 // The exception are array values.
85                 $dbvalue = (!is_array($value) ? (string)$value : $value);
86
87                 $stored = $this->get($cat, $key);
88
89                 if (!isset($this->in_db[$cat])) {
90                         $this->in_db[$cat] = [];
91                 }
92                 if (!isset($this->in_db[$cat][$key])) {
93                         $this->in_db[$cat][$key] = false;
94                 }
95
96                 if (($stored === $dbvalue) && $this->in_db[$cat][$key]) {
97                         return true;
98                 }
99
100                 // manage array value
101                 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
102
103                 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
104
105                 $this->in_db[$cat][$key] = $result;
106
107                 return $result;
108         }
109
110         /**
111          * {@inheritdoc}
112          */
113         public function delete($cat, $key)
114         {
115                 if (!$this->isConnected()) {
116                         return false;
117                 }
118
119                 if (isset($this->cache[$cat][$key])) {
120                         unset($this->in_db[$cat][$key]);
121                 }
122
123                 $result = DBA::delete('config', ['cat' => $cat, 'k' => $key]);
124
125                 return $result;
126         }
127
128         /**
129          * {@inheritdoc}
130          */
131         public function isLoaded($cat, $key)
132         {
133                 if (!$this->isConnected()) {
134                         return false;
135                 }
136
137                 return (isset($this->in_db[$cat][$key])) && $this->in_db[$cat][$key];
138         }
139 }