]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/PreloadConfigAdapter.php
ec68665dc46d1031efffa6287c360d54085cf865
[friendica.git] / src / Core / Config / PreloadConfigAdapter.php
1 <?php
2
3 namespace Friendica\Core\Config;
4
5 use Exception;
6 use Friendica\BaseObject;
7 use Friendica\Database\DBA;
8 use Friendica\Database\DBM;
9
10 require_once 'include/dba.php';
11
12 /**
13  * Preload Configuration Adapter
14  *
15  * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
16  *
17  * @author Hypolite Petovan <mrpetovan@gmail.com>
18  */
19 class PreloadConfigAdapter extends BaseObject implements IConfigAdapter
20 {
21         private $config_loaded = false;
22
23         public function __construct()
24         {
25                 $this->load();
26         }
27
28         public function load($family = 'config')
29         {
30                 if ($this->config_loaded) {
31                         return;
32                 }
33
34                 $configs = DBA::select('config', ['cat', 'v', 'k']);
35                 while ($config = DBA::fetch($configs)) {
36                         self::getApp()->setConfigValue($config['cat'], $config['k'], $config['v']);
37                 }
38                 DBA::close($configs);
39
40                 $this->config_loaded = true;
41         }
42
43         public function get($cat, $k, $default_value = null, $refresh = false)
44         {
45                 if ($refresh) {
46                         $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
47                         if (DBM::is_result($config)) {
48                                 self::getApp()->setConfigValue($cat, $k, $config['v']);
49                         }
50                 }
51
52                 $return = self::getApp()->getConfigValue($cat, $k, $default_value);
53
54                 return $return;
55         }
56
57         public function set($cat, $k, $value)
58         {
59                 // We store our setting values as strings.
60                 // So we have to do the conversion here so that the compare below works.
61                 // The exception are array values.
62                 $compare_value = !is_array($value) ? (string)$value : $value;
63
64                 if (self::getApp()->getConfigValue($cat, $k) === $compare_value) {
65                         return true;
66                 }
67
68                 self::getApp()->setConfigValue($cat, $k, $value);
69
70                 // manage array value
71                 $dbvalue = is_array($value) ? serialize($value) : $value;
72
73                 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
74                 if (!$result) {
75                         throw new Exception('Unable to store config value in [' . $cat . '][' . $k . ']');
76                 }
77
78                 return true;
79         }
80
81         public function delete($cat, $k)
82         {
83                 self::getApp()->deleteConfigValue($cat, $k);
84
85                 $result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);
86
87                 return $result;
88         }
89 }