forked from simonsww/edpx-zhixin
/
Conf.php
158 lines (139 loc) · 4.13 KB
/
Conf.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
<?php
/***************************************************************************
*
* Copyright (c) 2011 Baidu.com, Inc. All Rights Reserved
*
**************************************************************************/
/**
* @file Conf.php
* @author hanshinan(com@baidu.com)
* @date 2011/10/21 15:30:33
* @brief
*
**/
class Bd_Conf
{
protected static $set_conf = null;
/*
* 获取配置,重载了父类的定义,添加setConf的支持
*
* @param:
* $item: 指定配置项,表示获取全部配置
* $idc: 指定Idc名称,传null时,表示采用当前Idc
*
* @return
* array: 成功
* string: 成功
* false: 失败
*/
public static function getConf($item, $idc = null) {
if ($idc == null)
$conf = parent::getConf($item);
else
$conf = parent::getConf($item, $idc);
if (self::$set_conf == null)
return $conf;
if (empty($item)) return false;
//parse query string
if ($item[0] != '/') {
$item = self::getLevel() . "/$item";
}
$query = explode('/', $item);
$value = self::$set_conf;
while(($node = array_shift($query)) !== null) {
if ($node === '') continue;
if (isset($value[$node])) {
$value = $value[$node];
} else {
$value = null;
}
}
if ($value === null)
return $conf;
else if (is_array($value) && is_array($conf))
return self::my_array_merge_recursive($conf, $value);
else
return $value;
}
/*
* 设置配置项,调试时使用。下次getConf时可获取这里设置的值
*
* @param:
* $item: 指定配置项,表示获取全部配置
* $value: 指定要设置成的值
*
* @return void
*/
public static function setConf($item, $value)
{
// setLevel
if ($item[0] != '/') {
$item = self::getLevel() . "/$item";
}
// root has to be an array
if (($item == '' || $item == '/') && !is_array($value)) {
return;
}
// parse query string
$query = explode('/', $item);
// build array
$built = $value;
while (($node = array_pop($query)) !== null) {
if ($node === '') continue;
$built = array($node => $built);
}
// merge with global set_conf
if (self::$set_conf == null) {
self::$set_conf = $built;
} else {
self::$set_conf = self::my_array_merge_recursive(self::$set_conf, $built);
}
}
/*
* 获取当前App或指定App的配置
*
* @param:
* $item: 指定子配置项,传null时表示获取全部配置
* $app: 指定App名称,传null时,表示采用当前App
*
* @return
* array: 成功
* string: 成功
* false: 失败
*/
public static function getAppConf($item = null, $app = null)
{
//和ODP环境解耦
if(!defined('IS_ODP')){
return self::getConf($item);
}
$conf_path = Bd_AppEnv::getEnv('conf', $app);
if(!empty($item))
{
$conf_path .= "/$item";
}
return self::getConf($conf_path);
}
public static function getMyConf($item = null, $app = null)
{
return self::getAppConf($item, $app);
}
// helper functions
private static function my_array_merge_recursive() {
$arrays = func_get_args();
$base = array_shift($arrays);
foreach ($arrays as $array) {
reset($base); //important
while (list($key, $value) = @each($array)) {
if (is_array($value) && @is_array($base[$key])) {
$base[$key] = self::my_array_merge_recursive($base[$key], $value);
} else {
$base[$key] = $value;
}
}
}
return $base;
}
}
/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */
?>