3 namespace Friendica\Core\Config;
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 )
14 * The blacklist of configuration settings, which should not get saved to the backend
17 private $configSaveBlacklist = [
24 * @var Cache\IConfigCache
29 * @var Adapter\IConfigAdapter
31 private $configAdapter;
34 * @param Cache\IConfigCache $configCache The configuration cache (based on the config-files)
35 * @param Adapter\IConfigAdapter $configAdapter The configuration DB-backend
37 public function __construct(Cache\IConfigCache $configCache, Adapter\IConfigAdapter $configAdapter)
39 $this->configCache = $configCache;
40 $this->configAdapter = $configAdapter;
46 * Returns the Config Cache
48 * @return Cache\IConfigCache
50 public function getCache()
52 return $this->configCache;
56 * @brief Loads all configuration values of family into a cached storage.
58 * All configuration values of the system are stored in the cache ( @see IConfigCache )
60 * @param string $cat The category of the configuration value
64 public function load($cat = 'config')
66 // If not connected, do nothing
67 if (!$this->configAdapter->isConnected()) {
71 // load the whole category out of the DB into the cache
72 $this->configCache->load($this->configAdapter->load($cat), true);
76 * @brief Get a particular user's config variable given the category name
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 ).
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)
88 * @return mixed Stored value or null if it does not exist
90 public function get($cat, $key, $default_value = null, $refresh = false)
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) ||
97 $dbvalue = $this->configAdapter->get($cat, $key);
99 if (isset($dbvalue)) {
100 $this->configCache->set($cat, $key, $dbvalue);
105 // use the config cache for return
106 $result = $this->configCache->get($cat, $key);
108 return (isset($result)) ? $result : $default_value;
112 * @brief Sets a configuration value for system config
114 * Stores a config value ($value) in the category ($cat) under the key ($key)
116 * Note: Please do not store booleans - convert to 0/1 integer values!
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
122 * @return bool Operation success
124 public function set($cat, $key, $value)
126 // set the cache first
127 $cached = $this->configCache->set($cat, $key, $value);
129 // If there is no connected adapter, we're finished
130 if (!$this->configAdapter->isConnected() && !empty($this->configSaveBlacklist[$cat][$key])) {
134 $stored = $this->configAdapter->set($cat, $key, $value);
136 return $cached && $stored;
140 * @brief Deletes the given key from the system configuration.
142 * Removes the configured value from the stored cache in $this->configCache
143 * (@see ConfigCache ) and removes it from the database (@see IConfigAdapter ).
145 * @param string $cat The category of the configuration value
146 * @param string $key The configuration key to delete
150 public function delete($cat, $key)
152 $cacheRemoved = $this->configCache->delete($cat, $key);
154 if (!$this->configAdapter->isConnected()) {
155 return $cacheRemoved;
158 $storeRemoved = $this->configAdapter->delete($cat, $key);
160 return $cacheRemoved || $storeRemoved;