]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Adapter/PreloadConfigAdapter.php
Fixing value check for configuration
[friendica.git] / src / Core / Config / Adapter / PreloadConfigAdapter.php
1 <?php
2
3 namespace Friendica\Core\Config\Adapter;
4
5 use Friendica\Database\DBA;
6
7 /**
8  * Preload Configuration Adapter
9  *
10  * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
11  *
12  * @author Hypolite Petovan <hypolite@mrpetovan.com>
13  */
14 class PreloadConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter
15 {
16         private $config_loaded = false;
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                 if ($this->config_loaded) {
30                         return $return;
31                 }
32
33                 $configs = DBA::select('config', ['cat', 'v', 'k']);
34                 while ($config = DBA::fetch($configs)) {
35                         $return[$config['cat']][$config['k']] = $config['v'];
36                 }
37                 DBA::close($configs);
38
39                 $this->config_loaded = true;
40
41                 return $return;
42         }
43
44         /**
45          * {@inheritdoc}
46          */
47         public function get($cat, $key)
48         {
49                 if (!$this->isConnected()) {
50                         return '!<unset>!';
51                 }
52
53                 $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
54                 if (DBA::isResult($config)) {
55                         // manage array value
56                         $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
57
58                         if (isset($value) && $value !== '') {
59                                 return $value;
60                         }
61                 }
62
63                 return '!<unset>!';
64         }
65
66         /**
67          * {@inheritdoc}
68          */
69         public function set($cat, $key, $value)
70         {
71                 if (!$this->isConnected()) {
72                         return false;
73                 }
74
75                 // We store our setting values as strings.
76                 // So we have to do the conversion here so that the compare below works.
77                 // The exception are array values.
78                 $compare_value = !is_array($value) ? (string)$value : $value;
79
80                 if ($this->get($cat, $key) === $compare_value) {
81                         return true;
82                 }
83
84                 // manage array value
85                 $dbvalue = is_array($value) ? serialize($value) : $value;
86
87                 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
88
89                 return $result;
90         }
91
92         /**
93          * {@inheritdoc}
94          */
95         public function delete($cat, $key)
96         {
97                 if (!$this->isConnected()) {
98                         return false;
99                 }
100
101                 $result = DBA::delete('config', ['cat' => $cat, 'k' => $key]);
102
103                 return $result;
104         }
105
106         /**
107          * {@inheritdoc}
108          */
109         public function isLoaded($cat, $key)
110         {
111                 if (!$this->isConnected()) {
112                         return false;
113                 }
114
115                 return $this->config_loaded;
116         }
117 }