]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Adapter/PreloadConfigAdapter.php
Merge remote-tracking branch 'upstream/develop' into ap-delivery-failure
[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 = $this->toConfigValue($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                         $value = $this->toConfigValue($config['v']);
59
60                         if (isset($value)) {
61                                 return $value;
62                         }
63                 }
64
65                 return null;
66         }
67
68         /**
69          * {@inheritdoc}
70          */
71         public function set($cat, $key, $value)
72         {
73                 if (!$this->isConnected()) {
74                         return false;
75                 }
76
77                 // We store our setting values as strings.
78                 // So we have to do the conversion here so that the compare below works.
79                 // The exception are array values.
80                 $compare_value = !is_array($value) ? (string)$value : $value;
81                 $stored_value = $this->get($cat, $key);
82
83                 if (isset($stored_value) && $stored_value === $compare_value) {
84                         return true;
85                 }
86
87                 $dbvalue = $this->toDbValue($value);
88
89                 return DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
90         }
91
92         /**
93          * {@inheritdoc}
94          */
95         public function delete($cat, $key)
96         {
97                 if (!$this->isConnected()) {
98                         return false;
99                 }
100
101                 return DBA::delete('config', ['cat' => $cat, 'k' => $key]);
102         }
103
104         /**
105          * {@inheritdoc}
106          */
107         public function isLoaded($cat, $key)
108         {
109                 if (!$this->isConnected()) {
110                         return false;
111                 }
112
113                 return $this->config_loaded;
114         }
115 }