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