Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Configuration.php 4.2KB

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