]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Adapter/JITConfigAdapter.php
Merge branch 'master' into develop
[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                         $value = $this->toConfigValue($config['v']);
71
72                         // just return it in case it is set
73                         if (isset($value)) {
74                                 return $value;
75                         }
76                 }
77
78                 return null;
79         }
80
81         /**
82          * {@inheritdoc}
83          */
84         public function set($cat, $key, $value)
85         {
86                 if (!$this->isConnected()) {
87                         return false;
88                 }
89
90                 // We store our setting values in a string variable.
91                 // So we have to do the conversion here so that the compare below works.
92                 // The exception are array values.
93                 $compare_value = (!is_array($value) ? (string)$value : $value);
94                 $stored_value = $this->get($cat, $key, false);
95
96                 if (!isset($this->in_db[$cat])) {
97                         $this->in_db[$cat] = [];
98                 }
99                 if (!isset($this->in_db[$cat][$key])) {
100                         $this->in_db[$cat][$key] = false;
101                 }
102
103                 if (isset($stored_value) && ($stored_value === $compare_value) && $this->in_db[$cat][$key]) {
104                         return true;
105                 }
106
107                 $dbvalue = $this->toDbValue($value);
108
109                 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
110
111                 $this->in_db[$cat][$key] = $result;
112
113                 return $result;
114         }
115
116         /**
117          * {@inheritdoc}
118          */
119         public function delete($cat, $key)
120         {
121                 if (!$this->isConnected()) {
122                         return false;
123                 }
124
125                 if (isset($this->cache[$cat][$key])) {
126                         unset($this->in_db[$cat][$key]);
127                 }
128
129                 $result = DBA::delete('config', ['cat' => $cat, 'k' => $key]);
130
131                 return $result;
132         }
133
134         /**
135          * {@inheritdoc}
136          */
137         public function isLoaded($cat, $key)
138         {
139                 if (!$this->isConnected()) {
140                         return false;
141                 }
142
143                 return (isset($this->in_db[$cat][$key])) && $this->in_db[$cat][$key];
144         }
145 }