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\Exception\ConfigFileException;
27 use Friendica\Core\Config\Exception\ConfigPersistenceException;
28 use Friendica\Core\Config\Util\ConfigFileManager;
29 use Friendica\Core\Config\ValueObject\Cache;
32 * Configuration model, which manages the whole system configuration
34 class Config implements IManageConfigValues
37 protected $configCache;
39 /** @var ConfigFileManager */
40 protected $configFileManager;
43 * @param ConfigFileManager $configFileManager The configuration file manager to save back configs
44 * @param Cache $configCache The configuration cache (based on the config-files)
46 public function __construct(ConfigFileManager $configFileManager, Cache $configCache)
48 $this->configFileManager = $configFileManager;
49 $this->configCache = $configCache;
55 public function getCache(): Cache
57 return $this->configCache;
61 public function beginTransaction(): ISetConfigValuesTransactionally
63 return new ConfigTransaction($this);
67 * Saves the current Configuration back into the data config.
68 * @see ConfigFileManager::CONFIG_DATA_FILE
70 protected function save()
73 $this->configFileManager->saveData($this->configCache);
74 } catch (ConfigFileException $e) {
75 throw new ConfigPersistenceException('Cannot save config', $e);
80 public function reload()
82 $configCache = new Cache();
85 $this->configFileManager->setupCache($configCache);
86 } catch (ConfigFileException $e) {
87 throw new ConfigPersistenceException('Cannot reload config', $e);
89 $this->configCache = $configCache;
93 public function load(Cache $cache)
95 $this->configCache = $cache;
100 public function get(string $cat, string $key = null, $default_value = null)
102 return $this->configCache->get($cat, $key) ?? $default_value;
106 public function set(string $cat, string $key, $value): bool
108 if ($this->configCache->set($cat, $key, $value, Cache::SOURCE_DATA)) {
117 public function delete(string $cat, string $key): bool
119 if ($this->configCache->delete($cat, $key)) {