3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Core\Config\Model;
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;
31 * Complete system configuration model, bound with the database
33 class DatabaseConfig implements IManageConfigValues
40 public function __construct(Database $database, Cache $cache)
42 $this->database = $database;
43 $this->cache = $cache;
49 public function reload()
51 $config = $this->database->selectToArray('config');
53 foreach ($config as $entry) {
54 $this->cache->set($entry['cat'], $entry['k'], SerializeUtil::maybeUnserialize($entry['v']), Cache::SOURCE_DATA);
58 public function setAndSave(Cache $setCache, Cache $delCache): bool
60 $this->database->transaction();
62 foreach ($setCache->getAll() as $category => $data) {
63 foreach ($data as $key => $value) {
64 $this->set($category, $key, $value);
68 foreach ($delCache->getAll() as $category => $keys) {
69 foreach ($keys as $key => $value) {
70 $this->delete($category, $key);
74 return $this->database->commit();
78 public function get(string $cat, string $key = null, $default_value = null)
80 return $this->cache->get($cat, $key) ?? $default_value;
84 public function isWritable(string $cat, string $key): bool
86 return $this->cache->getSource($cat, $key) < Cache::SOURCE_ENV;
90 public function set(string $cat, string $key, $value): bool
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);
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);
101 public function beginTransaction(): ISetConfigValuesTransactionally
103 return new ConfigTransaction($this);
107 public function delete(string $cat, string $key): bool
109 $this->cache->delete($cat, $key);
110 return $this->database->delete('config', ['cat' => $cat, 'k' => $key]);
114 public function getCache(): Cache