]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Adapter/JITConfigAdapter.php
check if value is set
[friendica.git] / src / Core / Config / Adapter / JITConfigAdapter.php
1 <?php
2 namespace Friendica\Core\Config\Adapter;
3
4 use Friendica\Core\Logger;
5 use Friendica\Database\DBA;
6
7 /**
8  * JustInTime Configuration Adapter
9  *
10  * Default Config Adapter. Provides the best performance for pages loading few configuration variables.
11  *
12  * @author Hypolite Petovan <hypolite@mrpetovan.com>
13  */
14 class JITConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter
15 {
16         private $in_db;
17
18         /**
19          * {@inheritdoc}
20          */
21         public function load($cat = "config")
22         {
23                 $return = [];
24
25                 if (!$this->isConnected()) {
26                         return $return;
27                 }
28
29                 // We don't preload "system" anymore.
30                 // This reduces the number of database reads a lot.
31                 if ($cat === 'system') {
32                         return $return;
33                 }
34
35                 $configs = DBA::select('config', ['v', 'k'], ['cat' => $cat]);
36                 while ($config = DBA::fetch($configs)) {
37                         $key = $config['k'];
38
39                         $return[$key] = $config['v'];
40                         $this->in_db[$cat][$key] = true;
41                 }
42                 DBA::close($configs);
43
44                 return [$cat => $config];
45         }
46
47         /**
48          * {@inheritdoc}
49          */
50         public function get($cat, $key)
51         {
52                 if (!$this->isConnected()) {
53                         return '!<unset>!';
54                 }
55
56                 $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
57                 if (DBA::isResult($config)) {
58                         // manage array value
59                         $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
60
61                         if ($key === 'last_worker_execution') {
62                                 Logger::alert('catchmeifyou', ['store' => $value, 'in_db' => $this->in_db[$cat][$key]]);
63                         }
64
65                         if (isset($value) && $value !== '') {
66                                 $this->in_db[$cat][$key] = true;
67                                 return $value;
68                         } else {
69                                 $this->in_db[$cat][$key] = false;
70                                 return '!<unset>!';
71                         }
72                 } else {
73
74                         $this->in_db[$cat][$key] = false;
75                         return '!<unset>!';
76                 }
77         }
78
79         /**
80          * {@inheritdoc}
81          */
82         public function set($cat, $key, $value)
83         {
84                 if (!$this->isConnected()) {
85                         return false;
86                 }
87
88                 // We store our setting values in a string variable.
89                 // So we have to do the conversion here so that the compare below works.
90                 // The exception are array values.
91                 $dbvalue = (!is_array($value) ? (string)$value : $value);
92
93                 $stored = $this->get($cat, $key);
94
95                 if (!isset($this->in_db[$cat])) {
96                         $this->in_db[$cat] = [];
97                 }
98                 if (!isset($this->in_db[$cat][$key])) {
99                         $this->in_db[$cat][$key] = false;
100                 }
101
102                 if ($key === 'last_worker_execution') {
103                         Logger::alert('catchmeifyou', ['db' => $dbvalue, 'store' => $stored, 'in_db' => $this->in_db[$cat][$key]]);
104                 }
105
106                 if (($stored === $dbvalue) && $this->in_db[$cat][$key]) {
107                         return true;
108                 }
109
110                 // manage array value
111                 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
112
113                 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
114
115                 $this->in_db[$cat][$key] = $result;
116
117                 return $result;
118         }
119
120         /**
121          * {@inheritdoc}
122          */
123         public function delete($cat, $key)
124         {
125                 if (!$this->isConnected()) {
126                         return false;
127                 }
128
129                 if (isset($this->cache[$cat][$key])) {
130                         unset($this->in_db[$cat][$key]);
131                 }
132
133                 $result = DBA::delete('config', ['cat' => $cat, 'k' => $key]);
134
135                 return $result;
136         }
137
138         /**
139          * {@inheritdoc}
140          */
141         public function isLoaded($cat, $key)
142         {
143                 if (!$this->isConnected()) {
144                         return false;
145                 }
146
147                 return (isset($this->in_db[$cat][$key])) && $this->in_db[$cat][$key];
148         }
149 }