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