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