Config.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2023 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. declare(strict_types=1);
  12. namespace think;
  13. /**
  14. * 配置管理类
  15. * @package think
  16. */
  17. class Config
  18. {
  19. /**
  20. * 配置参数
  21. * @var array
  22. */
  23. protected $config = [];
  24. /**
  25. * 构造方法
  26. * @access public
  27. */
  28. public function __construct(protected string $path = '', protected string $ext = '.php')
  29. {
  30. }
  31. public static function __make(App $app)
  32. {
  33. $path = $app->getConfigPath();
  34. $ext = $app->getConfigExt();
  35. return new static($path, $ext);
  36. }
  37. /**
  38. * 加载配置文件(多种格式)
  39. * @access public
  40. * @param string $file 配置文件名
  41. * @param string $name 一级配置名
  42. * @return array
  43. */
  44. public function load(string $file, string $name = ''): array
  45. {
  46. if (is_file($file)) {
  47. $filename = $file;
  48. } elseif (is_file($this->path . $file . $this->ext)) {
  49. $filename = $this->path . $file . $this->ext;
  50. }
  51. if (isset($filename)) {
  52. return $this->parse($filename, $name);
  53. }
  54. return $this->config;
  55. }
  56. /**
  57. * 解析配置文件
  58. * @access public
  59. * @param string $file 配置文件名
  60. * @param string $name 一级配置名
  61. * @return array
  62. */
  63. protected function parse(string $file, string $name): array
  64. {
  65. $type = pathinfo($file, PATHINFO_EXTENSION);
  66. $config = [];
  67. $config = match ($type) {
  68. 'php' => include $file,
  69. 'yml','yaml' => function_exists('yaml_parse_file') ? yaml_parse_file($file) : [],
  70. 'ini' => parse_ini_file($file, true, INI_SCANNER_TYPED) ?: [],
  71. 'json' => json_decode(file_get_contents($file), true),
  72. default => [],
  73. };
  74. return is_array($config) ? $this->set($config, strtolower($name)) : [];
  75. }
  76. /**
  77. * 检测配置是否存在
  78. * @access public
  79. * @param string $name 配置参数名(支持多级配置 .号分割)
  80. * @return bool
  81. */
  82. public function has(string $name): bool
  83. {
  84. if (!str_contains($name, '.') && !isset($this->config[strtolower($name)])) {
  85. return false;
  86. }
  87. return !is_null($this->get($name));
  88. }
  89. /**
  90. * 获取一级配置
  91. * @access protected
  92. * @param string $name 一级配置名
  93. * @return array
  94. */
  95. protected function pull(string $name): array
  96. {
  97. $name = strtolower($name);
  98. return $this->config[$name] ?? [];
  99. }
  100. /**
  101. * 获取配置参数 为空则获取所有配置
  102. * @access public
  103. * @param string $name 配置参数名(支持多级配置 .号分割)
  104. * @param mixed $default 默认值
  105. * @return mixed
  106. */
  107. public function get(string $name = null, $default = null)
  108. {
  109. // 无参数时获取所有
  110. if (empty($name)) {
  111. return $this->config;
  112. }
  113. if (!str_contains($name, '.')) {
  114. return $this->pull($name);
  115. }
  116. $name = explode('.', $name);
  117. $name[0] = strtolower($name[0]);
  118. $config = $this->config;
  119. // 按.拆分成多维数组进行判断
  120. foreach ($name as $val) {
  121. if (isset($config[$val])) {
  122. $config = $config[$val];
  123. } else {
  124. return $default;
  125. }
  126. }
  127. return $config;
  128. }
  129. /**
  130. * 设置配置参数 name为数组则为批量设置
  131. * @access public
  132. * @param array $config 配置参数
  133. * @param string $name 配置名
  134. * @return array
  135. */
  136. public function set(array $config, string $name = null): array
  137. {
  138. if (empty($name)) {
  139. $this->config = array_merge($this->config, array_change_key_case($config));
  140. return $this->config;
  141. }
  142. if (isset($this->config[$name])) {
  143. $result = array_merge($this->config[$name], $config);
  144. } else {
  145. $result = $config;
  146. }
  147. $this->config[$name] = $result;
  148. return $result;
  149. }
  150. }