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