]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Type/JitConfig.php
New function to exit the program
[friendica.git] / src / Core / Config / Type / JitConfig.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Type;
23
24 use Friendica\Core\Config\ValueObject\Cache;
25 use Friendica\Core\Config\Repository\Config;
26
27 /**
28  * This class implements the Just-In-Time configuration, which will cache
29  * config values in a cache, once they are retrieved.
30  *
31  * Default Configuration type.
32  * Provides the best performance for pages loading few configuration variables.
33  */
34 class JitConfig extends AbstractConfig
35 {
36         /**
37          * @var array Array of already loaded db values (even if there was no value)
38          */
39         private $db_loaded;
40
41         /**
42          * @param Cache  $configCache The configuration cache (based on the config-files)
43          * @param Config $configRepo  The configuration model
44          */
45         public function __construct(Cache $configCache, Config $configRepo)
46         {
47                 parent::__construct($configCache, $configRepo);
48                 $this->db_loaded = [];
49
50                 $this->load();
51         }
52
53         /**
54          * {@inheritDoc}
55          */
56         public function load(string $cat = 'config')
57         {
58                 // If not connected, do nothing
59                 if (!$this->configRepo->isConnected()) {
60                         return;
61                 }
62
63                 $config = $this->configRepo->load($cat);
64
65                 if (!empty($config[$cat])) {
66                         foreach ($config[$cat] as $key => $value) {
67                                 $this->db_loaded[$cat][$key] = true;
68                         }
69                 }
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 the value isn't loaded or refresh is needed, load it to the cache
81                 if ($this->configRepo->isConnected() &&
82                         (empty($this->db_loaded[$cat][$key]) ||
83                          $refresh)) {
84                         $dbValue = $this->configRepo->get($cat, $key);
85
86                         if (isset($dbValue)) {
87                                 $this->configCache->set($cat, $key, $dbValue, Cache::SOURCE_DB);
88                                 unset($dbValue);
89                         }
90
91                         $this->db_loaded[$cat][$key] = true;
92                 }
93
94                 // use the config cache for return
95                 $result = $this->configCache->get($cat, $key);
96
97                 return (isset($result)) ? $result : $default_value;
98         }
99
100         /**
101          * {@inheritDoc}
102          */
103         public function set(string $cat, string $key, $value): bool
104         {
105                 // set the cache first
106                 $cached = $this->configCache->set($cat, $key, $value, Cache::SOURCE_DB);
107
108                 // If there is no connected adapter, we're finished
109                 if (!$this->configRepo->isConnected()) {
110                         return $cached;
111                 }
112
113                 $stored = $this->configRepo->set($cat, $key, $value);
114
115                 $this->db_loaded[$cat][$key] = $stored;
116
117                 return $cached && $stored;
118         }
119
120         /**
121          * {@inheritDoc}
122          */
123         public function delete(string $cat, string $key): bool
124         {
125                 $cacheRemoved = $this->configCache->delete($cat, $key);
126
127                 if (isset($this->db_loaded[$cat][$key])) {
128                         unset($this->db_loaded[$cat][$key]);
129                 }
130
131                 if (!$this->configRepo->isConnected()) {
132                         return $cacheRemoved;
133                 }
134
135                 $storeRemoved = $this->configRepo->delete($cat, $key);
136
137                 return $cacheRemoved || $storeRemoved;
138         }
139 }