]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Model/Config.php
24f5fd3b590a1f105e7be081e342541d42ed3249
[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\ISetConfigValuesTransactional;
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         /** @var array */
43         protected $server;
44
45         /**
46          * @param ConfigFileManager $configFileManager The configuration file manager to save back configs
47          * @param Cache             $configCache       The configuration cache (based on the config-files)
48          * @param array             $server            The $_SERVER variable
49          */
50         public function __construct(ConfigFileManager $configFileManager, Cache $configCache, array $server = [])
51         {
52                 $this->configFileManager = $configFileManager;
53                 $this->configCache       = $configCache;
54                 $this->server            = $server;
55         }
56
57         /**
58          * {@inheritDoc}
59          */
60         public function getCache(): Cache
61         {
62                 return $this->configCache;
63         }
64
65         /**     {@inheritDoc} */
66         public function transactional(): ISetConfigValuesTransactional
67         {
68                 return new TransactionalConfig($this);
69         }
70
71         /**
72          * Saves the current Configuration back into the data config.
73          * @see ConfigFileManager::CONFIG_DATA_FILE
74          */
75         protected function save()
76         {
77                 try {
78                         $this->configFileManager->saveData($this->configCache);
79                 } catch (ConfigFileException $e) {
80                         throw new ConfigPersistenceException('Cannot save config', $e);
81                 }
82         }
83
84         /** {@inheritDoc} */
85         public function reload()
86         {
87                 $configCache = new Cache();
88
89                 try {
90                         $this->configFileManager->setupCache($configCache, $this->server);
91                 } catch (ConfigFileException $e) {
92                         throw new ConfigPersistenceException('Cannot reload config', $e);
93                 }
94                 $this->configCache = $configCache;
95         }
96
97         /** {@inheritDoc} */
98         public function load(Cache $cache)
99         {
100                 $this->configCache = $cache;
101                 $this->save();
102         }
103
104         /** {@inheritDoc} */
105         public function get(string $cat, string $key, $default_value = null)
106         {
107                 return $this->configCache->get($cat, $key) ?? $default_value;
108         }
109
110         /** {@inheritDoc} */
111         public function set(string $cat, string $key, $value): bool
112         {
113                 if ($this->configCache->set($cat, $key, $value, Cache::SOURCE_DATA)) {
114                         $this->save();
115                         return true;
116                 } else {
117                         return false;
118                 }
119         }
120
121         /** {@inheritDoc} */
122         public function delete(string $cat, string $key): bool
123         {
124                 if ($this->configCache->delete($cat, $key)) {
125                         $this->save();
126                         return true;
127                 } else {
128                         return false;
129                 }
130         }
131 }