]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/JitConfiguration.php
Move Preload/JIT Configuration logic from Adapter to Core-Configuration
[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                 foreach ($config[$cat] as $key => $value) {
54                         $this->in_db[$cat][$key] = true;
55                 }
56
57                 // load the whole category out of the DB into the cache
58                 $this->configCache->load($config, true);
59         }
60
61         /**
62          * {@inheritDoc}
63          */
64         public function get(string $cat, string $key, $default_value = null, bool $refresh = false)
65         {
66                 // if the value isn't loaded or refresh is needed, load it to the cache
67                 if ($this->configModel->isConnected() &&
68                     (empty($this->in_db[$cat][$key]) ||
69                      $refresh)) {
70
71                         $dbvalue = $this->configModel->get($cat, $key);
72
73                         if (isset($dbvalue)) {
74                                 $this->configCache->set($cat, $key, $dbvalue);
75                                 unset($dbvalue);
76                                 $this->in_db[$cat][$key] = true;
77                         }
78                 }
79
80                 // use the config cache for return
81                 $result = $this->configCache->get($cat, $key);
82
83                 return (isset($result)) ? $result : $default_value;
84         }
85
86         /**
87          * {@inheritDoc}
88          */
89         public function set(string $cat, string $key, $value)
90         {
91                 // set the cache first
92                 $cached = $this->configCache->set($cat, $key, $value);
93
94                 // If there is no connected adapter, we're finished
95                 if (!$this->configModel->isConnected()) {
96                         return $cached;
97                 }
98
99                 $stored = $this->configModel->set($cat, $key, $value);
100
101                 $this->in_db[$cat][$key] = $stored;
102
103                 return $cached && $stored;
104         }
105
106         /**
107          * @brief Deletes the given key from the system configuration.
108          *
109          * Removes the configured value from the stored cache in $this->configCache
110          * (@param string $cat The category of the configuration value
111          *
112          * @param string $key The configuration key to delete
113          *
114          * @return bool
115          * @see   ConfigCache ) and removes it from the database (@see IConfigAdapter ).
116          *
117          */
118         public function delete(string $cat, string $key)
119         {
120                 $cacheRemoved = $this->configCache->delete($cat, $key);
121
122                 if (isset($this->in_db[$cat][$key])) {
123                         unset($this->in_db[$cat][$key]);
124                 }
125
126                 if (!$this->configModel->isConnected()) {
127                         return $cacheRemoved;
128                 }
129
130                 $storeRemoved = $this->configModel->delete($cat, $key);
131
132                 return $cacheRemoved || $storeRemoved;
133         }
134 }