]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Adapter/PreloadConfigAdapter.php
fixing tests and preload config
[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)) {
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 null;
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)) {
62                                 return $value;
63                         }
64                 }
65
66                 return null;
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                 return DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
91         }
92
93         /**
94          * {@inheritdoc}
95          */
96         public function delete($cat, $key)
97         {
98                 if (!$this->isConnected()) {
99                         return false;
100                 }
101
102                 return DBA::delete('config', ['cat' => $cat, 'k' => $key]);
103         }
104
105         /**
106          * {@inheritdoc}
107          */
108         public function isLoaded($cat, $key)
109         {
110                 if (!$this->isConnected()) {
111                         return false;
112                 }
113
114                 return $this->config_loaded;
115         }
116 }