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