]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Adapter/JITPConfigAdapter.php
Improve & fixing Tests
[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          * @param bool $mark if true, mark the selection of the current cat/key pair
54          */
55         public function get($uid, $cat, $key, $mark = true)
56         {
57                 if (!$this->isConnected()) {
58                         return null;
59                 }
60
61                 // The value was in the db, so don't check it again (unless you have to)
62                 if ($mark) {
63                         $this->in_db[$uid][$cat][$key] = true;
64                 }
65
66                 $pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
67                 if (DBA::isResult($pconfig)) {
68                         $value = $this->toConfigValue($pconfig['v']);
69
70                         if (isset($value)) {
71                                 return $value;
72                         }
73                 }
74
75                 $this->in_db[$uid][$cat][$key] = false;
76                 return null;
77         }
78
79         /**
80          * {@inheritdoc}
81          */
82         public function set($uid, $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                 $compare_value = (!is_array($value) ? (string)$value : $value);
92                 $stored_value = $this->get($uid, $cat, $key, false);
93
94                 if (!isset($this->in_db[$uid])) {
95                         $this->in_db[$uid] = [];
96                 }
97                 if (!isset($this->in_db[$uid][$cat])) {
98                         $this->in_db[$uid][$cat] = [];
99                 }
100                 if (!isset($this->in_db[$uid][$cat][$key])) {
101                         $this->in_db[$uid][$cat][$key] = false;
102                 }
103
104                 if (isset($stored_value) && ($stored_value === $compare_value) && $this->in_db[$uid][$cat][$key]) {
105                         return true;
106                 }
107
108                 // manage array value
109                 $dbvalue = (is_array($value) ? serialize($value) : $value);
110
111                 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
112
113                 $this->in_db[$uid][$cat][$key] = $result;
114
115                 return $result;
116         }
117
118         /**
119          * {@inheritdoc}
120          */
121         public function delete($uid, $cat, $key)
122         {
123                 if (!$this->isConnected()) {
124                         return false;
125                 }
126
127                 if (isset($this->in_db[$uid][$cat][$key])) {
128                         unset($this->in_db[$uid][$cat][$key]);
129                 }
130
131                 return DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
132         }
133
134         /**
135          * {@inheritdoc}
136          */
137         public function isLoaded($uid, $cat, $key)
138         {
139                 if (!$this->isConnected()) {
140                         return false;
141                 }
142
143                 return (isset($this->in_db[$uid][$cat][$key])) && $this->in_db[$uid][$cat][$key];
144         }
145 }