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