]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Model/Config.php
3af2f7e4b1e8c3f992ce09d6ddf05e28bb25b441
[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\Util\ConfigFileManager;
26 use Friendica\Core\Config\ValueObject\Cache;
27
28 /**
29  * Configuration model, which manages the whole system configuration
30  */
31 class Config implements IManageConfigValues
32 {
33         /**
34          * @var Cache
35          */
36         protected $configCache;
37
38         /** @var ConfigFileManager */
39         protected $configFileManager;
40
41         /**
42          * @param ConfigFileManager $configFileManager The configuration file manager to save back configs
43          * @param Cache  $configCache The configuration cache (based on the config-files)
44          */
45         public function __construct(ConfigFileManager $configFileManager, Cache $configCache)
46         {
47                 $this->configFileManager = $configFileManager;
48                 $this->configCache       = $configCache;
49         }
50
51         /**
52          * {@inheritDoc}
53          */
54         public function getCache(): Cache
55         {
56                 return $this->configCache;
57         }
58
59         public function save()
60         {
61                 $this->configFileManager->saveData($this->configCache);
62         }
63
64         public function load(string $cat = 'config')
65         {
66                 $configCache = new Cache();
67
68                 $this->configFileManager->setupCache($configCache, $_SERVER);
69                 $this->configCache = $configCache;
70         }
71
72         public function get(string $cat, string $key, $default_value = null, bool $refresh = false)
73         {
74                 return $this->configCache->get($cat, $key) ?? $default_value;
75         }
76
77         public function set(string $cat, string $key, $value, bool $autosave = true): bool
78         {
79                 $stored = $this->configCache->set($cat, $key, $value, Cache::SOURCE_DATA);
80
81                 if ($stored && $autosave) {
82                         $this->save();
83                 }
84
85                 return $stored;
86         }
87
88         public function delete(string $cat, string $key, bool $autosave = true): bool
89         {
90                 $removed = $this->configCache->delete($cat, $key);
91
92                 if ($removed && $autosave) {
93                         $this->save();
94                 }
95
96                 return $removed;
97         }
98 }