Files
cunkebao_v3/Server/thinkphp/library/think/Config.php

215 lines
6.4 KiB
PHP
Raw Normal View History

2025-04-02 16:38:48 +08:00
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think;
2025-07-07 11:31:25 +08:00
class Config
2025-04-02 16:38:48 +08:00
{
/**
2025-07-07 11:31:25 +08:00
* @var array 配置参数
2025-04-02 16:38:48 +08:00
*/
2025-07-07 11:31:25 +08:00
private static $config = [];
2025-04-02 16:38:48 +08:00
/**
2025-07-07 11:31:25 +08:00
* @var string 参数作用域
2025-04-02 16:38:48 +08:00
*/
2025-07-07 11:31:25 +08:00
private static $range = '_sys_';
2025-04-02 16:38:48 +08:00
/**
2025-07-07 11:31:25 +08:00
* 设定配置参数的作用域
2025-04-02 16:38:48 +08:00
* @access public
2025-07-07 11:31:25 +08:00
* @param string $range 作用域
2025-04-02 16:38:48 +08:00
* @return void
*/
2025-07-07 11:31:25 +08:00
public static function range($range)
2025-04-02 16:38:48 +08:00
{
2025-07-07 11:31:25 +08:00
self::$range = $range;
2025-04-02 16:38:48 +08:00
2025-07-07 11:31:25 +08:00
if (!isset(self::$config[$range])) self::$config[$range] = [];
2025-04-02 16:38:48 +08:00
}
/**
* 解析配置文件或内容
* @access public
2025-07-07 11:31:25 +08:00
* @param string $config 配置文件路径或内容
* @param string $type 配置解析类型
* @param string $name 配置名(如设置即表示二级配置)
* @param string $range 作用域
2025-04-02 16:38:48 +08:00
* @return mixed
*/
2025-07-07 11:31:25 +08:00
public static function parse($config, $type = '', $name = '', $range = '')
2025-04-02 16:38:48 +08:00
{
2025-07-07 11:31:25 +08:00
$range = $range ?: self::$range;
2025-04-02 16:38:48 +08:00
2025-07-07 11:31:25 +08:00
if (empty($type)) $type = pathinfo($config, PATHINFO_EXTENSION);
2025-04-02 16:38:48 +08:00
2025-07-07 11:31:25 +08:00
$class = false !== strpos($type, '\\') ?
$type :
'\\think\\config\\driver\\' . ucwords($type);
return self::set((new $class())->parse($config), $name, $range);
2025-04-02 16:38:48 +08:00
}
/**
2025-07-07 11:31:25 +08:00
* 加载配置文件PHP格式
2025-04-02 16:38:48 +08:00
* @access public
2025-07-07 11:31:25 +08:00
* @param string $file 配置文件名
* @param string $name 配置名(如设置即表示二级配置)
* @param string $range 作用域
2025-04-02 16:38:48 +08:00
* @return mixed
*/
2025-07-07 11:31:25 +08:00
public static function load($file, $name = '', $range = '')
2025-04-02 16:38:48 +08:00
{
2025-07-07 11:31:25 +08:00
$range = $range ?: self::$range;
2025-04-02 16:38:48 +08:00
2025-07-07 11:31:25 +08:00
if (!isset(self::$config[$range])) self::$config[$range] = [];
2025-04-02 16:38:48 +08:00
2025-07-07 11:31:25 +08:00
if (is_file($file)) {
$name = strtolower($name);
$type = pathinfo($file, PATHINFO_EXTENSION);
2025-04-02 16:38:48 +08:00
2025-07-07 11:31:25 +08:00
if ('php' == $type) {
return self::set(include $file, $name, $range);
2025-04-02 16:38:48 +08:00
}
2025-07-07 11:31:25 +08:00
if ('yaml' == $type && function_exists('yaml_parse_file')) {
return self::set(yaml_parse_file($file), $name, $range);
}
2025-04-02 16:38:48 +08:00
2025-07-07 11:31:25 +08:00
return self::parse($file, $type, $name, $range);
2025-04-02 16:38:48 +08:00
}
2025-07-07 11:31:25 +08:00
return self::$config[$range];
2025-04-02 16:38:48 +08:00
}
/**
* 检测配置是否存在
* @access public
2025-07-07 11:31:25 +08:00
* @param string $name 配置参数名(支持二级配置 . 号分割)
* @param string $range 作用域
2025-04-02 16:38:48 +08:00
* @return bool
*/
2025-07-07 11:31:25 +08:00
public static function has($name, $range = '')
2025-04-02 16:38:48 +08:00
{
2025-07-07 11:31:25 +08:00
$range = $range ?: self::$range;
2025-04-02 16:38:48 +08:00
2025-07-07 11:31:25 +08:00
if (!strpos($name, '.')) {
return isset(self::$config[$range][strtolower($name)]);
2025-04-02 16:38:48 +08:00
}
2025-07-07 11:31:25 +08:00
// 二维数组设置和获取支持
$name = explode('.', $name, 2);
return isset(self::$config[$range][strtolower($name[0])][$name[1]]);
2025-04-02 16:38:48 +08:00
}
/**
* 获取配置参数 为空则获取所有配置
* @access public
2025-07-07 11:31:25 +08:00
* @param string $name 配置参数名(支持二级配置 . 号分割)
* @param string $range 作用域
2025-04-02 16:38:48 +08:00
* @return mixed
*/
2025-07-07 11:31:25 +08:00
public static function get($name = null, $range = '')
2025-04-02 16:38:48 +08:00
{
2025-07-07 11:31:25 +08:00
$range = $range ?: self::$range;
2025-04-02 16:38:48 +08:00
// 无参数时获取所有
2025-07-07 11:31:25 +08:00
if (empty($name) && isset(self::$config[$range])) {
return self::$config[$range];
2025-04-02 16:38:48 +08:00
}
2025-07-07 11:31:25 +08:00
// 非二级配置时直接返回
if (!strpos($name, '.')) {
$name = strtolower($name);
return isset(self::$config[$range][$name]) ? self::$config[$range][$name] : null;
2025-04-02 16:38:48 +08:00
}
2025-07-07 11:31:25 +08:00
// 二维数组设置和获取支持
$name = explode('.', $name, 2);
2025-04-02 16:38:48 +08:00
$name[0] = strtolower($name[0]);
2025-07-07 11:31:25 +08:00
if (!isset(self::$config[$range][$name[0]])) {
// 动态载入额外配置
$module = Request::instance()->module();
$file = CONF_PATH . ($module ? $module . DS : '') . 'extra' . DS . $name[0] . CONF_EXT;
is_file($file) && self::load($file, $name[0]);
2025-04-02 16:38:48 +08:00
}
2025-07-07 11:31:25 +08:00
return isset(self::$config[$range][$name[0]][$name[1]]) ?
self::$config[$range][$name[0]][$name[1]] :
null;
2025-04-02 16:38:48 +08:00
}
/**
2025-07-07 11:31:25 +08:00
* 设置配置参数 name 为数组则为批量设置
2025-04-02 16:38:48 +08:00
* @access public
2025-07-07 11:31:25 +08:00
* @param string|array $name 配置参数名(支持二级配置 . 号分割)
* @param mixed $value 配置值
* @param string $range 作用域
2025-04-02 16:38:48 +08:00
* @return mixed
*/
2025-07-07 11:31:25 +08:00
public static function set($name, $value = null, $range = '')
2025-04-02 16:38:48 +08:00
{
2025-07-07 11:31:25 +08:00
$range = $range ?: self::$range;
2025-04-02 16:38:48 +08:00
2025-07-07 11:31:25 +08:00
if (!isset(self::$config[$range])) self::$config[$range] = [];
2025-04-02 16:38:48 +08:00
2025-07-07 11:31:25 +08:00
// 字符串则表示单个配置设置
if (is_string($name)) {
if (!strpos($name, '.')) {
self::$config[$range][strtolower($name)] = $value;
2025-04-02 16:38:48 +08:00
} else {
2025-07-07 11:31:25 +08:00
// 二维数组
$name = explode('.', $name, 2);
self::$config[$range][strtolower($name[0])][$name[1]] = $value;
2025-04-02 16:38:48 +08:00
}
return $value;
2025-07-07 11:31:25 +08:00
}
// 数组则表示批量设置
if (is_array($name)) {
2025-04-02 16:38:48 +08:00
if (!empty($value)) {
2025-07-07 11:31:25 +08:00
self::$config[$range][$value] = isset(self::$config[$range][$value]) ?
array_merge(self::$config[$range][$value], $name) :
$name;
2025-04-02 16:38:48 +08:00
2025-07-07 11:31:25 +08:00
return self::$config[$range][$value];
2025-04-02 16:38:48 +08:00
}
2025-07-07 11:31:25 +08:00
return self::$config[$range] = array_merge(
self::$config[$range], array_change_key_case($name)
);
2025-04-02 16:38:48 +08:00
}
2025-07-07 11:31:25 +08:00
// 为空直接返回已有配置
return self::$config[$range];
2025-04-02 16:38:48 +08:00
}
/**
* 重置配置参数
* @access public
2025-07-07 11:31:25 +08:00
* @param string $range 作用域
2025-04-02 16:38:48 +08:00
* @return void
*/
2025-07-07 11:31:25 +08:00
public static function reset($range = '')
2025-04-02 16:38:48 +08:00
{
2025-07-07 11:31:25 +08:00
$range = $range ?: self::$range;
if (true === $range) {
self::$config = [];
2025-04-02 16:38:48 +08:00
} else {
2025-07-07 11:31:25 +08:00
self::$config[$range] = [];
2025-04-02 16:38:48 +08:00
}
}
}