]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/PreloadConfigAdapter.php
Merge pull request #6638 from Ixiter/develop-markdown-anchors
[friendica.git] / src / Core / Config / PreloadConfigAdapter.php
1 <?php
2
3 namespace Friendica\Core\Config;
4
5 use Exception;
6 use Friendica\Database\DBA;
7
8 /**
9  * Preload Configuration Adapter
10  *
11  * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
12  *
13  * @author Hypolite Petovan <hypolite@mrpetovan.com>
14  */
15 class PreloadConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter
16 {
17         private $config_loaded = false;
18
19         /**
20          * @var IConfigCache The config cache of this driver
21          */
22         private $configCache;
23
24         /**
25          * @param IConfigCache $configCache The config cache of this driver
26          */
27         public function __construct(IConfigCache $configCache)
28         {
29                 $this->configCache = $configCache;
30                 $this->connected = DBA::connected();
31                 $this->load();
32         }
33
34         /**
35          * {@inheritdoc}
36          */
37         public function load($family = 'config')
38         {
39                 if (!$this->isConnected()) {
40                         return;
41                 }
42
43                 if ($this->config_loaded) {
44                         return;
45                 }
46
47                 $configs = DBA::select('config', ['cat', 'v', 'k']);
48                 while ($config = DBA::fetch($configs)) {
49                         $this->configCache->set($config['cat'], $config['k'], $config['v']);
50                 }
51                 DBA::close($configs);
52
53                 $this->config_loaded = true;
54         }
55
56         /**
57          * {@inheritdoc}
58          */
59         public function get($cat, $k, $default_value = null, $refresh = false)
60         {
61                 if (!$this->isConnected()) {
62                         return $default_value;
63                 }
64
65                 if ($refresh) {
66                         $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
67                         if (DBA::isResult($config)) {
68                                 $this->configCache->set($cat, $k, $config['v']);
69                         }
70                 }
71
72                 $return = $this->configCache->get($cat, $k, $default_value);
73
74                 return $return;
75         }
76
77         /**
78          * {@inheritdoc}
79          */
80         public function set($cat, $k, $value)
81         {
82                 if (!$this->isConnected()) {
83                         return false;
84                 }
85
86                 // We store our setting values as strings.
87                 // So we have to do the conversion here so that the compare below works.
88                 // The exception are array values.
89                 $compare_value = !is_array($value) ? (string)$value : $value;
90
91                 if ($this->configCache->get($cat, $k) === $compare_value) {
92                         return true;
93                 }
94
95                 $this->configCache->set($cat, $k, $value);
96
97                 // manage array value
98                 $dbvalue = is_array($value) ? serialize($value) : $value;
99
100                 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
101                 if (!$result) {
102                         throw new Exception('Unable to store config value in [' . $cat . '][' . $k . ']');
103                 }
104
105                 return true;
106         }
107
108         /**
109          * {@inheritdoc}
110          */
111         public function delete($cat, $k)
112         {
113                 if (!$this->isConnected()) {
114                         return false;
115                 }
116
117                 $this->configCache->delete($cat, $k);
118
119                 $result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);
120
121                 return $result;
122         }
123 }