f10@t's blog

ThinkPHP框架学习---config类和config助手函数

字数统计: 2.6k阅读时长: 12 min
2018/11/25

All of the problems of the world could be settled easily if men were only willing to think.

Config类

  这个类中有许多常用的方法,这里学习一下它的源码。   这个类的定义位于thinkphp目录下的think目录的Config.php中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?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;

class Config
{
/**
* @var array 配置参数
*/
private static $config = [];

/**
* @var string 参数作用域
*/
private static $range = '_sys_';

/**
* 设定配置参数的作用域
* @access public
* @param string $range 作用域
* @return void
*/
public static function range($range)
{
self::$range = $range;

if (!isset(self::$config[$range])) self::$config[$range] = [];
}

/**
* 解析配置文件或内容
* @access public
* @param string $config 配置文件路径或内容
* @param string $type 配置解析类型
* @param string $name 配置名(如设置即表示二级配置)
* @param string $range 作用域
* @return mixed
*/
public static function parse($config, $type = '', $name = '', $range = '')
{
$range = $range ?: self::$range;

if (empty($type)) $type = pathinfo($config, PATHINFO_EXTENSION);

$class = false !== strpos($type, '\\') ?
$type :
'\\think\\config\\driver\\' . ucwords($type);

return self::set((new $class())->parse($config), $name, $range);
}

/**
* 加载配置文件(PHP格式)
* @access public
* @param string $file 配置文件名
* @param string $name 配置名(如设置即表示二级配置)
* @param string $range 作用域
* @return mixed
*/
public static function load($file, $name = '', $range = '')
{
$range = $range ?: self::$range;

if (!isset(self::$config[$range])) self::$config[$range] = [];

if (is_file($file)) {
$name = strtolower($name);
$type = pathinfo($file, PATHINFO_EXTENSION);

if ('php' == $type) {
return self::set(include $file, $name, $range);
}

if ('yaml' == $type && function_exists('yaml_parse_file')) {
return self::set(yaml_parse_file($file), $name, $range);
}

return self::parse($file, $type, $name, $range);
}

return self::$config[$range];
}

/**
* 检测配置是否存在
* @access public
* @param string $name 配置参数名(支持二级配置 . 号分割)
* @param string $range 作用域
* @return bool
*/
public static function has($name, $range = '')
{
$range = $range ?: self::$range;

if (!strpos($name, '.')) {
return isset(self::$config[$range][strtolower($name)]);
}

// 二维数组设置和获取支持
$name = explode('.', $name, 2);
return isset(self::$config[$range][strtolower($name[0])][$name[1]]);
}

/**
* 获取配置参数 为空则获取所有配置
* @access public
* @param string $name 配置参数名(支持二级配置 . 号分割)
* @param string $range 作用域
* @return mixed
*/
public static function get($name = null, $range = '')
{
$range = $range ?: self::$range;

// 无参数时获取所有
if (empty($name) && isset(self::$config[$range])) {
return self::$config[$range];
}

// 非二级配置时直接返回
if (!strpos($name, '.')) {
$name = strtolower($name);
return isset(self::$config[$range][$name]) ? self::$config[$range][$name] : null;
}

// 二维数组设置和获取支持
$name = explode('.', $name, 2);
$name[0] = strtolower($name[0]);

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]);
}

return isset(self::$config[$range][$name[0]][$name[1]]) ?
self::$config[$range][$name[0]][$name[1]] :
null;
}

/**
* 设置配置参数 name 为数组则为批量设置
* @access public
* @param string|array $name 配置参数名(支持二级配置 . 号分割)
* @param mixed $value 配置值
* @param string $range 作用域
* @return mixed
*/
public static function set($name, $value = null, $range = '')
{
$range = $range ?: self::$range;

if (!isset(self::$config[$range])) self::$config[$range] = [];

// 字符串则表示单个配置设置
if (is_string($name)) {
if (!strpos($name, '.')) {
self::$config[$range][strtolower($name)] = $value;
} else {
// 二维数组
$name = explode('.', $name, 2);
self::$config[$range][strtolower($name[0])][$name[1]] = $value;
}

return $value;
}

// 数组则表示批量设置
if (is_array($name)) {
if (!empty($value)) {
self::$config[$range][$value] = isset(self::$config[$range][$value]) ?
array_merge(self::$config[$range][$value], $name) :
$name;

return self::$config[$range][$value];
}

return self::$config[$range] = array_merge(
self::$config[$range], array_change_key_case($name)
);
}

// 为空直接返回已有配置
return self::$config[$range];
}

/**
* 重置配置参数
* @access public
* @param string $range 作用域
* @return void
*/
public static function reset($range = '')
{
$range = $range ?: self::$range;

if (true === $range) {
self::$config = [];
} else {
self::$config[$range] = [];
}
}
}
  首先建立了两个私有静态变量,$range$config。分别用于获取参数作用域和配置参数。   #### range()方法   官方注释:
1
2
3
4
5
6
/**
* 设定配置参数的作用域
* @access public
* @param string $range 作用域
* @return void
*/
  1. 作用:设定作用域   2. 参数:$range作用域   3. 返回值:空   方法源码:
1
2
3
4
5
6
public static function range($range)
{
self::$range = $range;

if (!isset(self::$config[$range])) self::$config[$range] = [];
}
  将传入的参数作用域赋予静态变量range,并判断\(config变量中是否存在range对应的key值,如果没有的话,将其设为一个空数组。    #### parse方法   官方注释:
1
2
3
4
5
6
7
8
9
/**
* 解析配置文件或内容
* @access public
* @param string $config 配置文件路径或内容
* @param string $type 配置解析类型
* @param string $name 配置名(如设置即表示二级配置)
* @param string $range 作用域
* @return mixed
*/
  1. 作用:解析配置文件或内容   2. 参数:`\)
config配置文件的路径或内容,\(type`解析类型、`、\)name配置名、\(range`作用域   3. 返回值:混合   源码如下:
1
2
3
4
5
6
7
8
9
10
11
12
public static function parse($config, $type = '', $name = '', $range = '')
{
$range = $range ?: self::$range;

if (empty($type)) $type = pathinfo($config, PATHINFO_EXTENSION);

$class = false !== strpos($type, '\\') ?
$type :
'\\think\\config\\driver\\' . ucwords($type);

return self::set((new $class())->parse($config), $name, $range);
}
  该函数共有四个参数,config,type,name,range。   首先判断参数range是否为空,若为空,则使用默认的类定义的默认range值;非空不进行操作。   判断`\)
type参数是否为空,如果为空的话,将其赋值为\(config`变量(路径)下的文件类型(比如php、txt等)。   如果`\)type变量中没有'/'字符的话,strpos函数返回false,不等式不成立,将
1
'\\think\\config\\driver\\'.ucwords($type)
`加上$type变量字符串的每个单词首字母大写后的结果。 赋给$class;如果含有/字符的话,strpos有返回值,不等式成立,将$type赋给$class。   prase函数的返回值为使用Config类的静态函数set,将    #### load方法   官方注释:
1
2
3
4
5
6
7
8
/**
* 加载配置文件(PHP格式)
* @access public
* @param string $file 配置文件名
* @param string $name 配置名(如设置即表示二级配置)
* @param string $range 作用域
* @return mixed
*/
  1. 作用:加载PHP配置文件   2. 参数:$file要加载的配置文件名、$name配置名、$range作用域   3. 返回值:混合   源码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static function load($file, $name = '', $range = '')
{
$range = $range ?: self::$range;

if (!isset(self::$config[$range])) self::$config[$range] = [];

if (is_file($file)) {
$name = strtolower($name);
$type = pathinfo($file, PATHINFO_EXTENSION);

if ('php' == $type) {
return self::set(include $file, $name, $range);
}

if ('yaml' == $type && function_exists('yaml_parse_file')) {
return self::set(yaml_parse_file($file), $name, $range);
}

return self::parse($file, $type, $name, $range);
}

return self::$config[$range];
}
  接受三个参数:file,设参数传入的$name$range均为字符串类型。   若接收的$range参数为空,将默认range传入;否则无操作。   接着判断$config下是否存在$range对应的值,若不存在,为其创建一个空值。   接着判断传入的文件格式是否正确,如果文件传入正确,将参数$name转为小写。将$file的文件后缀名赋给type。   1. 如果类型是php,就   2. 如果是yaml文件,且存在yaml_parse_file方法,就   3. 如果不是上面的两种文件类型,就将它传入parse()函数进行去解析。   最后返回$config$range对应值。

has()方法

  官方注释:

1
2
3
4
5
6
7
/**
* 检测配置是否存在
* @access public
* @param string $name 配置参数名(支持二级配置 . 号分割)
* @param string $range 作用域
* @return bool
*/
  1. 作用:检测配置是否存在   2. 参数:$name配置参数名、$range作用域   3. 返回值:布尔值   源码如下:
1
2
3
4
5
6
7
8
9
10
11
12
public static function has($name, $range = '')
{
$range = $range ?: self::$range;

if (!strpos($name, '.')) {
return isset(self::$config[$range][strtolower($name)]);
}

// 二维数组设置和获取支持
$name = explode('.', $name, 2);
return isset(self::$config[$range][strtolower($name[0])][$name[1]]);
}
  还是先检查参数$range,为空则使用默认,否则无变化。   如果$name参数中没有有.字符,说明是一级配置,返回$config数组中$range对应的值为全小写的$name的isset值。   否则为二级配置,使用explode函数将$name按'.'字符分割,并将第二部分值赋给$name,返回$config数组中的$range对应的数组中为全小写的$name[0]的数组中的对应为$name[1]的值的isset值。

get()方法

  官方注释:

1
2
3
4
5
6
7
/**
* 获取配置参数 为空则获取所有配置
* @access public
* @param string $name 配置参数名(支持二级配置 . 号分割)
* @param string $range 作用域
* @return mixed
*/
  1. 作用:获取配置参数   2. 参数:$name配置参数名、$range作用域   3. 返回值:混合   源码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public static function get($name = null, $range = '')
{
$range = $range ?: self::$range;

// 无参数时获取所有
if (empty($name) && isset(self::$config[$range])) {
return self::$config[$range];
}

// 非二级配置时直接返回
if (!strpos($name, '.')) {
$name = strtolower($name);
return isset(self::$config[$range][$name]) ? self::$config[$range][$name] : null;
}

// 二维数组设置和获取支持
$name = explode('.', $name, 2);
$name[0] = strtolower($name[0]);

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]);
}

return isset(self::$config[$range][$name[0]][$name[1]]) ?
self::$config[$range][$name[0]][$name[1]] :
null;
}
#### set()方法   官方注释:
1
2
3
4
5
6
7
8
/**
* 设置配置参数 name 为数组则为批量设置
* @access public
* @param string|array $name 配置参数名(支持二级配置 . 号分割)
* @param mixed $value 配置值
* @param string $range 作用域
* @return mixed
*/
  1. 作用:设置配置参数   2. 参数:$name配置参数名(可为数组或字符串类型)、$value要配置的值、$value作用域。   3. 返回值:混合   源码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public static function set($name, $value = null, $range = '')
{
$range = $range ?: self::$range;

if (!isset(self::$config[$range])) self::$config[$range] = [];

// 字符串则表示单个配置设置
if (is_string($name)) {
if (!strpos($name, '.')) {
self::$config[$range][strtolower($name)] = $value;
} else {
// 二维数组
$name = explode('.', $name, 2);
self::$config[$range][strtolower($name[0])][$name[1]] = $value;
}

return $value;
}

// 数组则表示批量设置
if (is_array($name)) {
if (!empty($value)) {
self::$config[$range][$value] = isset(self::$config[$range][$value]) ?
array_merge(self::$config[$range][$value], $name) :
$name;

return self::$config[$range][$value];
}

return self::$config[$range] = array_merge(
self::$config[$range], array_change_key_case($name)
);
}

// 为空直接返回已有配置
return self::$config[$range];
}
#### reset()方法   官方注释:
1
2
3
4
5
6
/**
* 重置配置参数
* @access public
* @param string $range 作用域
* @return void
*/
  1. 作用:重置配置参数   2. 参数:$range作用域   3. 返回值:空   源码如下:
1
2
3
4
5
6
7
8
9
10
public static function reset($range = '')
{
$range = $range ?: self::$range;

if (true === $range) {
self::$config = [];
} else {
self::$config[$range] = [];
}
}

CATALOG
  1. 1. Config类
    1. 1.1. has()方法
    2. 1.2. get()方法