1
|
|
|
<?php |
2
|
|
|
// +---------------------------------------------------------------------- |
|
|
|
|
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
4
|
|
|
// +---------------------------------------------------------------------- |
5
|
|
|
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved. |
6
|
|
|
// +---------------------------------------------------------------------- |
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
8
|
|
|
// +---------------------------------------------------------------------- |
9
|
|
|
// | Author: liu21st <[email protected]> |
10
|
|
|
// +---------------------------------------------------------------------- |
11
|
|
|
declare (strict_types = 1); |
12
|
|
|
|
13
|
|
|
//------------------------ |
14
|
|
|
// ThinkPHP 助手函数 |
15
|
|
|
//------------------------- |
16
|
|
|
|
17
|
|
|
use think\App; |
18
|
|
|
use think\Container; |
19
|
|
|
use think\exception\HttpException; |
|
|
|
|
20
|
|
|
use think\exception\HttpResponseException; |
|
|
|
|
21
|
|
|
use think\facade\Cache; |
22
|
|
|
use think\facade\Config; |
23
|
|
|
use think\facade\Cookie; |
24
|
|
|
use think\facade\Db; |
25
|
|
|
use think\facade\Env; |
26
|
|
|
use think\facade\Event; |
|
|
|
|
27
|
|
|
use think\facade\Lang; |
28
|
|
|
use think\facade\Log; |
29
|
|
|
use think\facade\Request; |
30
|
|
|
use think\facade\Route; |
31
|
|
|
use think\facade\Session; |
32
|
|
|
use think\Model; |
33
|
|
|
use think\model\Collection as ModelCollection; |
34
|
|
|
use think\Response; |
35
|
|
|
use think\route\RuleItem; |
36
|
|
|
|
37
|
|
|
if (!function_exists('abort')) { |
38
|
|
|
/** |
39
|
|
|
* 抛出HTTP异常 |
40
|
|
|
* @param integer|Response $code 状态码 或者 Response对象实例 |
41
|
|
|
* @param string $message 错误信息 |
42
|
|
|
* @param array $header 参数 |
43
|
|
|
*/ |
|
|
|
|
44
|
|
|
function abort($code, string $message = null, array $header = []) |
45
|
|
|
{ |
46
|
|
|
if ($code instanceof Response) { |
47
|
|
|
throw new HttpResponseException($code); |
48
|
|
|
} else { |
49
|
|
|
throw new HttpException($code, $message, null, $header); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (!function_exists('app')) { |
55
|
|
|
/** |
56
|
|
|
* 快速获取容器中的实例 支持依赖注入 |
57
|
|
|
* @param string $name 类名或标识 默认获取当前应用实例 |
|
|
|
|
58
|
|
|
* @param array $args 参数 |
|
|
|
|
59
|
|
|
* @param bool $newInstance 是否每次创建新的实例 |
|
|
|
|
60
|
|
|
* @return object|App |
61
|
|
|
*/ |
62
|
|
|
function app(string $name = App::class, array $args = [], bool $newInstance = false) |
63
|
|
|
{ |
64
|
|
|
return Container::pull($name, $args, $newInstance); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (!function_exists('bind')) { |
69
|
|
|
/** |
70
|
|
|
* 绑定一个类到容器 |
71
|
|
|
* @param string|array $abstract 类标识、接口(支持批量绑定) |
72
|
|
|
* @param mixed $concrete 要绑定的类、闭包或者实例 |
73
|
|
|
* @return Container |
74
|
|
|
*/ |
75
|
|
|
function bind($abstract, $concrete = null) |
76
|
|
|
{ |
77
|
|
|
return Container::getInstance()->bind($abstract, $concrete); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (!function_exists('cache')) { |
82
|
|
|
/** |
83
|
|
|
* 缓存管理 |
84
|
|
|
* @param mixed $name 缓存名称,如果为数组表示进行缓存设置 |
85
|
|
|
* @param mixed $value 缓存值 |
86
|
|
|
* @param mixed $options 缓存参数 |
87
|
|
|
* @param string $tag 缓存标签 |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
|
|
function cache($name, $value = '', $options = null, $tag = null) |
91
|
|
|
{ |
92
|
|
|
if (is_array($name)) { |
93
|
|
|
// 缓存初始化 |
94
|
|
|
return Cache::connect($name); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if ('' === $value) { |
98
|
|
|
// 获取缓存 |
99
|
|
|
return 0 === strpos($name, '?') ? Cache::has(substr($name, 1)) : Cache::get($name); |
100
|
|
|
} elseif (is_null($value)) { |
101
|
|
|
// 删除缓存 |
102
|
|
|
return Cache::rm($name); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// 缓存数据 |
106
|
|
|
if (is_array($options)) { |
107
|
|
|
$expire = $options['expire'] ?? null; //修复查询缓存无法设置过期时间 |
108
|
|
|
} else { |
109
|
|
|
$expire = is_numeric($options) ? $options : null; //默认快捷缓存设置过期时间 |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (is_null($tag)) { |
113
|
|
|
return Cache::set($name, $value, $expire); |
114
|
|
|
} else { |
115
|
|
|
return Cache::tag($tag)->set($name, $value, $expire); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if (!function_exists('call')) { |
121
|
|
|
/** |
122
|
|
|
* 调用反射执行callable 支持依赖注入 |
123
|
|
|
* @param mixed $callable 支持闭包等callable写法 |
124
|
|
|
* @param array $args 参数 |
125
|
|
|
* @return mixed |
126
|
|
|
*/ |
127
|
|
|
function call(callable $callable, array $args = []) |
128
|
|
|
{ |
129
|
|
|
return Container::getInstance()->invoke($callable, $args); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (!function_exists('class_basename')) { |
134
|
|
|
/** |
135
|
|
|
* 获取类名(不包含命名空间) |
136
|
|
|
* |
137
|
|
|
* @param mixed $class 类名 |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
function class_basename($class) |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
$class = is_object($class) ? get_class($class) : $class; |
143
|
|
|
return basename(str_replace('\\', '/', $class)); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if (!function_exists('trait_uses_recursive')) { |
148
|
|
|
/** |
149
|
|
|
* 获取一个trait里所有引用到的trait |
150
|
|
|
* |
151
|
|
|
* @param string $trait |
|
|
|
|
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
|
|
function trait_uses_recursive($trait) |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
$traits = class_uses($trait); |
157
|
|
|
|
158
|
|
|
foreach ($traits as $trait) { |
|
|
|
|
159
|
|
|
$traits += trait_uses_recursive($trait); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $traits; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if (!function_exists('class_uses_recursive')) { |
167
|
|
|
/** |
168
|
|
|
*获取一个类里所有用到的trait,包括父类的 |
|
|
|
|
169
|
|
|
* |
170
|
|
|
* @param mixed $class 类名 |
171
|
|
|
* @return array |
172
|
|
|
*/ |
173
|
|
|
function class_uses_recursive($class) |
|
|
|
|
174
|
|
|
{ |
175
|
|
|
if (is_object($class)) { |
176
|
|
|
$class = get_class($class); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$results = []; |
180
|
|
|
$classes = array_merge([$class => $class], class_parents($class)); |
181
|
|
|
foreach ($classes as $class) { |
|
|
|
|
182
|
|
|
$results += trait_uses_recursive($class); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return array_unique($results); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if (!function_exists('config')) { |
190
|
|
|
/** |
191
|
|
|
* 获取和设置配置参数 |
192
|
|
|
* @param string|array $name 参数名 |
193
|
|
|
* @param mixed $value 参数值 |
194
|
|
|
* @return mixed |
195
|
|
|
*/ |
196
|
|
|
function config($name = '', $value = null) |
197
|
|
|
{ |
198
|
|
|
if (is_array($name)) { |
199
|
|
|
return Config::set($name, $value); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return 0 === strpos($name, '?') ? Config::has(substr($name, 1)) : Config::get($name, $value); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
if (!function_exists('cookie')) { |
207
|
|
|
/** |
208
|
|
|
* Cookie管理 |
209
|
|
|
* @param string $name cookie名称 |
210
|
|
|
* @param mixed $value cookie值 |
211
|
|
|
* @param mixed $option 参数 |
212
|
|
|
* @return mixed |
213
|
|
|
*/ |
214
|
|
|
function cookie(string $name, $value = '', $option = null) |
215
|
|
|
{ |
216
|
|
|
if (is_null($value)) { |
217
|
|
|
// 删除 |
218
|
|
|
return Cookie::delete($name); |
|
|
|
|
219
|
|
|
} elseif ('' === $value) { |
220
|
|
|
// 获取 |
221
|
|
|
return 0 === strpos($name, '?') ? Request::has(substr($name, 1), 'cookie') : Request::cookie($name); |
222
|
|
|
} else { |
223
|
|
|
// 设置 |
224
|
|
|
return Cookie::set($name, $value, $option); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
if (!function_exists('download')) { |
230
|
|
|
/** |
231
|
|
|
* 获取\think\response\Download对象实例 |
232
|
|
|
* @param string $filename 要下载的文件 |
233
|
|
|
* @param string $name 显示文件名 |
234
|
|
|
* @param bool $content 是否为内容 |
235
|
|
|
* @param int $expire 有效期(秒) |
236
|
|
|
* @return \think\response\File |
237
|
|
|
*/ |
238
|
|
|
function download(string $filename, string $name = '', bool $content = false, int $expire = 180) |
239
|
|
|
{ |
240
|
|
|
return Response::create($filename, 'file')->name($name)->isContent($content)->expire($expire); |
|
|
|
|
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
if (!function_exists('dump')) { |
245
|
|
|
/** |
246
|
|
|
* 浏览器友好的变量输出 |
247
|
|
|
* @param mixed $var 变量 |
248
|
|
|
* @param bool $echo 是否输出 默认为true 如果为false 则返回输出字符串 |
249
|
|
|
* @param string $label 标签 默认为空 |
250
|
|
|
* @return void|string |
251
|
|
|
*/ |
252
|
|
|
function dump($var, bool $echo = true, string $label = null) |
253
|
|
|
{ |
254
|
|
|
$label = (null === $label) ? '' : rtrim($label) . ':'; |
255
|
|
|
if ($var instanceof Model || $var instanceof ModelCollection) { |
256
|
|
|
$var = $var->toArray(); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
ob_start(); |
260
|
|
|
var_dump($var); |
|
|
|
|
261
|
|
|
|
262
|
|
|
$output = ob_get_clean(); |
263
|
|
|
$output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output); |
264
|
|
|
|
265
|
|
|
if (PHP_SAPI == 'cli') { |
266
|
|
|
$output = PHP_EOL . $label . $output . PHP_EOL; |
267
|
|
|
} else { |
268
|
|
|
if (!extension_loaded('xdebug')) { |
269
|
|
|
$output = htmlspecialchars($output, ENT_SUBSTITUTE); |
270
|
|
|
} |
271
|
|
|
$output = '<pre>' . $label . $output . '</pre>'; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
if ($echo) { |
275
|
|
|
echo $output; |
276
|
|
|
return; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
return $output; |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
if (!function_exists('env')) { |
284
|
|
|
/** |
285
|
|
|
* 获取环境变量值 |
286
|
|
|
* @access public |
287
|
|
|
* @param string $name 环境变量名(支持二级 .号分割) |
288
|
|
|
* @param string $default 默认值 |
289
|
|
|
* @return mixed |
290
|
|
|
*/ |
291
|
|
|
function env(string $name = null, $default = null) |
292
|
|
|
{ |
293
|
|
|
return Env::get($name, $default); |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
if (!function_exists('event')) { |
298
|
|
|
/** |
299
|
|
|
* 触发事件 |
300
|
|
|
* @param mixed $event 事件名(或者类名) |
301
|
|
|
* @param mixed $args 参数 |
302
|
|
|
* @return mixed |
303
|
|
|
*/ |
304
|
|
|
function event($event, $args = null) |
305
|
|
|
{ |
306
|
|
|
return Event::trigger($event, $args); |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
if (!function_exists('exception')) { |
311
|
|
|
/** |
312
|
|
|
* 抛出异常处理 |
313
|
|
|
* |
314
|
|
|
* @param string $msg 异常消息 |
315
|
|
|
* @param int $code 异常代码 默认为0 |
316
|
|
|
* @param string $exception 异常类 |
317
|
|
|
* |
318
|
|
|
* @throws Exception |
319
|
|
|
*/ |
|
|
|
|
320
|
|
|
function exception(string $msg, int $code = 0, string $exception = '') |
321
|
|
|
{ |
322
|
|
|
$e = $exception ?: '\think\Exception'; |
323
|
|
|
throw new $e($msg, $code); |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
if (!function_exists('halt')) { |
328
|
|
|
/** |
329
|
|
|
* 调试变量并且中断输出 |
330
|
|
|
* @param mixed $var 调试变量或者信息 |
331
|
|
|
*/ |
|
|
|
|
332
|
|
|
function halt($var) |
333
|
|
|
{ |
334
|
|
|
dump($var); |
335
|
|
|
|
336
|
|
|
throw new HttpResponseException(new Response); |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
if (!function_exists('input')) { |
341
|
|
|
/** |
342
|
|
|
* 获取输入数据 支持默认值和过滤 |
343
|
|
|
* @param string $key 获取的变量名 |
344
|
|
|
* @param mixed $default 默认值 |
345
|
|
|
* @param string $filter 过滤方法 |
346
|
|
|
* @return mixed |
347
|
|
|
*/ |
348
|
|
|
function input(string $key = '', $default = null, $filter = '') |
349
|
|
|
{ |
350
|
|
|
if (0 === strpos($key, '?')) { |
351
|
|
|
$key = substr($key, 1); |
352
|
|
|
$has = true; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
if ($pos = strpos($key, '.')) { |
356
|
|
|
// 指定参数来源 |
357
|
|
|
$method = substr($key, 0, $pos); |
358
|
|
|
if (in_array($method, ['get', 'post', 'put', 'patch', 'delete', 'route', 'param', 'request', 'session', 'cookie', 'server', 'env', 'path', 'file'])) { |
359
|
|
|
$key = substr($key, $pos + 1); |
360
|
|
|
} else { |
361
|
|
|
$method = 'param'; |
362
|
|
|
} |
363
|
|
|
} else { |
364
|
|
|
// 默认为自动判断 |
365
|
|
|
$method = 'param'; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
if (isset($has)) { |
369
|
|
|
return request()->has($key, $method, $default); |
370
|
|
|
} else { |
371
|
|
|
return request()->$method($key, $default, $filter); |
372
|
|
|
} |
373
|
|
|
} |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
if (!function_exists('json')) { |
377
|
|
|
/** |
378
|
|
|
* 获取\think\response\Json对象实例 |
379
|
|
|
* @param mixed $data 返回的数据 |
380
|
|
|
* @param int $code 状态码 |
381
|
|
|
* @param array $header 头部 |
382
|
|
|
* @param array $options 参数 |
383
|
|
|
* @return \think\response\Json |
384
|
|
|
*/ |
385
|
|
|
function json($data = [], $code = 200, $header = [], $options = []) |
386
|
|
|
{ |
387
|
|
|
return Response::create($data, 'json', $code)->header($header)->options($options); |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
if (!function_exists('jsonp')) { |
392
|
|
|
/** |
393
|
|
|
* 获取\think\response\Jsonp对象实例 |
394
|
|
|
* @param mixed $data 返回的数据 |
395
|
|
|
* @param int $code 状态码 |
396
|
|
|
* @param array $header 头部 |
397
|
|
|
* @param array $options 参数 |
398
|
|
|
* @return \think\response\Jsonp |
399
|
|
|
*/ |
400
|
|
|
function jsonp($data = [], $code = 200, $header = [], $options = []) |
401
|
|
|
{ |
402
|
|
|
return Response::create($data, 'jsonp', $code)->header($header)->options($options); |
403
|
|
|
} |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
if (!function_exists('lang')) { |
407
|
|
|
/** |
408
|
|
|
* 获取语言变量值 |
409
|
|
|
* @param string $name 语言变量名 |
410
|
|
|
* @param array $vars 动态变量值 |
411
|
|
|
* @param string $lang 语言 |
412
|
|
|
* @return mixed |
413
|
|
|
*/ |
414
|
|
|
function lang(string $name, array $vars = [], string $lang = '') |
415
|
|
|
{ |
416
|
|
|
return Lang::get($name, $vars, $lang); |
417
|
|
|
} |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
if (!function_exists('parse_name')) { |
421
|
|
|
/** |
422
|
|
|
* 字符串命名风格转换 |
423
|
|
|
* type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格 |
424
|
|
|
* @param string $name 字符串 |
425
|
|
|
* @param int $type 转换类型 |
426
|
|
|
* @param bool $ucfirst 首字母是否大写(驼峰规则) |
427
|
|
|
* @return string |
428
|
|
|
*/ |
429
|
|
|
function parse_name(string $name, int $type = 0, bool $ucfirst = true) |
|
|
|
|
430
|
|
|
{ |
431
|
|
|
if ($type) { |
432
|
|
|
$name = preg_replace_callback('/_([a-zA-Z])/', function ($match) { |
|
|
|
|
433
|
|
|
return strtoupper($match[1]); |
434
|
|
|
}, $name); |
|
|
|
|
435
|
|
|
|
436
|
|
|
return $ucfirst ? ucfirst($name) : lcfirst($name); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_")); |
440
|
|
|
} |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
if (!function_exists('raw')) { |
444
|
|
|
/** |
445
|
|
|
* 生成一个数据库的Raw对象 |
446
|
|
|
* @param string $sql SQL指令 |
447
|
|
|
* @return \think\db\Raw |
448
|
|
|
*/ |
449
|
|
|
function raw(string $sql) |
450
|
|
|
{ |
451
|
|
|
return Db::raw($sql); |
452
|
|
|
} |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
if (!function_exists('redirect')) { |
456
|
|
|
/** |
457
|
|
|
* 获取\think\response\Redirect对象实例 |
458
|
|
|
* @param mixed $url 重定向地址 支持Url::build方法的地址 |
459
|
|
|
* @param array|integer $params 额外参数 |
460
|
|
|
* @param int $code 状态码 |
461
|
|
|
* @return \think\response\Redirect |
462
|
|
|
*/ |
463
|
|
|
function redirect($url = [], $params = [], $code = 302) |
464
|
|
|
{ |
465
|
|
|
if (is_integer($params)) { |
466
|
|
|
$code = $params; |
467
|
|
|
$params = []; |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
return Response::create($url, 'redirect', $code)->params($params); |
|
|
|
|
471
|
|
|
} |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
if (!function_exists('request')) { |
475
|
|
|
/** |
476
|
|
|
* 获取当前Request对象实例 |
477
|
|
|
* @return Request |
478
|
|
|
*/ |
479
|
|
|
function request() |
480
|
|
|
{ |
481
|
|
|
return app('request'); |
482
|
|
|
} |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
if (!function_exists('response')) { |
486
|
|
|
/** |
487
|
|
|
* 创建普通 Response 对象实例 |
488
|
|
|
* @param mixed $data 输出数据 |
489
|
|
|
* @param int|string $code 状态码 |
490
|
|
|
* @param array $header 头信息 |
491
|
|
|
* @param string $type |
|
|
|
|
492
|
|
|
* @return Response |
493
|
|
|
*/ |
494
|
|
|
function response($data = '', $code = 200, $header = [], $type = 'html') |
495
|
|
|
{ |
496
|
|
|
return Response::create($data, $type, $code)->header($header); |
|
|
|
|
497
|
|
|
} |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
if (!function_exists('route')) { |
501
|
|
|
/** |
502
|
|
|
* 路由注册 |
503
|
|
|
* @param string $rule 路由规则 |
504
|
|
|
* @param mixed $route 路由地址 |
505
|
|
|
* @param string $method 请求类型 |
506
|
|
|
* @return RuleItem |
507
|
|
|
*/ |
508
|
|
|
function route(string $rule, $route, $method = '*') |
509
|
|
|
{ |
510
|
|
|
return Route::rule($rule, $route, $method); |
|
|
|
|
511
|
|
|
} |
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
if (!function_exists('session')) { |
515
|
|
|
/** |
516
|
|
|
* Session管理 |
517
|
|
|
* @param string $name session名称 |
518
|
|
|
* @param mixed $value session值 |
519
|
|
|
* @return mixed |
520
|
|
|
*/ |
521
|
|
|
function session(string $name = null, $value = '') |
522
|
|
|
{ |
523
|
|
|
if (is_null($name)) { |
524
|
|
|
// 清除 |
525
|
|
|
Session::clear(); |
526
|
|
|
} elseif (is_null($value)) { |
527
|
|
|
// 删除 |
528
|
|
|
Session::delete($name); |
529
|
|
|
} elseif ('' === $value) { |
530
|
|
|
// 判断或获取 |
531
|
|
|
return 0 === strpos($name, '?') ? Session::has(substr($name, 1)) : Session::get($name); |
532
|
|
|
} else { |
533
|
|
|
// 设置 |
534
|
|
|
Session::set($name, $value); |
535
|
|
|
} |
536
|
|
|
} |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
if (!function_exists('token')) { |
540
|
|
|
/** |
541
|
|
|
* 生成表单令牌 |
542
|
|
|
* @param string $name 令牌名称 |
543
|
|
|
* @param mixed $type 令牌生成方法 |
544
|
|
|
* @return string |
545
|
|
|
*/ |
546
|
|
|
function token(string $name = '__token__', string $type = 'md5'): string |
547
|
|
|
{ |
548
|
|
|
$token = Request::token($name, $type); |
549
|
|
|
|
550
|
|
|
return '<input type="hidden" name="' . $name . '" value="' . $token . '" />'; |
551
|
|
|
} |
552
|
|
|
} |
553
|
|
|
|
554
|
|
|
if (!function_exists('trace')) { |
555
|
|
|
/** |
556
|
|
|
* 记录日志信息 |
557
|
|
|
* @param mixed $log log信息 支持字符串和数组 |
558
|
|
|
* @param string $level 日志级别 |
559
|
|
|
* @return array|void |
560
|
|
|
*/ |
561
|
|
|
function trace($log = '[think]', string $level = 'log') |
562
|
|
|
{ |
563
|
|
|
if ('[think]' === $log) { |
564
|
|
|
return Log::getLog(); |
565
|
|
|
} |
566
|
|
|
|
567
|
|
|
Log::record($log, $level); |
568
|
|
|
} |
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
if (!function_exists('trait_uses_recursive')) { |
572
|
|
|
/** |
573
|
|
|
* 获取一个trait里所有引用到的trait |
574
|
|
|
* |
575
|
|
|
* @param string $trait Trait |
576
|
|
|
* @return array |
577
|
|
|
*/ |
578
|
|
|
function trait_uses_recursive(string $trait) |
|
|
|
|
579
|
|
|
{ |
580
|
|
|
$traits = class_uses($trait); |
581
|
|
|
foreach ($traits as $trait) { |
582
|
|
|
$traits += traitUsesRecursive($trait); |
|
|
|
|
583
|
|
|
} |
584
|
|
|
|
585
|
|
|
return $traits; |
586
|
|
|
} |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
if (!function_exists('url')) { |
590
|
|
|
/** |
591
|
|
|
* Url生成 |
592
|
|
|
* @param string $url 路由地址 |
|
|
|
|
593
|
|
|
* @param array $vars 变量 |
|
|
|
|
594
|
|
|
* @param bool|string $suffix 生成的URL后缀 |
|
|
|
|
595
|
|
|
* @param bool|string $domain 域名 |
|
|
|
|
596
|
|
|
* @return string |
597
|
|
|
*/ |
598
|
|
|
function url(string $url = '', array $vars = [], $suffix = true, $domain = false) |
599
|
|
|
{ |
600
|
|
|
return Route::buildUrl($url, $vars, $suffix, $domain); |
|
|
|
|
601
|
|
|
} |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
if (!function_exists('validate')) { |
605
|
|
|
/** |
606
|
|
|
* 验证数据 |
607
|
|
|
* @param array $data 数据 |
608
|
|
|
* @param string|array $validate 验证器名或者验证规则数组 |
609
|
|
|
* @param array $message 提示信息 |
610
|
|
|
* @param bool $batch 是否批量验证 |
611
|
|
|
* @return true |
612
|
|
|
* @throws ValidateException |
613
|
|
|
*/ |
614
|
|
|
function validate(array $data, $validate, array $message = [], bool $batch = false) |
615
|
|
|
{ |
616
|
|
|
if (is_array($validate)) { |
617
|
|
|
$v = new Validate(); |
|
|
|
|
618
|
|
|
$v->rule($validate); |
619
|
|
|
} else { |
620
|
|
|
if (strpos($validate, '.')) { |
621
|
|
|
// 支持场景 |
622
|
|
|
list($validate, $scene) = explode('.', $validate); |
623
|
|
|
} |
624
|
|
|
|
625
|
|
|
$class = app()->parseClass('validate', $validate); |
626
|
|
|
$v = new $class(); |
627
|
|
|
|
628
|
|
|
if (!empty($scene)) { |
629
|
|
|
$v->scene($scene); |
630
|
|
|
} |
631
|
|
|
} |
632
|
|
|
|
633
|
|
|
return $v->message($message)->batch($batch)->failException(true)->check($data); |
634
|
|
|
} |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
if (!function_exists('view')) { |
638
|
|
|
/** |
639
|
|
|
* 渲染模板输出 |
640
|
|
|
* @param string $template 模板文件 |
|
|
|
|
641
|
|
|
* @param array $vars 模板变量 |
|
|
|
|
642
|
|
|
* @param int $code 状态码 |
|
|
|
|
643
|
|
|
* @param callable $filter 内容过滤 |
|
|
|
|
644
|
|
|
* @return \think\response\View |
645
|
|
|
*/ |
646
|
|
|
function view(string $template = '', $vars = [], $code = 200, $filter = null) |
647
|
|
|
{ |
648
|
|
|
return Response::create($template, 'view', $code)->assign($vars)->filter($filter); |
|
|
|
|
649
|
|
|
} |
650
|
|
|
} |
651
|
|
|
|
652
|
|
|
if (!function_exists('xml')) { |
653
|
|
|
/** |
654
|
|
|
* 获取\think\response\Xml对象实例 |
655
|
|
|
* @param mixed $data 返回的数据 |
656
|
|
|
* @param int $code 状态码 |
657
|
|
|
* @param array $header 头部 |
658
|
|
|
* @param array $options 参数 |
659
|
|
|
* @return \think\response\Xml |
660
|
|
|
*/ |
661
|
|
|
function xml($data = [], $code = 200, $header = [], $options = []) |
662
|
|
|
{ |
663
|
|
|
return Response::create($data, 'xml', $code)->header($header)->options($options); |
664
|
|
|
} |
665
|
|
|
} |
666
|
|
|
|
667
|
|
|
if (!function_exists('yaconf')) { |
668
|
|
|
/** |
669
|
|
|
* 获取yaconf配置 |
670
|
|
|
* |
671
|
|
|
* @param string $name 配置参数名 |
672
|
|
|
* @param mixed $default 默认值 |
673
|
|
|
* @return mixed |
674
|
|
|
*/ |
675
|
|
|
function yaconf(string $name, $default = null) |
676
|
|
|
{ |
677
|
|
|
return Config::yaconf($name, $default); |
|
|
|
|
678
|
|
|
} |
679
|
|
|
} |
680
|
|
|
|