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