-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.php
193 lines (180 loc) · 4.14 KB
/
common.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
<?php
/**
* common.php
* Author: Ethan
* CreateTime: 2015/12/20 19:44
* Description:
*/
/*
* 自动加载需要的class并局部保存
*
* @param string class名称
* @param array class初始化时的参数,以数组形式传递
* @return object
*/
if(!function_exists('loadClass'))
{
function loadClass($class, $param = NULL)
{
if(!is_string($class))
{
logMsg(SL_ERROR,"参数错误!");
exit(-3);
}
static $commonClasses = array();
if(isset($commonClasses[$class]) && $commonClasses[$class] instanceof $class)
{
return $commonClasses[$class];
}
$filePath = '';
foreach(array(INCLUDE_DIR,SRC_DIR) as $path)
{
if(file_exists($path.'/'.$class.'.php'))
{
$filePath = $path;
break;
}
}
if(!empty($filePath) && !isLoadFile($class) && class_exists($class,false) === FALSE)
{
require_once $filePath.'/'.$class.'.php';
}
if(INCLUDE_DIR === $filePath)
{
$commonClasses[$class] = isset($param) ? new $class($param) : new $class();
return $commonClasses[$class];
}
else if(SRC_DIR == $filePath)
{
return isset($param) ? new $class($param) : new $class();
}
return FALSE;
}
}
/*
* 判断是否已加载该类所在文件
*
* @param string class 类名称
* @return boolean
*/
if(!function_exists('isLoadFile'))
{
function isLoadFile($class = '')
{
static $commonIsLoaded = array();
if(empty($class))
{
return FALSE;
}
if(isset($commonIsLoaded[$class]))
{
return TRUE;
}
else
{
$commonIsLoaded[$class] = 1;
return FALSE;
}
}
}
/*
* 从数组构建键值对
*
* @param array 需要被重组的参数
* @return string
*/
if(!function_exists('buildParamFromArray'))
{
function buildParamFromArray($arrParam)
{
if(!is_array($arrParam))
{
return FALSE;
}
$strResult = '';
foreach($arrParam as $key=>$val)
{
if(is_array($val))
{
$val = buildParamFromArray($val);
}
$strResult .= $key.'='.$val.'&';
}
$strResult = substr($strResult,0,strlen($strResult)-1);
return $strResult;
}
}
/*
* 写入数据至文件(未增加权限检验等措施)
*
* @param string 文件名称
* @param string 文件内容
* @param string 文件写入方式
* @return object
*/
if(!function_exists('writeToFile'))
{
function writeToFile($file, $content, $model = 'w')
{
if(!file_exists($file) && $model == 'r+')
{
return FALSE;
}
$handle = fopen($file,$model);
flock($handle, LOCK_EX);
fwrite($handle, $content);
fflush($handle);
flock($handle, LOCK_UN);
fclose($handle);
}
}
/*
* 获取配置信息
*
* @param string 配置索引
* @return string
*/
if(!function_exists('getConfig'))
{
function getConfig($key)
{
$configClass = loadClass('config');
return $configClass->getConfig($key);
}
}
/*
* 从数组生成更新SQL更新语句
*
* @param array 更新所有字段
* @return string
*/
if(!function_exists('buildUpdateSql'))
{
function buildUpdateSql($arrItem)
{
if(!is_array($arrItem))
{
return FALSE;
}
foreach($arrItem as $key => $val)
{
$arrItem[$key] = "{$key}='".addslashes(trim($val))."'";
}
return ' '.implode(',',$arrItem).' ';
}
}
/*
* 判断是否已加载该类所在文件
*
* @param int 错误等级(SL_DEBUG:正常debug记录;SL_ERROR:错误输出记录)
* @return boolean
*/
if(!function_exists('logMsg'))
{
function logMsg($iLevel,$sInfo)
{
$commonLog = loadClass('log');
$arrInfo = debug_backtrace();
$commonLog->addLog($iLevel,$sInfo,$arrInfo[0]['file'].':'.$arrInfo[0]['line']);
}
}