]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/PreloadConfig.php
Merge branch '2021.03-rc' into copyright-2021
[friendica.git] / src / Core / Config / PreloadConfig.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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;
23
24 use Friendica\Core\BaseConfig;
25 use Friendica\Model;
26
27 /**
28  * This class implements the preload configuration, which will cache
29  * all config values per call in a cache.
30  *
31  * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
32  */
33 class PreloadConfig extends BaseConfig
34 {
35         /** @var bool */
36         private $config_loaded;
37
38         /**
39          * @param Cache               $configCache The configuration cache (based on the config-files)
40          * @param Model\Config\Config $configModel The configuration model
41          */
42         public function __construct(Cache $configCache, Model\Config\Config $configModel)
43         {
44                 parent::__construct($configCache, $configModel);
45                 $this->config_loaded = false;
46
47                 $this->load();
48         }
49
50         /**
51          * {@inheritDoc}
52          *
53          * This loads all config values everytime load is called
54          *
55          */
56         public function load(string $cat = 'config')
57         {
58                 // Don't load the whole configuration twice
59                 if ($this->config_loaded) {
60                         return;
61                 }
62
63                 // If not connected, do nothing
64                 if (!$this->configModel->isConnected()) {
65                         return;
66                 }
67
68                 $config              = $this->configModel->load();
69                 $this->config_loaded = true;
70
71                 // load the whole category out of the DB into the cache
72                 $this->configCache->load($config, Cache::SOURCE_DB);
73         }
74
75         /**
76          * {@inheritDoc}
77          */
78         public function get(string $cat, string $key, $default_value = null, bool $refresh = false)
79         {
80                 if ($refresh) {
81                         if ($this->configModel->isConnected()) {
82                                 $config = $this->configModel->get($cat, $key);
83                                 if (isset($config)) {
84                                         $this->configCache->set($cat, $key, $config, Cache::SOURCE_DB);
85                                 }
86                         }
87                 }
88
89                 // use the config cache for return
90                 $result = $this->configCache->get($cat, $key);
91
92                 return (isset($result)) ? $result : $default_value;
93         }
94
95         /**
96          * {@inheritDoc}
97          */
98         public function set(string $cat, string $key, $value)
99         {
100                 if (!$this->config_loaded) {
101                         $this->load();
102                 }
103
104                 // set the cache first
105                 $cached = $this->configCache->set($cat, $key, $value, Cache::SOURCE_DB);
106
107                 // If there is no connected adapter, we're finished
108                 if (!$this->configModel->isConnected()) {
109                         return $cached;
110                 }
111
112                 $stored = $this->configModel->set($cat, $key, $value);
113
114                 return $cached && $stored;
115         }
116
117         /**
118          * {@inheritDoc}
119          */
120         public function delete(string $cat, string $key)
121         {
122                 if ($this->config_loaded) {
123                         $this->load();
124                 }
125
126                 $cacheRemoved = $this->configCache->delete($cat, $key);
127
128                 if (!$this->configModel->isConnected()) {
129                         return $cacheRemoved;
130                 }
131
132                 $storeRemoved = $this->configModel->delete($cat, $key);
133
134                 return $cacheRemoved || $storeRemoved;
135         }
136
137         public function testSetDouble()
138         {
139                 $this->configModel->shouldReceive('isConnected')
140                                                   ->andReturn(true);
141
142                 // constructor loading
143                 $this->configModel->shouldReceive('load')
144                                                   ->with('config')
145                                                   ->andReturn(['config' => ['test' => 'it']])
146                                                   ->once();
147
148                 parent::testSetDouble();
149         }
150 }