]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Configuration.php
Merge pull request #6942 from annando/worker-fast-commands
[friendica.git] / src / Core / Config / Configuration.php
1 <?php
2
3 namespace Friendica\Core\Config;
4
5 /**
6  * This class is responsible for all system-wide configuration values in Friendica
7  * There are two types of storage
8  * - The Config-Files    (loaded into the FileCache @see Cache\IConfigCache )
9  * - The Config-DB-Table (per Config-DB-adapter @see Adapter\IConfigAdapter )
10  */
11 class Configuration
12 {
13         /**
14          * The blacklist of configuration settings, which should not get saved to the backend
15          * @var array
16          */
17         private $configSaveBlacklist = [
18                 'config' => [
19                         'hostname' => true,
20                 ]
21         ];
22
23         /**
24          * @var Cache\IConfigCache
25          */
26         private $configCache;
27
28         /**
29          * @var Adapter\IConfigAdapter
30          */
31         private $configAdapter;
32
33         /**
34          * @param Cache\IConfigCache     $configCache   The configuration cache (based on the config-files)
35          * @param Adapter\IConfigAdapter $configAdapter The configuration DB-backend
36          */
37         public function __construct(Cache\IConfigCache $configCache, Adapter\IConfigAdapter $configAdapter)
38         {
39                 $this->configCache = $configCache;
40                 $this->configAdapter = $configAdapter;
41
42                 $this->load();
43         }
44
45         /**
46          * Returns the Config Cache
47          *
48          * @return Cache\IConfigCache
49          */
50         public function getCache()
51         {
52                 return $this->configCache;
53         }
54
55         /**
56          * @brief Loads all configuration values of family into a cached storage.
57          *
58          * All configuration values of the system are stored in the cache ( @see IConfigCache )
59          *
60          * @param string $cat The category of the configuration value
61          *
62          * @return void
63          */
64         public function load($cat = 'config')
65         {
66                 // If not connected, do nothing
67                 if (!$this->configAdapter->isConnected()) {
68                         return;
69                 }
70
71                 // load the whole category out of the DB into the cache
72                 $this->configCache->load($this->configAdapter->load($cat), true);
73         }
74
75         /**
76          * @brief Get a particular user's config variable given the category name
77          * ($cat) and a $key.
78          *
79          * Get a particular config value from the given category ($cat)
80          * and the $key from a cached storage either from the $this->configAdapter
81          * (@see IConfigAdapter ) or from the $this->configCache (@see IConfigCache ).
82          *
83          * @param string  $cat        The category of the configuration value
84          * @param string  $key           The configuration key to query
85          * @param mixed   $default_value optional, The value to return if key is not set (default: null)
86          * @param boolean $refresh       optional, If true the config is loaded from the db and not from the cache (default: false)
87          *
88          * @return mixed Stored value or null if it does not exist
89          */
90         public function get($cat, $key, $default_value = null, $refresh = false)
91         {
92                 // if the value isn't loaded or refresh is needed, load it to the cache
93                 if ($this->configAdapter->isConnected() &&
94                         (!$this->configAdapter->isLoaded($cat, $key) ||
95                         $refresh)) {
96
97                         $dbvalue = $this->configAdapter->get($cat, $key);
98
99                         if (isset($dbvalue)) {
100                                 $this->configCache->set($cat, $key, $dbvalue);
101                                 return $dbvalue;
102                         }
103                 }
104
105                 // use the config cache for return
106                 $result = $this->configCache->get($cat, $key);
107
108                 return (isset($result)) ? $result : $default_value;
109         }
110
111         /**
112          * @brief Sets a configuration value for system config
113          *
114          * Stores a config value ($value) in the category ($cat) under the key ($key)
115          *
116          * Note: Please do not store booleans - convert to 0/1 integer values!
117          *
118          * @param string $cat The category of the configuration value
119          * @param string $key    The configuration key to set
120          * @param mixed  $value  The value to store
121          *
122          * @return bool Operation success
123          */
124         public function set($cat, $key, $value)
125         {
126                 // set the cache first
127                 $cached = $this->configCache->set($cat, $key, $value);
128
129                 // If there is no connected adapter, we're finished
130                 if (!$this->configAdapter->isConnected() || !empty($this->configSaveBlacklist[$cat][$key])) {
131                         return $cached;
132                 }
133
134                 $stored = $this->configAdapter->set($cat, $key, $value);
135
136                 return $cached && $stored;
137         }
138
139         /**
140          * @brief Deletes the given key from the system configuration.
141          *
142          * Removes the configured value from the stored cache in $this->configCache
143          * (@see ConfigCache ) and removes it from the database (@see IConfigAdapter ).
144          *
145          * @param string $cat The category of the configuration value
146          * @param string $key    The configuration key to delete
147          *
148          * @return bool
149          */
150         public function delete($cat, $key)
151         {
152                 $cacheRemoved = $this->configCache->delete($cat, $key);
153
154                 if (!$this->configAdapter->isConnected()) {
155                         return $cacheRemoved;
156                 }
157
158                 $storeRemoved = $this->configAdapter->delete($cat, $key);
159
160                 return $cacheRemoved || $storeRemoved;
161         }
162 }