]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/PreloadConfigAdapter.php
3) Introducing ConfigFactory
[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 implements IConfigAdapter
16 {
17         private $config_loaded = false;
18
19         /**
20          * @var IConfigCache The config cache of this driver
21          */
22         private $config;
23
24         /**
25          * @param IConfigCache $config The config cache of this driver
26          */
27         public function __construct($config)
28         {
29                 $this->config = $config;
30                 $this->load();
31         }
32
33         public function load($family = 'config')
34         {
35                 if ($this->config_loaded) {
36                         return;
37                 }
38
39                 $configs = DBA::select('config', ['cat', 'v', 'k']);
40                 while ($config = DBA::fetch($configs)) {
41                         $this->config->set($config['cat'], $config['k'], $config['v']);
42                 }
43                 DBA::close($configs);
44
45                 $this->config_loaded = true;
46         }
47
48         public function get($cat, $k, $default_value = null, $refresh = false)
49         {
50                 if ($refresh) {
51                         $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
52                         if (DBA::isResult($config)) {
53                                 $this->config->set($cat, $k, $config['v']);
54                         }
55                 }
56
57                 $return = $this->config->get($cat, $k, $default_value);
58
59                 return $return;
60         }
61
62         public function set($cat, $k, $value)
63         {
64                 // We store our setting values as strings.
65                 // So we have to do the conversion here so that the compare below works.
66                 // The exception are array values.
67                 $compare_value = !is_array($value) ? (string)$value : $value;
68
69                 if ($this->config->get($cat, $k) === $compare_value) {
70                         return true;
71                 }
72
73                 $this->config->set($cat, $k, $value);
74
75                 // manage array value
76                 $dbvalue = is_array($value) ? serialize($value) : $value;
77
78                 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
79                 if (!$result) {
80                         throw new Exception('Unable to store config value in [' . $cat . '][' . $k . ']');
81                 }
82
83                 return true;
84         }
85
86         public function delete($cat, $k)
87         {
88                 $this->config->delete($cat, $k);
89
90                 $result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);
91
92                 return $result;
93         }
94 }