]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Model/Config.php
Restructure (P)Config to follow new paradigm
[friendica.git] / src / Core / Config / Model / Config.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Core\Config\Model;
23
24 use Friendica\Database\Database;
25
26 /**
27  * The Config model backend, which is using the general DB-model backend for configs
28  */
29 class Config
30 {
31         /** @var Database */
32         protected $dba;
33
34         /**
35          * @param Database $dba The database connection of this model
36          */
37         public function __construct(Database $dba)
38         {
39                 $this->dba = $dba;
40         }
41
42         /**
43          * Checks if the model is currently connected
44          *
45          * @return bool
46          */
47         public function isConnected()
48         {
49                 return $this->dba->isConnected();
50         }
51
52         /**
53          * Loads all configuration values and returns the loaded category as an array.
54          *
55          * @param string|null $cat The category of the configuration values to load
56          *
57          * @return array The config array
58          *
59          * @throws \Exception In case DB calls are invalid
60          */
61         public function load(string $cat = null)
62         {
63                 $return = [];
64
65                 if (empty($cat)) {
66                         $configs = $this->dba->select('config', ['cat', 'v', 'k']);
67                 } else {
68                         $configs = $this->dba->select('config', ['cat', 'v', 'k'], ['cat' => $cat]);
69                 }
70
71                 while ($config = $this->dba->fetch($configs)) {
72
73                         $key   = $config['k'];
74                         $value = DbaUtils::toConfigValue($config['v']);
75
76                         // just save it in case it is set
77                         if (isset($value)) {
78                                 $return[$config['cat']][$key] = $value;
79                         }
80                 }
81                 $this->dba->close($configs);
82
83                 return $return;
84         }
85
86         /**
87          * Get a particular, system-wide config variable out of the DB with the
88          * given category name ($cat) and a key ($key).
89          *
90          * Note: Boolean variables are defined as 0/1 in the database
91          *
92          * @param string $cat The category of the configuration value
93          * @param string $key The configuration key to query
94          *
95          * @return array|string|null Stored value or null if it does not exist
96          *
97          * @throws \Exception In case DB calls are invalid
98          */
99         public function get(string $cat, string $key)
100         {
101                 if (!$this->isConnected()) {
102                         return null;
103                 }
104
105                 $config = $this->dba->selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
106                 if ($this->dba->isResult($config)) {
107                         $value = DbaUtils::toConfigValue($config['v']);
108
109                         // just return it in case it is set
110                         if (isset($value)) {
111                                 return $value;
112                         }
113                 }
114
115                 return null;
116         }
117
118         /**
119          * Stores a config value ($value) in the category ($cat) under the key ($key).
120          *
121          * Note: Please do not store booleans - convert to 0/1 integer values!
122          *
123          * @param string $cat   The category of the configuration value
124          * @param string $key   The configuration key to set
125          * @param mixed  $value The value to store
126          *
127          * @return bool Operation success
128          *
129          * @throws \Exception In case DB calls are invalid
130          */
131         public function set(string $cat, string $key, $value)
132         {
133                 if (!$this->isConnected()) {
134                         return false;
135                 }
136
137                 // We store our setting values in a string variable.
138                 // So we have to do the conversion here so that the compare below works.
139                 // The exception are array values.
140                 $compare_value = (!is_array($value) ? (string)$value : $value);
141                 $stored_value  = $this->get($cat, $key);
142
143                 if (isset($stored_value) && ($stored_value === $compare_value)) {
144                         return true;
145                 }
146
147                 $dbvalue = DbaUtils::toDbValue($value);
148
149                 $result = $this->dba->update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
150
151                 return $result;
152         }
153
154         /**
155          * Removes the configured value from the database.
156          *
157          * @param string $cat The category of the configuration value
158          * @param string $key The configuration key to delete
159          *
160          * @return bool Operation success
161          *
162          * @throws \Exception In case DB calls are invalid
163          */
164         public function delete(string $cat, string $key)
165         {
166                 if (!$this->isConnected()) {
167                         return false;
168                 }
169
170                 return $this->dba->delete('config', ['cat' => $cat, 'k' => $key]);
171         }
172 }