]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Repository/Config.php
Refactoring Core class structures ...
[friendica.git] / src / Core / Config / Repository / 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\Repository;
23
24 use Friendica\Core\Config\Exception\ConfigPersistenceException;
25 use Friendica\Core\Config\Util\ValueConversion;
26 use Friendica\Database\Database;
27
28 /**
29  * The Config Repository, which is using the general DB-model backend for configs
30  */
31 class Config
32 {
33         /** @var Database */
34         protected $db;
35
36         public function __construct(Database $db)
37         {
38                 $this->db = $db;
39         }
40
41         protected static $table_name = 'config';
42
43         /**
44          * Checks if the model is currently connected
45          *
46          * @return bool
47          */
48         public function isConnected(): bool
49         {
50                 return $this->db->isConnected();
51         }
52
53         /**
54          * Loads all configuration values and returns the loaded category as an array.
55          *
56          * @param string|null $cat The category of the configuration values to load
57          *
58          * @return array The config array
59          *
60          * @throws ConfigPersistenceException In case the persistence layer throws errors
61          */
62         public function load(?string $cat = null): array
63         {
64                 $return = [];
65
66                 try {
67                         if (empty($cat)) {
68                                 $configs = $this->db->select(static::$table_name, ['cat', 'v', 'k']);
69                         } else {
70                                 $configs = $this->db->select(static::$table_name, ['cat', 'v', 'k'], ['cat' => $cat]);
71                         }
72
73                         while ($config = $this->db->fetch($configs)) {
74                                 $key   = $config['k'];
75                                 $value = ValueConversion::toConfigValue($config['v']);
76
77                                 // just save it in case it is set
78                                 if (isset($value)) {
79                                         $return[$config['cat']][$key] = $value;
80                                 }
81                         }
82                 } catch (\Exception $exception) {
83                         throw new ConfigPersistenceException(sprintf('Cannot load config category %s', $cat), $exception);
84                 } finally {
85                         $this->db->close($configs);
86                 }
87
88                 return $return;
89         }
90
91         /**
92          * Get a particular, system-wide config variable out of the DB with the
93          * given category name ($cat) and a key ($key).
94          *
95          * Note: Boolean variables are defined as 0/1 in the database
96          *
97          * @param string $cat The category of the configuration value
98          * @param string $key The configuration key to query
99          *
100          * @return array|string|null Stored value or null if it does not exist
101          *
102          * @throws ConfigPersistenceException In case the persistence layer throws errors
103          */
104         public function get(string $cat, string $key)
105         {
106                 if (!$this->isConnected()) {
107                         return null;
108                 }
109
110                 try {
111                         $config = $this->db->selectFirst(static::$table_name, ['v'], ['cat' => $cat, 'k' => $key]);
112                         if ($this->db->isResult($config)) {
113                                 $value = ValueConversion::toConfigValue($config['v']);
114
115                                 // just return it in case it is set
116                                 if (isset($value)) {
117                                         return $value;
118                                 }
119                         }
120                 } catch (\Exception $exception) {
121                         throw new ConfigPersistenceException(sprintf('Cannot get config with category %s and key %s', $cat, $key), $exception);
122                 }
123
124                 return null;
125         }
126
127         /**
128          * Stores a config value ($value) in the category ($cat) under the key ($key).
129          *
130          * Note: Please do not store booleans - convert to 0/1 integer values!
131          *
132          * @param string $cat   The category of the configuration value
133          * @param string $key   The configuration key to set
134          * @param mixed  $value The value to store
135          *
136          * @return bool Operation success
137          *
138          * @throws ConfigPersistenceException In case the persistence layer throws errors
139          */
140         public function set(string $cat, string $key, $value): bool
141         {
142                 if (!$this->isConnected()) {
143                         return false;
144                 }
145
146                 // We store our setting values in a string variable.
147                 // So we have to do the conversion here so that the compare below works.
148                 // The exception are array values.
149                 $compare_value = (!is_array($value) ? (string)$value : $value);
150                 $stored_value  = $this->get($cat, $key);
151
152                 if (isset($stored_value) && ($stored_value === $compare_value)) {
153                         return true;
154                 }
155
156                 $dbValue = ValueConversion::toDbValue($value);
157
158                 try {
159                         return $this->db->update(static::$table_name, ['v' => $dbValue], ['cat' => $cat, 'k' => $key], true);
160                 } catch (\Exception $exception) {
161                         throw new ConfigPersistenceException(sprintf('Cannot set config with category %s and key %s', $cat, $key), $exception);
162                 }
163         }
164
165         /**
166          * Removes the configured value from the database.
167          *
168          * @param string $cat The category of the configuration value
169          * @param string $key The configuration key to delete
170          *
171          * @return bool Operation success
172          *
173          * @throws ConfigPersistenceException In case the persistence layer throws errors
174          */
175         public function delete(string $cat, string $key): bool
176         {
177                 if (!$this->isConnected()) {
178                         return false;
179                 }
180
181                 try {
182                         return $this->db->delete(static::$table_name, ['cat' => $cat, 'k' => $key]);
183                 } catch (\Exception $exception) {
184                         throw new ConfigPersistenceException(sprintf('Cannot delete config with category %s and key %s', $cat, $key), $exception);
185                 }
186         }
187 }