Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
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.

207 lines
4.5KB

  1. <?php
  2. namespace Friendica\Core\Config\Cache;
  3. /**
  4. * The Friendica config cache for the application
  5. * Initial, all *.config.php files are loaded into this cache with the
  6. * ConfigCacheLoader ( @see ConfigCacheLoader )
  7. *
  8. * Is used for further caching operations too (depending on the ConfigAdapter )
  9. */
  10. class ConfigCache implements IConfigCache, IPConfigCache
  11. {
  12. /**
  13. * @var array
  14. */
  15. private $config;
  16. /**
  17. * @param array $config A initial config array
  18. */
  19. public function __construct(array $config = [])
  20. {
  21. $this->load($config);
  22. }
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function load(array $config, $overwrite = false)
  27. {
  28. $categories = array_keys($config);
  29. foreach ($categories as $category) {
  30. if (isset($config[$category]) && is_array($config[$category])) {
  31. $keys = array_keys($config[$category]);
  32. foreach ($keys as $key) {
  33. if (isset($config[$category][$key])) {
  34. if ($overwrite) {
  35. $this->set($category, $key, $config[$category][$key]);
  36. } else {
  37. $this->setDefault($category, $key, $config[$category][$key]);
  38. }
  39. }
  40. }
  41. }
  42. }
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function get($cat, $key = null)
  48. {
  49. if (isset($this->config[$cat][$key])) {
  50. return $this->config[$cat][$key];
  51. } elseif ($key == null && isset($this->config[$cat])) {
  52. return $this->config[$cat];
  53. } else {
  54. return '!<unset>!';
  55. }
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function has($cat, $key = null)
  61. {
  62. return (isset($this->config[$cat][$key]) && $this->config[$cat][$key] !== '!<unset>!') ||
  63. ($key == null && isset($this->config[$cat]) && $this->config[$cat] !== '!<unset>!' && is_array($this->config[$cat]));
  64. }
  65. /**
  66. * Sets a default value in the config cache. Ignores already existing keys.
  67. *
  68. * @param string $cat Config category
  69. * @param string $k Config key
  70. * @param mixed $v Default value to set
  71. */
  72. private function setDefault($cat, $k, $v)
  73. {
  74. if (!isset($this->config[$cat][$k])) {
  75. $this->set($cat, $k, $v);
  76. }
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function set($cat, $key, $value)
  82. {
  83. // Only arrays are serialized in database, so we have to unserialize sparingly
  84. $value = is_string($value) && preg_match("|^a:[0-9]+:{.*}$|s", $value) ? unserialize($value) : $value;
  85. if (!isset($this->config[$cat])) {
  86. $this->config[$cat] = [];
  87. }
  88. $this->config[$cat][$key] = $value;
  89. return true;
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function hasP($uid, $cat, $key = null)
  95. {
  96. return (isset($this->config[$uid][$cat][$key]) && $this->config[$uid][$cat][$key] !== '!<unset>!') ||
  97. ($key == null && isset($this->config[$uid][$cat]) && $this->config[$uid][$cat] !== '!<unset>!' && is_array($this->config[$uid][$cat]));
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function delete($cat, $key)
  103. {
  104. if (isset($this->config[$cat][$key])) {
  105. unset($this->config[$cat][$key]);
  106. if (count($this->config[$cat]) == 0) {
  107. unset($this->config[$cat]);
  108. }
  109. return true;
  110. } else {
  111. return false;
  112. }
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function loadP($uid, array $config)
  118. {
  119. foreach ($config as $category => $values) {
  120. foreach ($values as $key => $value) {
  121. $this->setP($uid, $category, $key, $value);
  122. }
  123. }
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function getP($uid, $cat, $key = null)
  129. {
  130. if (isset($this->config[$uid][$cat][$key])) {
  131. return $this->config[$uid][$cat][$key];
  132. } elseif ($key == null && isset($this->config[$uid][$cat])) {
  133. return $this->config[$uid][$cat];
  134. } else {
  135. return '!<unset>!';
  136. }
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function setP($uid, $cat, $key, $value)
  142. {
  143. // Only arrays are serialized in database, so we have to unserialize sparingly
  144. $value = is_string($value) && preg_match("|^a:[0-9]+:{.*}$|s", $value) ? unserialize($value) : $value;
  145. if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
  146. $this->config[$uid] = [];
  147. }
  148. if (!isset($this->config[$uid][$cat])) {
  149. $this->config[$uid][$cat] = [];
  150. }
  151. $this->config[$uid][$cat][$key] = $value;
  152. return true;
  153. }
  154. /**
  155. * {@inheritdoc}
  156. */
  157. public function deleteP($uid, $cat, $key)
  158. {
  159. if (isset($this->config[$uid][$cat][$key])) {
  160. unset($this->config[$uid][$cat][$key]);
  161. if (count($this->config[$uid][$cat]) == 0) {
  162. unset($this->config[$uid][$cat]);
  163. if (count($this->config[$uid]) == 0) {
  164. unset($this->config[$uid]);
  165. }
  166. }
  167. return true;
  168. } else {
  169. return false;
  170. }
  171. }
  172. /**
  173. * Returns the whole configuration
  174. *
  175. * @return array The configuration
  176. */
  177. public function getAll()
  178. {
  179. return $this->config;
  180. }
  181. }