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