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