Config.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace Typecho;
  3. /**
  4. * 配置管理类
  5. *
  6. * @category typecho
  7. * @package Config
  8. * @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
  9. * @license GNU General Public License 2.0
  10. */
  11. class Config implements \Iterator, \ArrayAccess
  12. {
  13. /**
  14. * 当前配置
  15. *
  16. * @access private
  17. * @var array
  18. */
  19. private $currentConfig = [];
  20. /**
  21. * 实例化一个当前配置
  22. *
  23. * @access public
  24. * @param array|string|null $config 配置列表
  25. */
  26. public function __construct($config = [])
  27. {
  28. /** 初始化参数 */
  29. $this->setDefault($config);
  30. }
  31. /**
  32. * 工厂模式实例化一个当前配置
  33. *
  34. * @access public
  35. *
  36. * @param array|string|null $config 配置列表
  37. *
  38. * @return Config
  39. */
  40. public static function factory($config = []): Config
  41. {
  42. return new self($config);
  43. }
  44. /**
  45. * 设置默认的配置
  46. *
  47. * @access public
  48. *
  49. * @param mixed $config 配置信息
  50. * @param boolean $replace 是否替换已经存在的信息
  51. *
  52. * @return void
  53. */
  54. public function setDefault($config, bool $replace = false)
  55. {
  56. if (empty($config)) {
  57. return;
  58. }
  59. /** 初始化参数 */
  60. if (is_string($config)) {
  61. parse_str($config, $params);
  62. } else {
  63. $params = $config;
  64. }
  65. /** 设置默认参数 */
  66. foreach ($params as $name => $value) {
  67. if ($replace || !array_key_exists($name, $this->currentConfig)) {
  68. $this->currentConfig[$name] = $value;
  69. }
  70. }
  71. }
  72. /**
  73. * @return bool
  74. */
  75. public function isEmpty(): bool
  76. {
  77. return empty($this->currentConfig);
  78. }
  79. /**
  80. * 重设指针
  81. *
  82. * @access public
  83. * @return void
  84. */
  85. public function rewind(): void
  86. {
  87. reset($this->currentConfig);
  88. }
  89. /**
  90. * 返回当前值
  91. *
  92. * @access public
  93. * @return mixed
  94. */
  95. #[\ReturnTypeWillChange]
  96. public function current()
  97. {
  98. return current($this->currentConfig);
  99. }
  100. /**
  101. * 指针后移一位
  102. *
  103. * @access public
  104. * @return void
  105. */
  106. public function next(): void
  107. {
  108. next($this->currentConfig);
  109. }
  110. /**
  111. * 获取当前指针
  112. *
  113. * @access public
  114. * @return mixed
  115. */
  116. #[\ReturnTypeWillChange]
  117. public function key()
  118. {
  119. return key($this->currentConfig);
  120. }
  121. /**
  122. * 验证当前值是否到达最后
  123. *
  124. * @access public
  125. * @return boolean
  126. */
  127. public function valid(): bool
  128. {
  129. return false !== $this->current();
  130. }
  131. /**
  132. * 魔术函数获取一个配置值
  133. *
  134. * @access public
  135. * @param string $name 配置名称
  136. * @return mixed
  137. */
  138. public function __get(string $name)
  139. {
  140. return $this->offsetGet($name);
  141. }
  142. /**
  143. * 魔术函数设置一个配置值
  144. *
  145. * @access public
  146. * @param string $name 配置名称
  147. * @param mixed $value 配置值
  148. * @return void
  149. */
  150. public function __set(string $name, $value)
  151. {
  152. $this->offsetSet($name, $value);
  153. }
  154. /**
  155. * 直接输出默认配置值
  156. *
  157. * @access public
  158. * @param string $name 配置名称
  159. * @param array|null $args 参数
  160. * @return void
  161. */
  162. public function __call(string $name, ?array $args)
  163. {
  164. echo $this->currentConfig[$name];
  165. }
  166. /**
  167. * 判断当前配置值是否存在
  168. *
  169. * @access public
  170. * @param string $name 配置名称
  171. * @return boolean
  172. */
  173. public function __isSet(string $name): bool
  174. {
  175. return $this->offsetExists($name);
  176. }
  177. /**
  178. * 魔术方法,打印当前配置数组
  179. *
  180. * @access public
  181. * @return string
  182. */
  183. public function __toString(): string
  184. {
  185. return serialize($this->currentConfig);
  186. }
  187. /**
  188. * @return array
  189. */
  190. public function toArray(): array
  191. {
  192. return $this->currentConfig;
  193. }
  194. /**
  195. * @param mixed $offset
  196. * @return bool
  197. */
  198. public function offsetExists($offset): bool
  199. {
  200. return isset($this->currentConfig[$offset]);
  201. }
  202. /**
  203. * @param mixed $offset
  204. * @return mixed
  205. */
  206. #[\ReturnTypeWillChange]
  207. public function offsetGet($offset)
  208. {
  209. return $this->currentConfig[$offset] ?? null;
  210. }
  211. /**
  212. * @param mixed $offset
  213. * @param mixed $value
  214. */
  215. public function offsetSet($offset, $value): void
  216. {
  217. $this->currentConfig[$offset] = $value;
  218. }
  219. /**
  220. * @param mixed $offset
  221. */
  222. public function offsetUnset($offset): void
  223. {
  224. unset($this->currentConfig[$offset]);
  225. }
  226. }