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