]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/JITPConfigAdapter.php
Merge pull request #6209 from MrPetovan/task/move-config-to-php-array
[friendica.git] / src / Core / Config / JITPConfigAdapter.php
1 <?php
2 namespace Friendica\Core\Config;
3
4 use Friendica\BaseObject;
5 use Friendica\Database\DBA;
6
7 /**
8  * JustInTime User Configuration Adapter
9  *
10  * Default PConfig Adapter. Provides the best performance for pages loading few configuration variables.
11  *
12  * @author Hypolite Petovan <hypolite@mrpetovan.com>
13  */
14 class JITPConfigAdapter extends BaseObject implements IPConfigAdapter
15 {
16         private $in_db;
17
18         public function load($uid, $cat)
19         {
20                 $a = self::getApp();
21
22                 $pconfigs = DBA::select('pconfig', ['v', 'k'], ['cat' => $cat, 'uid' => $uid]);
23                 if (DBA::isResult($pconfigs)) {
24                         while ($pconfig = DBA::fetch($pconfigs)) {
25                                 $k = $pconfig['k'];
26
27                                 self::getApp()->setPConfigValue($uid, $cat, $k, $pconfig['v']);
28
29                                 $this->in_db[$uid][$cat][$k] = true;
30                         }
31                 } else if ($cat != 'config') {
32                         // Negative caching
33                         $a->config[$uid][$cat] = "!<unset>!";
34                 }
35                 DBA::close($pconfigs);
36         }
37
38         public function get($uid, $cat, $k, $default_value = null, $refresh = false)
39         {
40                 $a = self::getApp();
41
42                 if (!$refresh) {
43                         // Looking if the whole family isn't set
44                         if (isset($a->config[$uid][$cat])) {
45                                 if ($a->config[$uid][$cat] === '!<unset>!') {
46                                         return $default_value;
47                                 }
48                         }
49
50                         if (isset($a->config[$uid][$cat][$k])) {
51                                 if ($a->config[$uid][$cat][$k] === '!<unset>!') {
52                                         return $default_value;
53                                 }
54                                 return $a->config[$uid][$cat][$k];
55                         }
56                 }
57
58                 $pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
59                 if (DBA::isResult($pconfig)) {
60                         $val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
61
62                         self::getApp()->setPConfigValue($uid, $cat, $k, $val);
63
64                         $this->in_db[$uid][$cat][$k] = true;
65
66                         return $val;
67                 } else {
68                         self::getApp()->setPConfigValue($uid, $cat, $k, '!<unset>!');
69
70                         $this->in_db[$uid][$cat][$k] = false;
71
72                         return $default_value;
73                 }
74         }
75
76         public function set($uid, $cat, $k, $value)
77         {
78                 // We store our setting values in a string variable.
79                 // So we have to do the conversion here so that the compare below works.
80                 // The exception are array values.
81                 $dbvalue = (!is_array($value) ? (string)$value : $value);
82
83                 $stored = $this->get($uid, $cat, $k, null, true);
84
85                 if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$k]) {
86                         return true;
87                 }
88
89                 self::getApp()->setPConfigValue($uid, $cat, $k, $value);
90
91                 // manage array value
92                 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
93
94                 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
95
96                 if ($result) {
97                         $this->in_db[$uid][$cat][$k] = true;
98                 }
99
100                 return $result;
101         }
102
103         public function delete($uid, $cat, $k)
104         {
105                 self::getApp()->deletePConfigValue($uid, $cat, $k);
106
107                 if (!empty($this->in_db[$uid][$cat][$k])) {
108                         unset($this->in_db[$uid][$cat][$k]);
109                 }
110
111                 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
112
113                 return $result;
114         }
115 }