Config.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 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. namespace think;
  12. class Config
  13. {
  14. /**
  15. * @var array 配置参数
  16. */
  17. private static $config = [];
  18. /**
  19. * @var string 参数作用域
  20. */
  21. private static $range = '_sys_';
  22. /**
  23. * 设定配置参数的作用域
  24. * @access public
  25. * @param string $range 作用域
  26. * @return void
  27. */
  28. public static function range($range)
  29. {
  30. self::$range = $range;
  31. if (!isset(self::$config[$range])) self::$config[$range] = [];
  32. }
  33. /**
  34. * 解析配置文件或内容
  35. * @access public
  36. * @param string $config 配置文件路径或内容
  37. * @param string $type 配置解析类型
  38. * @param string $name 配置名(如设置即表示二级配置)
  39. * @param string $range 作用域
  40. * @return mixed
  41. */
  42. public static function parse($config, $type = '', $name = '', $range = '')
  43. {
  44. $range = $range ?: self::$range;
  45. if (empty($type)) $type = pathinfo($config, PATHINFO_EXTENSION);
  46. $class = false !== strpos($type, '\\') ?
  47. $type :
  48. '\\think\\config\\driver\\' . ucwords($type);
  49. return self::set((new $class())->parse($config), $name, $range);
  50. }
  51. /**
  52. * 加载配置文件(PHP格式)
  53. * @access public
  54. * @param string $file 配置文件名
  55. * @param string $name 配置名(如设置即表示二级配置)
  56. * @param string $range 作用域
  57. * @return mixed
  58. */
  59. public static function load($file, $name = '', $range = '')
  60. {
  61. $range = $range ?: self::$range;
  62. if (!isset(self::$config[$range])) self::$config[$range] = [];
  63. if (is_file($file)) {
  64. $name = strtolower($name);
  65. $type = pathinfo($file, PATHINFO_EXTENSION);
  66. if ('php' == $type) {
  67. return self::set(include $file, $name, $range);
  68. }
  69. if ('yaml' == $type && function_exists('yaml_parse_file')) {
  70. return self::set(yaml_parse_file($file), $name, $range);
  71. }
  72. return self::parse($file, $type, $name, $range);
  73. }
  74. return self::$config[$range];
  75. }
  76. /**
  77. * 检测配置是否存在
  78. * @access public
  79. * @param string $name 配置参数名(支持二级配置 . 号分割)
  80. * @param string $range 作用域
  81. * @return bool
  82. */
  83. public static function has($name, $range = '')
  84. {
  85. $range = $range ?: self::$range;
  86. if (!strpos($name, '.')) {
  87. return isset(self::$config[$range][strtolower($name)]);
  88. }
  89. // 二维数组设置和获取支持
  90. $name = explode('.', $name, 2);
  91. return isset(self::$config[$range][strtolower($name[0])][$name[1]]);
  92. }
  93. /**
  94. * 获取配置参数 为空则获取所有配置
  95. * @access public
  96. * @param string $name 配置参数名(支持二级配置 . 号分割)
  97. * @param string $range 作用域
  98. * @return mixed
  99. */
  100. public static function get($name = null, $range = '')
  101. {
  102. $range = $range ?: self::$range;
  103. // 无参数时获取所有
  104. if (empty($name) && isset(self::$config[$range])) {
  105. return self::$config[$range];
  106. }
  107. // 非二级配置时直接返回
  108. if (!strpos($name, '.')) {
  109. $name = strtolower($name);
  110. return isset(self::$config[$range][$name]) ? self::$config[$range][$name] : null;
  111. }
  112. // 二维数组设置和获取支持
  113. $name = explode('.', $name, 2);
  114. $name[0] = strtolower($name[0]);
  115. if (!isset(self::$config[$range][$name[0]])) {
  116. // 动态载入额外配置
  117. $module = Request::instance()->module();
  118. $file = CONF_PATH . ($module ? $module . DS : '') . 'extra' . DS . $name[0] . CONF_EXT;
  119. is_file($file) && self::load($file, $name[0]);
  120. }
  121. return isset(self::$config[$range][$name[0]][$name[1]]) ?
  122. self::$config[$range][$name[0]][$name[1]] :
  123. null;
  124. }
  125. /**
  126. * 设置配置参数 name 为数组则为批量设置
  127. * @access public
  128. * @param string|array $name 配置参数名(支持二级配置 . 号分割)
  129. * @param mixed $value 配置值
  130. * @param string $range 作用域
  131. * @return mixed
  132. */
  133. public static function set($name, $value = null, $range = '')
  134. {
  135. $range = $range ?: self::$range;
  136. if (!isset(self::$config[$range])) self::$config[$range] = [];
  137. // 字符串则表示单个配置设置
  138. if (is_string($name)) {
  139. if (!strpos($name, '.')) {
  140. self::$config[$range][strtolower($name)] = $value;
  141. } else {
  142. // 二维数组
  143. $name = explode('.', $name, 2);
  144. self::$config[$range][strtolower($name[0])][$name[1]] = $value;
  145. }
  146. return $value;
  147. }
  148. // 数组则表示批量设置
  149. if (is_array($name)) {
  150. if (!empty($value)) {
  151. self::$config[$range][$value] = isset(self::$config[$range][$value]) ?
  152. array_merge(self::$config[$range][$value], $name) :
  153. $name;
  154. return self::$config[$range][$value];
  155. }
  156. return self::$config[$range] = array_merge(
  157. self::$config[$range], array_change_key_case($name)
  158. );
  159. }
  160. // 为空直接返回已有配置
  161. return self::$config[$range];
  162. }
  163. /**
  164. * 重置配置参数
  165. * @access public
  166. * @param string $range 作用域
  167. * @return void
  168. */
  169. public static function reset($range = '')
  170. {
  171. $range = $range ?: self::$range;
  172. if (true === $range) {
  173. self::$config = [];
  174. } else {
  175. self::$config[$range] = [];
  176. }
  177. }
  178. }