Completed
Push — 6.0 ( b7fde5...2aa756 )
by liu
22:21 queued 19:13
created

cache()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 12
c 2
b 0
f 0
nc 7
nop 4
dl 0
loc 21
rs 9.2222
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, HttpException. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
20
use think\exception\HttpResponseException;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, HttpResponseException. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Event. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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\route\Url as UrlBuild;
33
use think\Validate;
34
35
if (!function_exists('abort')) {
36
    /**
37
     * 抛出HTTP异常
38
     * @param integer|Response $code    状态码 或者 Response对象实例
39
     * @param string           $message 错误信息
40
     * @param array            $header  参数
41
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
42
    function abort($code, string $message = null, array $header = [])
43
    {
44
        if ($code instanceof Response) {
45
            throw new HttpResponseException($code);
46
        } else {
47
            throw new HttpException($code, $message, null, $header);
48
        }
49
    }
50
}
51
52
if (!function_exists('app')) {
53
    /**
54
     * 快速获取容器中的实例 支持依赖注入
55
     * @param string $name        类名或标识 默认获取当前应用实例
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
56
     * @param array  $args        参数
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
57
     * @param bool   $newInstance 是否每次创建新的实例
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
58
     * @return object|App
59
     */
60
    function app(string $name = '', array $args = [], bool $newInstance = false)
61
    {
62
        return Container::getInstance()->make($name ?: App::class, $args, $newInstance);
63
    }
64
}
65
66
if (!function_exists('bind')) {
67
    /**
68
     * 绑定一个类到容器
69
     * @param  string|array $abstract 类标识、接口(支持批量绑定)
70
     * @param  mixed        $concrete 要绑定的类、闭包或者实例
71
     * @return Container
72
     */
73
    function bind($abstract, $concrete = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
74
    {
75
        return Container::getInstance()->bind($abstract, $concrete);
76
    }
77
}
78
79
if (!function_exists('cache')) {
80
    /**
81
     * 缓存管理
82
     * @param  mixed  $name    缓存名称,如果为数组表示进行缓存设置
83
     * @param  mixed  $value   缓存值
84
     * @param  mixed  $options 缓存参数
85
     * @param  string $tag     缓存标签
86
     * @return mixed
87
     */
88
    function cache($name, $value = '', $options = null, $tag = null)
89
    {
90
        if ('' === $value) {
91
            // 获取缓存
92
            return 0 === strpos($name, '?') ? Cache::has(substr($name, 1)) : Cache::get($name);
93
        } elseif (is_null($value)) {
94
            // 删除缓存
95
            return Cache::delete($name);
96
        }
97
98
        // 缓存数据
99
        if (is_array($options)) {
100
            $expire = $options['expire'] ?? null; //修复查询缓存无法设置过期时间
101
        } else {
102
            $expire = $options;
103
        }
104
105
        if (is_null($tag)) {
106
            return Cache::set($name, $value, $expire);
107
        } else {
108
            return Cache::tag($tag)->set($name, $value, $expire);
109
        }
110
    }
111
}
112
113
if (!function_exists('class_basename')) {
114
    /**
115
     * 获取类名(不包含命名空间)
116
     *
117
     * @param  mixed $class 类名
118
     * @return string
119
     */
120
    function class_basename($class): string
0 ignored issues
show
Coding Style introduced by
Function name "class_basename" is prefixed with a package name but does not begin with a capital letter
Loading history...
Coding Style introduced by
Function name "class_basename" is invalid; consider "Class_basename" instead
Loading history...
121
    {
122
        $class = is_object($class) ? get_class($class) : $class;
123
        return basename(str_replace('\\', '/', $class));
124
    }
125
}
126
127
if (!function_exists('class_uses_recursive')) {
128
    /**
129
     *获取一个类里所有用到的trait,包括父类的
0 ignored issues
show
Coding Style introduced by
Expected 1 space after asterisk; 0 found
Loading history...
130
     *
131
     * @param  mixed $class 类名
132
     * @return array
133
     */
134
    function class_uses_recursive($class): array
0 ignored issues
show
Coding Style introduced by
Function name "class_uses_recursive" is prefixed with a package name but does not begin with a capital letter
Loading history...
Coding Style introduced by
Function name "class_uses_recursive" is invalid; consider "Class_Uses_recursive" instead
Loading history...
135
    {
136
        if (is_object($class)) {
137
            $class = get_class($class);
138
        }
139
140
        $results = [];
141
        $classes = array_merge([$class => $class], class_parents($class));
142
        foreach ($classes as $class) {
0 ignored issues
show
introduced by
$class is overwriting one of the parameters of this function.
Loading history...
143
            $results += trait_uses_recursive($class);
144
        }
145
146
        return array_unique($results);
147
    }
148
}
149
150
if (!function_exists('config')) {
151
    /**
152
     * 获取和设置配置参数
153
     * @param  string|array $name  参数名
154
     * @param  mixed        $value 参数值
155
     * @return mixed
156
     */
157
    function config($name = '', $value = null)
158
    {
159
        if (is_array($name)) {
160
            return Config::set($name, $value);
161
        }
162
163
        return 0 === strpos($name, '?') ? Config::has(substr($name, 1)) : Config::get($name, $value);
164
    }
165
}
166
167
if (!function_exists('cookie')) {
168
    /**
169
     * Cookie管理
170
     * @param  string $name   cookie名称
171
     * @param  mixed  $value  cookie值
172
     * @param  mixed  $option 参数
173
     * @return mixed
174
     */
175
    function cookie(string $name, $value = '', $option = null)
176
    {
177
        if (is_null($value)) {
178
            // 删除
179
            Cookie::delete($name);
180
        } elseif ('' === $value) {
181
            // 获取
182
            return 0 === strpos($name, '?') ? Cookie::has(substr($name, 1)) : Cookie::get($name);
0 ignored issues
show
Bug introduced by
The method has() does not exist on think\facade\Cookie. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

182
            return 0 === strpos($name, '?') ? Cookie::/** @scrutinizer ignore-call */ has(substr($name, 1)) : Cookie::get($name);
Loading history...
Bug introduced by
The method get() does not exist on think\facade\Cookie. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

182
            return 0 === strpos($name, '?') ? Cookie::has(substr($name, 1)) : Cookie::/** @scrutinizer ignore-call */ get($name);
Loading history...
183
        } else {
184
            // 设置
185
            return Cookie::set($name, $value, $option);
186
        }
187
    }
188
}
189
190
if (!function_exists('download')) {
191
    /**
192
     * 获取\think\response\Download对象实例
193
     * @param  string $filename 要下载的文件
194
     * @param  string $name     显示文件名
195
     * @param  bool   $content  是否为内容
196
     * @param  int    $expire   有效期(秒)
197
     * @return \think\response\File
198
     */
199
    function download(string $filename, string $name = '', bool $content = false, int $expire = 180)
200
    {
201
        return Response::create($filename, 'file')->name($name)->isContent($content)->expire($expire);
0 ignored issues
show
Bug introduced by
The method name() does not exist on think\Response. It seems like you code against a sub-type of think\Response such as think\response\File. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

201
        return Response::create($filename, 'file')->/** @scrutinizer ignore-call */ name($name)->isContent($content)->expire($expire);
Loading history...
202
    }
203
}
204
205
if (!function_exists('dump')) {
206
    /**
0 ignored issues
show
Coding Style introduced by
Parameter ...$vars should have a doc-comment as per coding-style.
Loading history...
207
     * 浏览器友好的变量输出
208
     * @param  mixed  $vars 要输出的变量
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter $vars does not match actual variable name ...$vars
Loading history...
209
     * @return void
210
     */
211
    function dump(...$vars)
212
    {
213
        ob_start();
214
        var_dump(...$vars);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($vars) looks like debug code. Are you sure you do not want to remove it?
Loading history...
215
216
        $output = ob_get_clean();
217
        $output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output);
218
219
        if (PHP_SAPI == 'cli') {
220
            $output = PHP_EOL . $output . PHP_EOL;
221
        } else {
222
            if (!extension_loaded('xdebug')) {
223
                $output = htmlspecialchars($output, ENT_SUBSTITUTE);
224
            }
225
            $output = '<pre>' . $output . '</pre>';
226
        }
227
228
        echo $output;
229
    }
230
}
231
232
if (!function_exists('env')) {
233
    /**
234
     * 获取环境变量值
235
     * @access public
236
     * @param  string $name    环境变量名(支持二级 .号分割)
237
     * @param  string $default 默认值
238
     * @return mixed
239
     */
240
    function env(string $name = null, $default = null)
241
    {
242
        return Env::get($name, $default);
243
    }
244
}
245
246
if (!function_exists('event')) {
247
    /**
248
     * 触发事件
249
     * @param  mixed $event 事件名(或者类名)
250
     * @param  mixed $args  参数
251
     * @return mixed
252
     */
253
    function event($event, $args = null)
254
    {
255
        return Event::trigger($event, $args);
256
    }
257
}
258
259
if (!function_exists('halt')) {
260
    /**
0 ignored issues
show
Coding Style introduced by
Parameter ...$vars should have a doc-comment as per coding-style.
Loading history...
261
     * 调试变量并且中断输出
262
     * @param mixed $vars 调试变量或者信息
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter $vars does not match actual variable name ...$vars
Loading history...
263
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
264
    function halt(...$vars)
265
    {
266
        dump(...$vars);
267
268
        throw new HttpResponseException(new Response);
269
    }
270
}
271
272
if (!function_exists('input')) {
273
    /**
274
     * 获取输入数据 支持默认值和过滤
275
     * @param  string $key     获取的变量名
276
     * @param  mixed  $default 默认值
277
     * @param  string $filter  过滤方法
278
     * @return mixed
279
     */
280
    function input(string $key = '', $default = null, $filter = '')
281
    {
282
        if (0 === strpos($key, '?')) {
283
            $key = substr($key, 1);
284
            $has = true;
285
        }
286
287
        if ($pos = strpos($key, '.')) {
288
            // 指定参数来源
289
            $method = substr($key, 0, $pos);
290
            if (in_array($method, ['get', 'post', 'put', 'patch', 'delete', 'route', 'param', 'request', 'session', 'cookie', 'server', 'env', 'path', 'file'])) {
291
                $key = substr($key, $pos + 1);
292
                if ('server' == $method && is_null($default)) {
293
                    $default = '';
294
                }
295
            } else {
296
                $method = 'param';
297
            }
298
        } else {
299
            // 默认为自动判断
300
            $method = 'param';
301
        }
302
303
        return isset($has) ?
304
        request()->has($key, $method) :
305
        request()->$method($key, $default, $filter);
306
    }
307
}
308
309
if (!function_exists('invoke')) {
310
    /**
311
     * 调用反射实例化对象或者执行方法 支持依赖注入
312
     * @param  mixed $call 类名或者callable
313
     * @param  array $args 参数
314
     * @return mixed
315
     */
316
    function invoke($call, array $args = [])
317
    {
318
        if (is_callable($call)) {
319
            return Container::getInstance()->invoke($call, $args);
320
        }
321
322
        return Container::getInstance()->invokeClass($call, $args);
323
    }
324
}
325
326
if (!function_exists('json')) {
327
    /**
328
     * 获取\think\response\Json对象实例
329
     * @param  mixed $data    返回的数据
330
     * @param  int   $code    状态码
331
     * @param  array $header  头部
332
     * @param  array $options 参数
333
     * @return \think\response\Json
334
     */
335
    function json($data = [], $code = 200, $header = [], $options = [])
336
    {
337
        return Response::create($data, 'json', $code)->header($header)->options($options);
338
    }
339
}
340
341
if (!function_exists('jsonp')) {
342
    /**
343
     * 获取\think\response\Jsonp对象实例
344
     * @param  mixed $data    返回的数据
345
     * @param  int   $code    状态码
346
     * @param  array $header  头部
347
     * @param  array $options 参数
348
     * @return \think\response\Jsonp
349
     */
350
    function jsonp($data = [], $code = 200, $header = [], $options = [])
351
    {
352
        return Response::create($data, 'jsonp', $code)->header($header)->options($options);
353
    }
354
}
355
356
if (!function_exists('lang')) {
357
    /**
358
     * 获取语言变量值
359
     * @param  string $name 语言变量名
360
     * @param  array  $vars 动态变量值
361
     * @param  string $lang 语言
362
     * @return mixed
363
     */
364
    function lang(string $name, array $vars = [], string $lang = '')
365
    {
366
        return Lang::get($name, $vars, $lang);
367
    }
368
}
369
370
if (!function_exists('parse_name')) {
371
    /**
372
     * 字符串命名风格转换
373
     * type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格
374
     * @param  string $name    字符串
375
     * @param  int    $type    转换类型
376
     * @param  bool   $ucfirst 首字母是否大写(驼峰规则)
377
     * @return string
378
     */
379
    function parse_name(string $name, int $type = 0, bool $ucfirst = true): string
0 ignored issues
show
Coding Style introduced by
Function name "parse_name" is prefixed with a package name but does not begin with a capital letter
Loading history...
Coding Style introduced by
Function name "parse_name" is invalid; consider "Parse_name" instead
Loading history...
380
    {
381
        if ($type) {
382
            $name = preg_replace_callback('/_([a-zA-Z])/', function ($match) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
383
                return strtoupper($match[1]);
384
            }, $name);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
385
386
            return $ucfirst ? ucfirst($name) : lcfirst($name);
387
        }
388
389
        return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
390
    }
391
}
392
393
if (!function_exists('redirect')) {
394
    /**
395
     * 获取\think\response\Redirect对象实例
396
     * @param  mixed         $url    重定向地址 支持Url::build方法的地址
397
     * @param  array|integer $params 额外参数
398
     * @param  int           $code   状态码
399
     * @return \think\response\Redirect
400
     */
401
    function redirect($url = [], $params = [], $code = 302)
402
    {
403
        if (is_integer($params)) {
404
            $code   = $params;
405
            $params = [];
406
        }
407
408
        return Response::create($url, 'redirect', $code)->params($params);
0 ignored issues
show
Bug introduced by
The method params() does not exist on think\Response. It seems like you code against a sub-type of think\Response such as think\response\Redirect. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

408
        return Response::create($url, 'redirect', $code)->/** @scrutinizer ignore-call */ params($params);
Loading history...
409
    }
410
}
411
412
if (!function_exists('request')) {
413
    /**
414
     * 获取当前Request对象实例
415
     * @return Request
416
     */
417
    function request()
418
    {
419
        return app('request');
0 ignored issues
show
Bug Best Practice introduced by
The expression return app('request') also could return the type think\App which is incompatible with the documented return type think\facade\Request.
Loading history...
420
    }
421
}
422
423
if (!function_exists('response')) {
424
    /**
425
     * 创建普通 Response 对象实例
426
     * @param  mixed      $data   输出数据
427
     * @param  int|string $code   状态码
428
     * @param  array      $header 头信息
429
     * @param  string     $type
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
430
     * @return Response
431
     */
432
    function response($data = '', $code = 200, $header = [], $type = 'html')
433
    {
434
        return Response::create($data, $type, $code)->header($header);
0 ignored issues
show
Bug introduced by
It seems like $code can also be of type string; however, parameter $code of think\Response::create() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

434
        return Response::create($data, $type, /** @scrutinizer ignore-type */ $code)->header($header);
Loading history...
435
    }
436
}
437
438
if (!function_exists('session')) {
439
    /**
440
     * Session管理
441
     * @param  string $name  session名称
442
     * @param  mixed  $value session值
443
     * @return mixed
444
     */
445
    function session(string $name = null, $value = '')
446
    {
447
        if (is_null($name)) {
448
            // 清除
449
            Session::clear();
450
        } elseif (is_null($value)) {
451
            // 删除
452
            Session::delete($name);
453
        } elseif ('' === $value) {
454
            // 判断或获取
455
            return 0 === strpos($name, '?') ? Session::has(substr($name, 1)) : Session::get($name);
456
        } else {
457
            // 设置
458
            Session::set($name, $value);
459
        }
460
    }
461
}
462
463
if (!function_exists('token')) {
464
    /**
465
     * 获取Token令牌
466
     * @param  string $name 令牌名称
467
     * @param  mixed  $type 令牌生成方法
468
     * @return string
469
     */
470
    function token(string $name = '__token__', string $type = 'md5'): string
471
    {
472
        return Request::buildToken($name, $type);
473
    }
474
}
475
476
if (!function_exists('token_field')) {
477
    /**
478
     * 生成令牌隐藏表单
479
     * @param  string $name 令牌名称
480
     * @param  mixed  $type 令牌生成方法
481
     * @return string
482
     */
483
    function token_field(string $name = '__token__', string $type = 'md5'): string
0 ignored issues
show
Coding Style introduced by
Function name "token_field" is prefixed with a package name but does not begin with a capital letter
Loading history...
Coding Style introduced by
Function name "token_field" is invalid; consider "Token_field" instead
Loading history...
484
    {
485
        $token = Request::buildToken($name, $type);
486
487
        return '<input type="hidden" name="' . $name . '" value="' . $token . '" />';
488
    }
489
}
490
491
if (!function_exists('token_meta')) {
492
    /**
493
     * 生成令牌meta
494
     * @param  string $name 令牌名称
495
     * @param  mixed  $type 令牌生成方法
496
     * @return string
497
     */
498
    function token_meta(string $name = '__token__', string $type = 'md5'): string
0 ignored issues
show
Coding Style introduced by
Function name "token_meta" is prefixed with a package name but does not begin with a capital letter
Loading history...
Coding Style introduced by
Function name "token_meta" is invalid; consider "Token_meta" instead
Loading history...
499
    {
500
        $token = Request::buildToken($name, $type);
501
502
        return '<meta name="csrf-token" content="' . $token . '">';
503
    }
504
}
505
506
if (!function_exists('trace')) {
507
    /**
508
     * 记录日志信息
509
     * @param  mixed  $log   log信息 支持字符串和数组
510
     * @param  string $level 日志级别
511
     * @return array|void
512
     */
513
    function trace($log = '[think]', string $level = 'log')
514
    {
515
        if ('[think]' === $log) {
516
            return Log::getLog();
517
        }
518
519
        Log::record($log, $level);
520
    }
521
}
522
523
if (!function_exists('trait_uses_recursive')) {
524
    /**
525
     * 获取一个trait里所有引用到的trait
526
     *
527
     * @param  string $trait Trait
528
     * @return array
529
     */
530
    function trait_uses_recursive(string $trait): array
0 ignored issues
show
Coding Style introduced by
Function name "trait_uses_recursive" is prefixed with a package name but does not begin with a capital letter
Loading history...
Coding Style introduced by
Function name "trait_uses_recursive" is invalid; consider "Trait_Uses_recursive" instead
Loading history...
531
    {
532
        $traits = class_uses($trait);
533
        foreach ($traits as $trait) {
534
            $traits += trait_uses_recursive($trait);
535
        }
536
537
        return $traits;
538
    }
539
}
540
541
if (!function_exists('url')) {
542
    /**
543
     * Url生成
544
     * @param string      $url    路由地址
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
545
     * @param array       $vars   变量
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
546
     * @param bool|string $suffix 生成的URL后缀
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
547
     * @param bool|string $domain 域名
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
548
     * @return UrlBuild
549
     */
550
    function url(string $url = '', array $vars = [], $suffix = true, $domain = false): UrlBuild
551
    {
552
        return Route::buildUrl($url, $vars)->suffix($suffix)->domain($domain);
553
    }
554
}
555
556
if (!function_exists('validate')) {
557
    /**
558
     * 生成验证对象
559
     * @param  string|array $validate 验证器类名或者验证规则数组
560
     * @param  array        $message  错误提示信息
561
     * @param  bool         $batch    是否批量验证
562
     * @return Validate
563
     */
564
    function validate($validate = '', array $message = [], bool $batch = false): Validate
565
    {
566
        if (is_array($validate) || '' === $validate) {
567
            $v = new Validate();
568
            if (is_array($validate)) {
569
                $v->rule($validate);
570
            }
571
        } else {
572
            if (strpos($validate, '.')) {
573
                // 支持场景
574
                list($validate, $scene) = explode('.', $validate);
575
            }
576
577
            $class = false !== strpos($validate, '\\') ? $validate : app()->parseClass('validate', $validate);
578
579
            $v = new $class();
580
581
            if (!empty($scene)) {
582
                $v->scene($scene);
583
            }
584
        }
585
586
        return $v->message($message)->batch($batch)->failException(true);
587
    }
588
}
589
590
if (!function_exists('view')) {
591
    /**
592
     * 渲染模板输出
593
     * @param string    $template 模板文件
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
594
     * @param array     $vars 模板变量
1 ignored issue
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
595
     * @param int       $code 状态码
1 ignored issue
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
596
     * @param callable  $filter 内容过滤
1 ignored issue
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
597
     * @return \think\response\View
598
     */
599
    function view(string $template = '', $vars = [], $code = 200, $filter = null)
600
    {
601
        return Response::create($template, 'view', $code)->assign($vars)->filter($filter);
0 ignored issues
show
Bug introduced by
The method assign() does not exist on think\Response. It seems like you code against a sub-type of think\Response such as think\response\View. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

601
        return Response::create($template, 'view', $code)->/** @scrutinizer ignore-call */ assign($vars)->filter($filter);
Loading history...
602
    }
603
}
604
605
if (!function_exists('display')) {
606
    /**
607
     * 渲染模板输出
608
     * @param string    $content 渲染内容
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
609
     * @param array     $vars 模板变量
1 ignored issue
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
610
     * @param int       $code 状态码
1 ignored issue
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
611
     * @param callable  $filter 内容过滤
1 ignored issue
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
612
     * @return \think\response\View
613
     */
614
    function display(string $content, $vars = [], $code = 200, $filter = null)
615
    {
616
        return Response::create($template, 'view', $code)->isContent(true)->assign($vars)->filter($filter);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $template seems to be never defined.
Loading history...
Bug introduced by
The method isContent() does not exist on think\Response. It seems like you code against a sub-type of think\Response such as think\response\File or think\response\View. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

616
        return Response::create($template, 'view', $code)->/** @scrutinizer ignore-call */ isContent(true)->assign($vars)->filter($filter);
Loading history...
617
    }
618
}
619
620
if (!function_exists('xml')) {
621
    /**
622
     * 获取\think\response\Xml对象实例
623
     * @param  mixed $data    返回的数据
624
     * @param  int   $code    状态码
625
     * @param  array $header  头部
626
     * @param  array $options 参数
627
     * @return \think\response\Xml
628
     */
629
    function xml($data = [], $code = 200, $header = [], $options = [])
630
    {
631
        return Response::create($data, 'xml', $code)->header($header)->options($options);
632
    }
633
}
634