]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Model/Config.php
46d5643b344bc66e4538b693b17bc400bcba296f
[friendica.git] / src / Core / Config / Model / Config.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\Exception\ConfigFileException;
27 use Friendica\Core\Config\Exception\ConfigPersistenceException;
28 use Friendica\Core\Config\Util\ConfigFileManager;
29 use Friendica\Core\Config\ValueObject\Cache;
30
31 /**
32  * Configuration model, which manages the whole system configuration
33  */
34 class Config implements IManageConfigValues
35 {
36         /** @var Cache */
37         protected $configCache;
38
39         /** @var ConfigFileManager */
40         protected $configFileManager;
41
42         /**
43          * @param ConfigFileManager $configFileManager The configuration file manager to save back configs
44          * @param Cache             $configCache       The configuration cache (based on the config-files)
45          */
46         public function __construct(ConfigFileManager $configFileManager, Cache $configCache)
47         {
48                 $this->configFileManager = $configFileManager;
49                 $this->configCache       = $configCache;
50         }
51
52         /**
53          * Load all configuration values from a given cache and saves it back in the configuration node store
54          * @see ConfigFileManager::CONFIG_DATA_FILE
55          *
56          * All configuration values of the system are stored in the cache.
57          *
58          * @param Cache $cache a new cache
59          *
60          * @return void
61          *
62          * @throws ConfigPersistenceException In case the persistence layer throws errors
63          */
64         public function setCacheAndSave(Cache $cache)
65         {
66                 $this->configCache = $cache;
67                 $this->save();
68         }
69
70         /**
71          * {@inheritDoc}
72          */
73         public function getCache(): Cache
74         {
75                 return $this->configCache;
76         }
77
78         /**     {@inheritDoc} */
79         public function beginTransaction(): ISetConfigValuesTransactionally
80         {
81                 return new ConfigTransaction($this);
82         }
83
84         /**
85          * Saves the current Configuration back into the data config.
86          * @see ConfigFileManager::CONFIG_DATA_FILE
87          */
88         protected function save()
89         {
90                 try {
91                         $this->configFileManager->saveData($this->configCache);
92                         // reload after the save to possible reload default values of lower source-priorities again
93                         $this->reload();
94                 } catch (ConfigFileException $e) {
95                         throw new ConfigPersistenceException('Cannot save config', $e);
96                 }
97         }
98
99         /** {@inheritDoc} */
100         public function reload()
101         {
102                 $configCache = new Cache();
103
104                 try {
105                         $this->configFileManager->setupCache($configCache);
106                 } catch (ConfigFileException $e) {
107                         throw new ConfigPersistenceException('Cannot reload config', $e);
108                 }
109                 $this->configCache = $configCache;
110         }
111
112         /** {@inheritDoc} */
113         public function get(string $cat, string $key = null, $default_value = null)
114         {
115                 return $this->configCache->get($cat, $key) ?? $default_value;
116         }
117
118         /** {@inheritDoc} */
119         public function set(string $cat, string $key, $value): bool
120         {
121                 if ($this->configCache->set($cat, $key, $value, Cache::SOURCE_DATA)) {
122                         $this->save();
123                         return true;
124                 } else {
125                         return false;
126                 }
127         }
128
129         /** {@inheritDoc} */
130         public function delete(string $cat, string $key): bool
131         {
132                 if ($this->configCache->delete($cat, $key)) {
133                         $this->save();
134                         return true;
135                 } else {
136                         return false;
137                 }
138         }
139 }