/
functions.php
423 lines (369 loc) · 10.1 KB
/
functions.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
<?php
use Think\Config as Config;
use Think\Log as Log;
use Think\Lang as Lang;
use Think\Debug as Debug;
use Think\Model as Model;
use Think\Import as Import;
use Think\Url as Url;
use Think\Cache as Cache;
// 最后留着的方法, A, C, D, L, M, R, S, U
/**
* A函数用于实例化Action
* 格式: [分组/]模块
*
* @param string $name Action资源地址
* @param string $layer 控制层名称
*
* @return Action|false
*/
function A($name, $layer) {
Debug::throw_exception('废弃了A方法');
}
/**
* 重写C方法,改为引用Config方法
*
* @param string $name
*/
function C($name=null, $value=null) {
// 无参数时获取所有
if (empty($name)) {
return Config::getAll();
}
// 字符串,为空则获取
if(is_string($name) && is_null($value)) {
return Config::get($name);
}
// 字符串,不为空则单个设置
if(is_string($name) && !is_null($value)) {
return Config::set($name, $value);
}
// 数组设置
if(is_array($name)) {
return Config::setAll($name);
}
// 避免非法参数
Debug::throw_exception('C funtion error!');
}
/**
* D函数用于实例化Model
* 格式: 分组/模块
*
* @param string $name Model资源地址
* @param string $layer 模型层名称
*
* @return Model
*/
function D($name = null, $groupName = null)
{
// 空值,返回实力化后的Model类
if(is_null($name)) {
return new Model;
}
// 缓存
static $_model = array();
// 分组名默认值
if(is_null($groupName)) {
$groupName = GROUP_NAME;
}
// 获得类名
$class = "App\\" . $groupName . "\\Model\\" . $name . "Model";
// 缓存存在则返回
if(isset($_model[$class])) {
return $_model[$class];
}
// 检查类是否存在,如果不存在则实例化Model类
if(class_exists($class)) {
$model = new $class($name);
}
else {
$model = new Model($name);
}
// 存入缓存
return $_model[$class] = $model;
}
/**
* 获取和设置语言定义(不区分大小写)
*
* @param string|array $name 语言变量
* @param string $value 语言值
*
* @return mixed
*/
function L($name = null, $value = null) {
// 空参数返回所有定义
if (empty($name))
return Lang::getAll();
// 获取
if(is_string($name) && is_null($value)) {
return Lang::get($name);
}
// 设置
if(is_string($name) && !is_null($value)) {
return Lang::set($name, $value);
}
// 批量定义
if(is_array($name)) {
return Lang::setAll($name);
}
}
/**
* M函数用于实例化一个没有模型文件的Model
*
* @param string $name Model名称 支持指定基础模型 例如 MongoModel:User
* @param string $tablePrefix 表前缀
* @param mixed $connection 数据库连接信息
*
* @return Model
*/
function M($name = '', $tablePrefix = '', $connection = '') {
// 缓存
static $_model = array();
// 解析基础模型
if(strpos($name, ':')) {
list($class, $name) = explode(':', $name);
}
else {
$class = 'Model';
}
// 表名
$guid = $tablePrefix . $name . '_' . $class;
if (!isset($_model[$guid])) {
$class = 'Think\\' . $class;
$_model[$guid] = new $class($name, $tablePrefix, $connection);
}
return $_model[$guid];
}
/**
* 远程调用模块的操作方法 URL 参数格式 [项目://][分组/]模块/操作
*
* @param string $url 调用地址
* @param string|array $vars 调用参数 支持字符串和数组
* @param string $layer 要调用的控制层名称
*
* @return mixed
*/
function R($url, $vars=array(), $layer = '') {
// 分析路径
$info = pathinfo($url);
$action = $info['basename'];
$module = $info['dirname'];
// 载入Controller
$class = Import::controller(GROUP_NAME, $module);
// 判断是否存在并执行
if($class){
if(is_string($vars)) {
parse_str($vars,$vars);
}
return call_user_func_array(array(&$class,$action.C('ACTION_SUFFIX')),$vars);
}else{
return false;
}
}
/**
* 缓存管理
*
* @param mixed $name 缓存名称,如果为数组表示进行缓存设置
* @param mixed $value 缓存值
* @param mixed $options 缓存参数
*
* @return mixed
*/
function S($name, $value = '', $options = null) {
static $cache = '';
// 缓存操作的同时初始化
if(is_array($options)) {
$type = isset($options['type']) ? $options['type'] : '';
$cache = Cache::getInstance($type, $options);
}
// 缓存初始化
else if(is_array($name)) {
$type = isset($name['type']) ? $name['type'] : '';
$cache = Cache::getInstance($type, $name);
return $cache;
}
// 自动初始化
else if(empty($cache)) {
$cache = Cache::getInstance();
}
// 获取缓存
if($value === '') {
return $cache->get($name);
}
// 删除缓存
else if(is_null($value)) {
return $cache->rm($name);
}
// 缓存数据
else {
$expire = is_numeric($options) ? $options : NULL;
return $cache->set($name, $value, $expire);
}
}
/**
* URL组装 支持不同URL模式
*
* @param string $url URL表达式,格式:'[分组/模块/操作#锚点@域名]?参数1=值1&参数2=值2...'
* @param string|array $vars 传入的参数,支持数组和字符串
* @param string $suffix 伪静态后缀,默认为true表示获取配置值
* @param boolean $redirect 是否跳转,如果设置为true则表示跳转到该URL地址
* @param boolean $domain 是否显示域名
*
* @return string
*/
function U($url = '', $vars = '', $suffix = true, $domain = false) {
return Url::make($url, $vars, $suffix, $domain);
}
/**
* 去除代码中的空白和注释
*
* @param string $content 代码内容
*
* @return string
*/
function strip_whitespace($content) {
$stripStr = '';
//分析php源码
$tokens = token_get_all($content);
$last_space = false;
for ($i = 0, $j = count($tokens); $i < $j; $i++) {
if (is_string($tokens[$i])) {
$last_space = false;
$stripStr .= $tokens[$i];
} else {
switch ($tokens[$i][0]) {
//过滤各种PHP注释
case T_COMMENT:
case T_DOC_COMMENT:
break;
//过滤空格
case T_WHITESPACE:
if (!$last_space) {
$stripStr .= ' ';
$last_space = true;
}
break;
case T_START_HEREDOC:
$stripStr .= "<<<THINK\n";
break;
case T_END_HEREDOC:
$stripStr .= "THINK;\n";
for($k = $i+1; $k < $j; $k++) {
if(is_string($tokens[$k]) && $tokens[$k] == ';') {
$i = $k;
break;
} else if($tokens[$k][0] == T_CLOSE_TAG) {
break;
}
}
break;
default:
$last_space = false;
$stripStr .= $tokens[$i][1];
}
}
}
return $stripStr;
}
/**
* 字符串命名风格转换
* type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格
*
* @param string $name 字符串
* @param integer $type 转换类型
*
* @return string
*/
function parse_name($name, $type=0) {
if ($type) {
return ucfirst(preg_replace("/_([a-zA-Z])/e", "strtoupper('\\1')", $name));
} else {
return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
}
}
/**
* 浏览器友好的变量输出
*
* @param mixed $var 变量
* @param boolean $echo 是否输出 默认为True 如果为false 则返回输出字符串
* @param string $label 标签 默认为空
* @param boolean $strict 是否严谨 默认为true
*
* @return void|string
*/
function dump($var, $echo=true, $label=null, $strict=true) {
return Debug::dump($var, $echo, $label, $strict);
}
/**
* 过滤器方法 引用传值
*
* @param string $name 过滤器名称
* @param string $content 要过滤的内容
*
* @return void
*/
function filter($name, &$content) {
$class = $name . 'Filter';
Import::load(GROUP_PATH . 'Filter/' . $class . EXT);
$filter = new $class();
$content = $filter->run($content);
}
/**
* 取得对象实例 支持调用类的静态方法
*
* @param string $name 类名
* @param string $method 方法名,如果为空则返回实例化对象
* @param array $args 调用参数
*
* @return object
*/
function get_instance_of($name, $method='', $args=array()) {
static $_instance = array();
$identify = empty($args) ? $name . $method : $name . $method . to_guid_string($args);
if (!isset($_instance[$identify])) {
if (class_exists($name)) {
$o = new $name();
if (method_exists($o, $method)) {
if (!empty($args)) {
$_instance[$identify] = call_user_func_array(array(&$o, $method), $args);
} else {
$_instance[$identify] = $o->$method();
}
}
else
$_instance[$identify] = $o;
}
else
Debug::halt(L('_CLASS_NOT_EXIST_') . ':' . $name);
}
return $_instance[$identify];
}
/**
* 根据PHP各种类型变量生成唯一标识号
*
* @param mixed $mix 变量
*
* @return string
*/
function to_guid_string($mix) {
if (is_object($mix) && function_exists('spl_object_hash')) {
return spl_object_hash($mix);
} elseif (is_resource($mix)) {
$mix = get_resource_type($mix) . strval($mix);
} else {
$mix = serialize($mix);
}
return md5($mix);
}
/**
* 过滤表单中的表达式
*
* @param string &$value
*
* @return void
*/
function filter_exp(&$value){
if (in_array(strtolower($value),array('exp','or'))){
$value .= ' ';
}
}