]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Configuration.php
updated the credits
[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\IConfigCache
15          */
16         private $configCache;
17
18         /**
19          * @var Adapter\IConfigAdapter
20          */
21         private $configAdapter;
22
23         /**
24          * @param Cache\IConfigCache     $configCache   The configuration cache (based on the config-files)
25          * @param Adapter\IConfigAdapter $configAdapter The configuration DB-backend
26          */
27         public function __construct(Cache\IConfigCache $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\IConfigCache
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                         $dbvalue = $this->configAdapter->get($cat, $key);
87
88                         if ($dbvalue !== '!<unset>!') {
89                                 $this->configCache->set($cat, $key, $dbvalue);
90                                 return $dbvalue;
91                         }
92                 }
93
94                 // use the config cache for return
95                 if ($this->configCache->has($cat, $key)) {
96                         return $this->configCache->get($cat, $key);
97                 } else {
98                         return $default_value;
99                 }
100         }
101
102         /**
103          * @brief Sets a configuration value for system config
104          *
105          * Stores a config value ($value) in the category ($cat) under the key ($key)
106          *
107          * Note: Please do not store booleans - convert to 0/1 integer values!
108          *
109          * @param string $cat The category of the configuration value
110          * @param string $key    The configuration key to set
111          * @param mixed  $value  The value to store
112          *
113          * @return bool Operation success
114          */
115         public function set($cat, $key, $value)
116         {
117                 // set the cache first
118                 $cached = $this->configCache->set($cat, $key, $value);
119
120                 // If there is no connected adapter, we're finished
121                 if (!$this->configAdapter->isConnected()) {
122                         return $cached;
123                 }
124
125                 $stored = $this->configAdapter->set($cat, $key, $value);
126
127                 return $cached && $stored;
128         }
129
130         /**
131          * @brief Deletes the given key from the system configuration.
132          *
133          * Removes the configured value from the stored cache in $this->configCache
134          * (@see ConfigCache ) and removes it from the database (@see IConfigAdapter ).
135          *
136          * @param string $cat The category of the configuration value
137          * @param string $key    The configuration key to delete
138          *
139          * @return bool
140          */
141         public function delete($cat, $key)
142         {
143                 $cacheRemoved = $this->configCache->delete($cat, $key);
144
145                 if (!$this->configAdapter->isConnected()) {
146                         return $cacheRemoved;
147                 }
148
149                 $storeRemoved = $this->configAdapter->delete($cat, $key);
150
151                 return $cacheRemoved || $storeRemoved;
152         }
153 }