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.

ConfigCache.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. } else {
  52. return '!<unset>!';
  53. }
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function has($cat, $key = null)
  59. {
  60. return isset($this->config[$cat][$key])
  61. && $this->config[$cat][$key] !== '!<unset>!';
  62. }
  63. /**
  64. * Sets a default value in the config cache. Ignores already existing keys.
  65. *
  66. * @param string $cat Config category
  67. * @param string $k Config key
  68. * @param mixed $v Default value to set
  69. */
  70. private function setDefault($cat, $k, $v)
  71. {
  72. if (!isset($this->config[$cat][$k])) {
  73. $this->set($cat, $k, $v);
  74. }
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function set($cat, $key, $value)
  80. {
  81. // Only arrays are serialized in database, so we have to unserialize sparingly
  82. $value = is_string($value) && preg_match("|^a:[0-9]+:{.*}$|s", $value) ? unserialize($value) : $value;
  83. if (!isset($this->config[$cat])) {
  84. $this->config[$cat] = [];
  85. }
  86. $this->config[$cat][$key] = $value;
  87. return true;
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function hasP($uid, $cat, $key = null)
  93. {
  94. return isset($this->config[$uid][$cat][$key])
  95. && $this->config[$uid][$cat][$key] !== '!<unset>!';
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function delete($cat, $key)
  101. {
  102. if (isset($this->config[$cat][$key])) {
  103. unset($this->config[$cat][$key]);
  104. if (count($this->config[$cat]) == 0) {
  105. unset($this->config[$cat]);
  106. }
  107. return true;
  108. } else {
  109. return false;
  110. }
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function loadP($uid, array $config)
  116. {
  117. foreach ($config as $category => $values) {
  118. foreach ($values as $key => $value) {
  119. $this->setP($uid, $category, $key, $value);
  120. }
  121. }
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function getP($uid, $cat, $key = null)
  127. {
  128. if (isset($this->config[$uid][$cat][$key])) {
  129. return $this->config[$uid][$cat][$key];
  130. } else {
  131. return '!<unset>!';
  132. }
  133. }
  134. /**
  135. * {@inheritdoc}
  136. */
  137. public function setP($uid, $cat, $key, $value)
  138. {
  139. // Only arrays are serialized in database, so we have to unserialize sparingly
  140. $value = is_string($value) && preg_match("|^a:[0-9]+:{.*}$|s", $value) ? unserialize($value) : $value;
  141. if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
  142. $this->config[$uid] = [];
  143. }
  144. if (!isset($this->config[$uid][$cat])) {
  145. $this->config[$uid][$cat] = [];
  146. }
  147. $this->config[$uid][$cat][$key] = $value;
  148. return true;
  149. }
  150. /**
  151. * {@inheritdoc}
  152. */
  153. public function deleteP($uid, $cat, $key)
  154. {
  155. if (isset($this->config[$uid][$cat][$key])) {
  156. unset($this->config[$uid][$cat][$key]);
  157. if (count($this->config[$uid][$cat]) == 0) {
  158. unset($this->config[$uid][$cat]);
  159. if (count($this->config[$uid]) == 0) {
  160. unset($this->config[$uid]);
  161. }
  162. }
  163. return true;
  164. } else {
  165. return false;
  166. }
  167. }
  168. /**
  169. * Returns the whole configuration
  170. *
  171. * @return array The configuration
  172. */
  173. public function getAll()
  174. {
  175. return $this->config;
  176. }
  177. }