Config.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2021 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. * @var string
  27. */
  28. protected $path;
  29. /**
  30. * 配置文件后缀
  31. * @var string
  32. */
  33. protected $ext;
  34. /**
  35. * 构造方法
  36. * @access public
  37. */
  38. public function __construct(string $path = null, string $ext = '.php')
  39. {
  40. $this->path = $path ?: '';
  41. $this->ext = $ext;
  42. }
  43. public static function __make(App $app)
  44. {
  45. $path = $app->getConfigPath();
  46. $ext = $app->getConfigExt();
  47. return new static($path, $ext);
  48. }
  49. /**
  50. * 加载配置文件(多种格式)
  51. * @access public
  52. * @param string $file 配置文件名
  53. * @param string $name 一级配置名
  54. * @return array
  55. */
  56. public function load(string $file, string $name = ''): array
  57. {
  58. if (is_file($file)) {
  59. $filename = $file;
  60. } elseif (is_file($this->path . $file . $this->ext)) {
  61. $filename = $this->path . $file . $this->ext;
  62. }
  63. if (isset($filename)) {
  64. return $this->parse($filename, $name);
  65. }
  66. return $this->config;
  67. }
  68. /**
  69. * 解析配置文件
  70. * @access public
  71. * @param string $file 配置文件名
  72. * @param string $name 一级配置名
  73. * @return array
  74. */
  75. protected function parse(string $file, string $name): array
  76. {
  77. $type = pathinfo($file, PATHINFO_EXTENSION);
  78. $config = [];
  79. switch ($type) {
  80. case 'php':
  81. $config = include $file;
  82. break;
  83. case 'yml':
  84. case 'yaml':
  85. if (function_exists('yaml_parse_file')) {
  86. $config = yaml_parse_file($file);
  87. }
  88. break;
  89. case 'ini':
  90. $config = parse_ini_file($file, true, INI_SCANNER_TYPED) ?: [];
  91. break;
  92. case 'json':
  93. $config = json_decode(file_get_contents($file), true);
  94. break;
  95. }
  96. return is_array($config) ? $this->set($config, strtolower($name)) : [];
  97. }
  98. /**
  99. * 检测配置是否存在
  100. * @access public
  101. * @param string $name 配置参数名(支持多级配置 .号分割)
  102. * @return bool
  103. */
  104. public function has(string $name): bool
  105. {
  106. if (false === strpos($name, '.') && !isset($this->config[strtolower($name)])) {
  107. return false;
  108. }
  109. return !is_null($this->get($name));
  110. }
  111. /**
  112. * 获取一级配置
  113. * @access protected
  114. * @param string $name 一级配置名
  115. * @return array
  116. */
  117. protected function pull(string $name): array
  118. {
  119. $name = strtolower($name);
  120. return $this->config[$name] ?? [];
  121. }
  122. /**
  123. * 获取配置参数 为空则获取所有配置
  124. * @access public
  125. * @param string $name 配置参数名(支持多级配置 .号分割)
  126. * @param mixed $default 默认值
  127. * @return mixed
  128. */
  129. public function get(string $name = null, $default = null)
  130. {
  131. // 无参数时获取所有
  132. if (empty($name)) {
  133. return $this->config;
  134. }
  135. if (false === strpos($name, '.')) {
  136. return $this->pull($name);
  137. }
  138. $name = explode('.', $name);
  139. $name[0] = strtolower($name[0]);
  140. $config = $this->config;
  141. // 按.拆分成多维数组进行判断
  142. foreach ($name as $val) {
  143. if (isset($config[$val])) {
  144. $config = $config[$val];
  145. } else {
  146. return $default;
  147. }
  148. }
  149. return $config;
  150. }
  151. /**
  152. * 设置配置参数 name为数组则为批量设置
  153. * @access public
  154. * @param array $config 配置参数
  155. * @param string $name 配置名
  156. * @return array
  157. */
  158. public function set(array $config, string $name = null): array
  159. {
  160. if (!empty($name)) {
  161. if (isset($this->config[$name])) {
  162. $result = array_merge($this->config[$name], $config);
  163. } else {
  164. $result = $config;
  165. }
  166. $this->config[$name] = $result;
  167. } else {
  168. $result = $this->config = array_merge($this->config, array_change_key_case($config));
  169. }
  170. return $result;
  171. }
  172. }