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 * @var Cache\IConfigCache
19 * @var Adapter\IConfigAdapter
21 private $configAdapter;
24 * @param Cache\IConfigCache $configCache The configuration cache (based on the config-files)
25 * @param Adapter\IConfigAdapter $configAdapter The configuration DB-backend
27 public function __construct(Cache\IConfigCache $configCache, Adapter\IConfigAdapter $configAdapter)
29 $this->configCache = $configCache;
30 $this->configAdapter = $configAdapter;
36 * Returns the Config Cache
38 * @return Cache\IConfigCache
40 public function getCache()
42 return $this->configCache;
46 * @brief Loads all configuration values of family into a cached storage.
48 * All configuration values of the system are stored in the cache ( @see IConfigCache )
50 * @param string $cat The category of the configuration value
54 public function load($cat = 'config')
56 // If not connected, do nothing
57 if (!$this->configAdapter->isConnected()) {
61 // load the whole category out of the DB into the cache
62 $this->configCache->load($this->configAdapter->load($cat), true);
66 * @brief Get a particular user's config variable given the category name
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 ).
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)
78 * @return mixed Stored value or null if it does not exist
80 public function get($cat, $key, $default_value = null, $refresh = false)
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) ||
86 $dbvalue = $this->configAdapter->get($cat, $key);
88 if ($dbvalue !== '!<unset>!') {
89 $this->configCache->set($cat, $key, $dbvalue);
94 // use the config cache for return
95 if ($this->configCache->has($cat, $key)) {
96 return $this->configCache->get($cat, $key);
98 return $default_value;
103 * @brief Sets a configuration value for system config
105 * Stores a config value ($value) in the category ($cat) under the key ($key)
107 * Note: Please do not store booleans - convert to 0/1 integer values!
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
113 * @return bool Operation success
115 public function set($cat, $key, $value)
117 // set the cache first
118 $cached = $this->configCache->set($cat, $key, $value);
120 // If there is no connected adapter, we're finished
121 if (!$this->configAdapter->isConnected()) {
125 $stored = $this->configAdapter->set($cat, $key, $value);
127 return $cached && $stored;
131 * @brief Deletes the given key from the system configuration.
133 * Removes the configured value from the stored cache in $this->configCache
134 * (@see ConfigCache ) and removes it from the database (@see IConfigAdapter ).
136 * @param string $cat The category of the configuration value
137 * @param string $key The configuration key to delete
141 public function delete($cat, $key)
143 $cacheRemoved = $this->configCache->delete($cat, $key);
145 if (!$this->configAdapter->isConnected()) {
146 return $cacheRemoved;
149 $storeRemoved = $this->configAdapter->delete($cat, $key);
151 return $cacheRemoved || $storeRemoved;