]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Model/DatabaseConfig.php
Disable setting fields in case we use environment variables
[friendica.git] / src / Core / Config / Model / DatabaseConfig.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Core\Config\Capability\IManageConfigValues;
25 use Friendica\Core\Config\Capability\ISetConfigValuesTransactionally;
26 use Friendica\Core\Config\Util\SerializeUtil;
27 use Friendica\Core\Config\ValueObject\Cache;
28 use Friendica\Database\Database;
29
30 /**
31  * Complete system configuration model, bound with the database
32  */
33 class DatabaseConfig implements IManageConfigValues
34 {
35         /** @var Database */
36         protected $database;
37         /** @var Cache */
38         protected $cache;
39
40         public function __construct(Database $database, Cache $cache)
41         {
42                 $this->database = $database;
43                 $this->cache    = $cache;
44
45                 $this->reload();
46         }
47
48         /** {@inheritDoc} */
49         public function reload()
50         {
51                 $config = $this->database->selectToArray('config');
52
53                 foreach ($config as $entry) {
54                         $this->cache->set($entry['cat'], $entry['k'], SerializeUtil::maybeUnserialize($entry['v']), Cache::SOURCE_DATA);
55                 }
56         }
57
58         public function setAndSave(Cache $setCache, Cache $delCache): bool
59         {
60                 $this->database->transaction();
61
62                 foreach ($setCache->getAll() as $category => $data) {
63                         foreach ($data as $key => $value) {
64                                 $this->set($category, $key, $value);
65                         }
66                 }
67
68                 foreach ($delCache->getAll() as $category => $keys) {
69                         foreach ($keys as $key => $value) {
70                                 $this->delete($category, $key);
71                         }
72                 }
73
74                 return $this->database->commit();
75         }
76
77         /** {@inheritDoc} */
78         public function get(string $cat, string $key = null, $default_value = null)
79         {
80                 return $this->cache->get($cat, $key) ?? $default_value;
81         }
82
83         /** {@inheritDoc} */
84         public function isSetDisabled(string $cat, string $key): bool
85         {
86                 return $this->cache->getSource($cat, $key) >= Cache::SOURCE_ENV;
87         }
88
89         /** {@inheritDoc} */
90         public function set(string $cat, string $key, $value): bool
91         {
92                 // In case someone or something already serialized a config entry, unserialize it first
93                 // We serialize values just once
94                 $value = SerializeUtil::maybeUnserialize($value);
95
96                 $this->cache->set($cat, $key, $value, Cache::SOURCE_DATA);
97                 return $this->database->insert('config', ['cat' => $cat, 'k' => $key, 'v' => serialize($value)], Database::INSERT_UPDATE);
98         }
99
100         /** {@inheritDoc} */
101         public function beginTransaction(): ISetConfigValuesTransactionally
102         {
103                 return new ConfigTransaction($this);
104         }
105
106         /** {@inheritDoc} */
107         public function delete(string $cat, string $key): bool
108         {
109                 $this->cache->delete($cat, $key);
110                 return $this->database->delete('config', ['cat' => $cat, 'k' => $key]);
111         }
112
113         /** {@inheritDoc} */
114         public function getCache(): Cache
115         {
116                 return $this->cache;
117         }
118 }