]> git.mxchange.org Git - friendica.git/blob - src/Core/Config.php
559ee83ece57e08b8bfedb53d94510b082606459
[friendica.git] / src / Core / Config.php
1 <?php
2 /**
3  * System Configuration Class
4  *
5  * @file include/Core/Config.php
6  *
7  * @brief Contains the class with methods for system configuration
8  */
9 namespace Friendica\Core;
10
11 use Friendica\Core\Config\ConfigCache;
12 use Friendica\Core\Config\IConfigAdapter;
13 use Friendica\Core\Config\IConfigCache;
14
15 /**
16  * @brief Arbitrary system configuration storage
17  *
18  * Note:
19  * If we ever would decide to return exactly the variable type as entered,
20  * we will have fun with the additional features. :-)
21  */
22 class Config
23 {
24         /**
25          * @var Config\IConfigAdapter|null
26          */
27         private static $adapter;
28
29         /**
30          * @var Config\IConfigCache
31          */
32         private static $cache;
33
34         /**
35          * Initialize the config with only the cache
36          *
37          * @param Config\IConfigCache $cache  The configuration cache
38          */
39         public static function init(Config\IConfigCache $cache)
40         {
41                 self::$cache  = $cache;
42         }
43
44         /**
45          * Add the adapter for DB-backend
46          *
47          * @param Config\IConfigAdapter $adapter
48          */
49         public static function setAdapter(Config\IConfigAdapter $adapter)
50         {
51                 self::$adapter = $adapter;
52         }
53
54         /**
55          * @brief Loads all configuration values of family into a cached storage.
56          *
57          * All configuration values of the system are stored in the cache ( @see IConfigCache )
58          *
59          * @param string $family The category of the configuration value
60          *
61          * @return void
62          */
63         public static function load($family = "config")
64         {
65                 if (!isset(self::$adapter) || !self::$adapter->isConnected()) {
66                         return;
67                 }
68
69                 self::$adapter->load($family);
70         }
71
72         /**
73          * @brief Get a particular user's config variable given the category name
74          * ($family) and a key.
75          *
76          * Get a particular config value from the given category ($family)
77          * and the $key from a cached storage either from the self::$adapter
78          * (@see IConfigAdapter ) or from the static::$cache (@see IConfigCache ).
79          *
80          * @param string  $family        The category of the configuration value
81          * @param string  $key           The configuration key to query
82          * @param mixed   $default_value optional, The value to return if key is not set (default: null)
83          * @param boolean $refresh       optional, If true the config is loaded from the db and not from the cache (default: false)
84          *
85          * @return mixed Stored value or null if it does not exist
86          */
87         public static function get($family, $key, $default_value = null, $refresh = false)
88         {
89                 if (!isset(self::$adapter) || !self::$adapter->isConnected()) {
90                         return self::$cache->get($family, $key, $default_value);
91                 }
92
93                 return self::$adapter->get($family, $key, $default_value, $refresh);
94         }
95
96         /**
97          * @brief Sets a configuration value for system config
98          *
99          * Stores a config value ($value) in the category ($family) under the key ($key)
100          *
101          * Note: Please do not store booleans - convert to 0/1 integer values!
102          *
103          * @param string $family The category of the configuration value
104          * @param string $key    The configuration key to set
105          * @param mixed  $value  The value to store
106          *
107          * @return bool Operation success
108          */
109         public static function set($family, $key, $value)
110         {
111                 if (!isset(self::$adapter) || !self::$adapter->isConnected()) {
112                         return self::$cache->set($family, $key, $value);
113                 }
114
115                 return self::$adapter->set($family, $key, $value);
116         }
117
118         /**
119          * @brief Deletes the given key from the system configuration.
120          *
121          * Removes the configured value from the stored cache in self::$config
122          * (@see ConfigCache ) and removes it from the database (@see IConfigAdapter ).
123          *
124          * @param string $family The category of the configuration value
125          * @param string $key    The configuration key to delete
126          *
127          * @return mixed
128          */
129         public static function delete($family, $key)
130         {
131                 if (!isset(self::$adapter) || !self::$adapter->isConnected()) {
132                         self::$cache->delete($family, $key);
133                 }
134
135                 return self::$adapter->delete($family, $key);
136         }
137 }