]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/PreloadConfigAdapter.php
Merge pull request #6605 from nupplaphil/config_mock
[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->load();
31         }
32
33         /**
34          * {@inheritdoc}
35          */
36         public function load($family = 'config')
37         {
38                 if (!$this->isConnected()) {
39                         return;
40                 }
41
42                 if ($this->config_loaded) {
43                         return;
44                 }
45
46                 $configs = DBA::select('config', ['cat', 'v', 'k']);
47                 while ($config = DBA::fetch($configs)) {
48                         $this->configCache->set($config['cat'], $config['k'], $config['v']);
49                 }
50                 DBA::close($configs);
51
52                 $this->config_loaded = true;
53         }
54
55         /**
56          * {@inheritdoc}
57          */
58         public function get($cat, $k, $default_value = null, $refresh = false)
59         {
60                 if (!$this->isConnected()) {
61                         return $default_value;
62                 }
63
64                 if ($refresh) {
65                         $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
66                         if (DBA::isResult($config)) {
67                                 $this->configCache->set($cat, $k, $config['v']);
68                         }
69                 }
70
71                 $return = $this->configCache->get($cat, $k, $default_value);
72
73                 return $return;
74         }
75
76         /**
77          * {@inheritdoc}
78          */
79         public function set($cat, $k, $value)
80         {
81                 if (!$this->isConnected()) {
82                         return false;
83                 }
84
85                 // We store our setting values as strings.
86                 // So we have to do the conversion here so that the compare below works.
87                 // The exception are array values.
88                 $compare_value = !is_array($value) ? (string)$value : $value;
89
90                 if ($this->configCache->get($cat, $k) === $compare_value) {
91                         return true;
92                 }
93
94                 $this->configCache->set($cat, $k, $value);
95
96                 // manage array value
97                 $dbvalue = is_array($value) ? serialize($value) : $value;
98
99                 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
100                 if (!$result) {
101                         throw new Exception('Unable to store config value in [' . $cat . '][' . $k . ']');
102                 }
103
104                 return true;
105         }
106
107         /**
108          * {@inheritdoc}
109          */
110         public function delete($cat, $k)
111         {
112                 if (!$this->isConnected()) {
113                         return false;
114                 }
115
116                 $this->configCache->delete($cat, $k);
117
118                 $result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);
119
120                 return $result;
121         }
122 }