]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Adapter/JITConfigAdapter.php
Merge pull request #6683 from rabuzarus/20190217_-_fix_magicLinks_mentions
[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
38                         $return[$key] = $config['v'];
39                         $this->in_db[$cat][$key] = true;
40                 }
41                 DBA::close($configs);
42
43                 return [$cat => $config];
44         }
45
46         /**
47          * {@inheritdoc}
48          */
49         public function get($cat, $key)
50         {
51                 if (!$this->isConnected()) {
52                         return '!<unset>!';
53                 }
54
55                 $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
56                 if (DBA::isResult($config)) {
57                         // manage array value
58                         $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
59
60                         if (isset($value) && $value !== '') {
61                                 $this->in_db[$cat][$key] = true;
62                                 return $value;
63                         }
64                 }
65
66                 $this->in_db[$cat][$key] = false;
67                 return '!<unset>!';
68         }
69
70         /**
71          * {@inheritdoc}
72          */
73         public function set($cat, $key, $value)
74         {
75                 if (!$this->isConnected()) {
76                         return false;
77                 }
78
79                 // We store our setting values in a string variable.
80                 // So we have to do the conversion here so that the compare below works.
81                 // The exception are array values.
82                 $dbvalue = (!is_array($value) ? (string)$value : $value);
83
84                 $stored = $this->get($cat, $key);
85
86                 if (!isset($this->in_db[$cat])) {
87                         $this->in_db[$cat] = [];
88                 }
89                 if (!isset($this->in_db[$cat][$key])) {
90                         $this->in_db[$cat][$key] = false;
91                 }
92
93                 if (($stored === $dbvalue) && $this->in_db[$cat][$key]) {
94                         return true;
95                 }
96
97                 // manage array value
98                 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
99
100                 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
101
102                 $this->in_db[$cat][$key] = $result;
103
104                 return $result;
105         }
106
107         /**
108          * {@inheritdoc}
109          */
110         public function delete($cat, $key)
111         {
112                 if (!$this->isConnected()) {
113                         return false;
114                 }
115
116                 if (isset($this->cache[$cat][$key])) {
117                         unset($this->in_db[$cat][$key]);
118                 }
119
120                 $result = DBA::delete('config', ['cat' => $cat, 'k' => $key]);
121
122                 return $result;
123         }
124
125         /**
126          * {@inheritdoc}
127          */
128         public function isLoaded($cat, $key)
129         {
130                 if (!$this->isConnected()) {
131                         return false;
132                 }
133
134                 return (isset($this->in_db[$cat][$key])) && $this->in_db[$cat][$key];
135         }
136 }