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