]> git.mxchange.org Git - friendica.git/blob - src/Model/Config/Config.php
Move Preload/JIT Configuration logic from Adapter to Core-Configuration
[friendica.git] / src / Model / Config / Config.php
1 <?php
2
3 namespace Friendica\Model\Config;
4
5
6 class Config extends DbaConfig
7 {
8         /**
9          * Loads all configuration values and returns the loaded category as an array.
10          *
11          * @param string $cat The category of the configuration values to load
12          *
13          * @return array
14          */
15         public function load(string $cat = null)
16         {
17                 $return = [];
18
19                 if (empty($cat)) {
20                         $configs = $this->dba->select('config', ['cat', 'v', 'k']);
21                 } else {
22                         $configs = $this->dba->select('config', ['cat', 'v', 'k'], ['cat' => $cat]);
23                 }
24
25                 while ($config = $this->dba->fetch($configs)) {
26
27                         $key   = $config['k'];
28                         $value = $this->toConfigValue($config['v']);
29
30                         // just save it in case it is set
31                         if (isset($value)) {
32                                 $return[$config['cat']][$key] = $value;
33                         }
34                 }
35                 $this->dba->close($configs);
36
37                 return $return;
38         }
39
40         /**
41          * Get a particular system-wide config variable given the category name
42          * ($cat) and a key ($key).
43          *
44          * Note: Boolean variables are defined as 0/1 in the database
45          *
46          * @param string $cat The category of the configuration value
47          * @param string $key The configuration key to query
48          *
49          * @return null|mixed Stored value or null if it does not exist
50          */
51         public function get(string $cat, string $key)
52         {
53                 if (!$this->isConnected()) {
54                         return null;
55                 }
56
57                 $config = $this->dba->selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
58                 if ($this->dba->isResult($config)) {
59                         $value = $this->toConfigValue($config['v']);
60
61                         // just return it in case it is set
62                         if (isset($value)) {
63                                 return $value;
64                         }
65                 }
66
67                 return null;
68         }
69
70         /**
71          * Stores a config value ($value) in the category ($cat) under the key ($key).
72          *
73          * Note: Please do not store booleans - convert to 0/1 integer values!
74          *
75          * @param string $cat   The category of the configuration value
76          * @param string $key   The configuration key to set
77          * @param mixed  $value The value to store
78          *
79          * @return bool Operation success
80          */
81         public function set(string $cat, string $key, $value)
82         {
83                 if (!$this->isConnected()) {
84                         return false;
85                 }
86
87                 // We store our setting values in a string variable.
88                 // So we have to do the conversion here so that the compare below works.
89                 // The exception are array values.
90                 $compare_value = (!is_array($value) ? (string)$value : $value);
91                 $stored_value  = $this->get($cat, $key);
92
93                 if (isset($stored_value) && ($stored_value === $compare_value)) {
94                         return true;
95                 }
96
97                 $dbvalue = $this->toDbValue($value);
98
99                 $result = $this->dba->update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
100
101                 return $result;
102         }
103
104         /**
105          * Removes the configured value from the stored cache
106          * and removes it from the database.
107          *
108          * @param string $cat The category of the configuration value
109          * @param string $key The configuration key to delete
110          *
111          * @return bool Operation success
112          */
113         public function delete(string $cat, string $key)
114         {
115                 if (!$this->isConnected()) {
116                         return false;
117                 }
118
119                 return $this->dba->delete('config', ['cat' => $cat, 'k' => $key]);
120         }
121 }