]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/JitConfiguration.php
e5b7b5e47c9f2a7d2e104f028917ef79cb9cd752
[friendica.git] / src / Core / Config / JitConfiguration.php
1 <?php
2
3 namespace Friendica\Core\Config;
4
5 use Friendica\Model;
6
7 /**
8  * This class is responsible for all system-wide configuration values in Friendica
9  * There are two types of storage
10  * - The Config-Files    (loaded into the FileCache @see Cache\ConfigCache )
11  * - The Config-DB-Table (per Config-DB-model @see Model\Config\Config )
12  */
13 class JitConfiguration extends Configuration
14 {
15         /** @var array */
16         private $in_db;
17
18         /**
19          * @param Cache\ConfigCache   $configCache The configuration cache (based on the config-files)
20          * @param Model\Config\Config $configModel The configuration model
21          */
22         public function __construct(Cache\ConfigCache $configCache, Model\Config\Config $configModel)
23         {
24                 parent::__construct($configCache, $configModel);
25                 $this->in_db = [];
26
27                 // take the values of the given cache instead of loading them from the model again
28                 $preSet = $configCache->getAll();
29                 if (!empty($preSet)) {
30                         foreach ($preSet as $cat => $data) {
31                                 foreach ($data as $key => $value) {
32                                         $this->in_db[$cat][$key] = true;
33                                 }
34                         }
35                 }
36
37                 $this->load();
38         }
39
40         /**
41          * {@inheritDoc}
42          *
43          */
44         public function load(string $cat = 'config')
45         {
46                 // If not connected, do nothing
47                 if (!$this->configModel->isConnected()) {
48                         return;
49                 }
50
51                 $config = $this->configModel->load($cat);
52
53                 if (!empty($config[$cat])) {
54                         foreach ($config[$cat] as $key => $value) {
55                                 $this->in_db[$cat][$key] = true;
56                         }
57                 }
58
59                 // load the whole category out of the DB into the cache
60                 $this->configCache->load($config, true);
61         }
62
63         /**
64          * {@inheritDoc}
65          */
66         public function get(string $cat, string $key, $default_value = null, bool $refresh = false)
67         {
68                 // if the value isn't loaded or refresh is needed, load it to the cache
69                 if ($this->configModel->isConnected() &&
70                     (empty($this->in_db[$cat][$key]) ||
71                      $refresh)) {
72
73                         $dbvalue = $this->configModel->get($cat, $key);
74
75                         if (isset($dbvalue)) {
76                                 $this->configCache->set($cat, $key, $dbvalue);
77                                 unset($dbvalue);
78                                 $this->in_db[$cat][$key] = true;
79                         }
80                 }
81
82                 // use the config cache for return
83                 $result = $this->configCache->get($cat, $key);
84
85                 return (isset($result)) ? $result : $default_value;
86         }
87
88         /**
89          * {@inheritDoc}
90          */
91         public function set(string $cat, string $key, $value)
92         {
93                 // set the cache first
94                 $cached = $this->configCache->set($cat, $key, $value);
95
96                 // If there is no connected adapter, we're finished
97                 if (!$this->configModel->isConnected()) {
98                         return $cached;
99                 }
100
101                 $stored = $this->configModel->set($cat, $key, $value);
102
103                 $this->in_db[$cat][$key] = $stored;
104
105                 return $cached && $stored;
106         }
107
108         /**
109          * @brief Deletes the given key from the system configuration.
110          *
111          * Removes the configured value from the stored cache in $this->configCache
112          * (@param string $cat The category of the configuration value
113          *
114          * @param string $key The configuration key to delete
115          *
116          * @return bool
117          * @see   ConfigCache ) and removes it from the database (@see IConfigAdapter ).
118          *
119          */
120         public function delete(string $cat, string $key)
121         {
122                 $cacheRemoved = $this->configCache->delete($cat, $key);
123
124                 if (isset($this->in_db[$cat][$key])) {
125                         unset($this->in_db[$cat][$key]);
126                 }
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 }