Completed
Push — 6.0 ( 9639e4...a5dd93 )
by liu
32:15 queued 23:15
created

cache()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 7
eloc 14
c 3
b 0
f 0
nc 8
nop 4
dl 0
loc 25
rs 8.8333
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  string $name    缓存名称
83
     * @param  mixed  $value   缓存值
84
     * @param  mixed  $options 缓存参数
85
     * @param  string $tag     缓存标签
86
     * @return mixed
87
     */
88
    function cache(string $name = null, $value = '', $options = null, $tag = null)
89
    {
90
        if (is_null($name)) {
91
            return app('cache');
92
        }
93
94
        if ('' === $value) {
95
            // 获取缓存
96
            return 0 === strpos($name, '?') ? Cache::has(substr($name, 1)) : Cache::get($name);
97
        } elseif (is_null($value)) {
98
            // 删除缓存
99
            return Cache::delete($name);
100
        }
101
102
        // 缓存数据
103
        if (is_array($options)) {
104
            $expire = $options['expire'] ?? null; //修复查询缓存无法设置过期时间
105
        } else {
106
            $expire = $options;
107
        }
108
109
        if (is_null($tag)) {
110
            return Cache::set($name, $value, $expire);
111
        } else {
112
            return Cache::tag($tag)->set($name, $value, $expire);
113
        }
114
    }
115
}
116
117
if (!function_exists('class_basename')) {
118
    /**
119
     * 获取类名(不包含命名空间)
120
     *
121
     * @param  mixed $class 类名
122
     * @return string
123
     */
124
    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...
125
    {
126
        $class = is_object($class) ? get_class($class) : $class;
127
        return basename(str_replace('\\', '/', $class));
128
    }
129
}
130
131
if (!function_exists('class_uses_recursive')) {
132
    /**
133
     *获取一个类里所有用到的trait,包括父类的
0 ignored issues
show
Coding Style introduced by
Expected 1 space after asterisk; 0 found
Loading history...
134
     *
135
     * @param  mixed $class 类名
136
     * @return array
137
     */
138
    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...
139
    {
140
        if (is_object($class)) {
141
            $class = get_class($class);
142
        }
143
144
        $results = [];
145
        $classes = array_merge([$class => $class], class_parents($class));
146
        foreach ($classes as $class) {
0 ignored issues
show
introduced by
$class is overwriting one of the parameters of this function.
Loading history...
147
            $results += trait_uses_recursive($class);
148
        }
149
150
        return array_unique($results);
151
    }
152
}
153
154
if (!function_exists('config')) {
155
    /**
156
     * 获取和设置配置参数
157
     * @param  string|array $name  参数名
158
     * @param  mixed        $value 参数值
159
     * @return mixed
160
     */
161
    function config($name = '', $value = null)
162
    {
163
        if (is_array($name)) {
164
            return Config::set($name, $value);
165
        }
166
167
        return 0 === strpos($name, '?') ? Config::has(substr($name, 1)) : Config::get($name, $value);
168
    }
169
}
170
171
if (!function_exists('cookie')) {
172
    /**
173
     * Cookie管理
174
     * @param  string $name   cookie名称
175
     * @param  mixed  $value  cookie值
176
     * @param  mixed  $option 参数
177
     * @return mixed
178
     */
179
    function cookie(string $name, $value = '', $option = null)
180
    {
181
        if (is_null($value)) {
182
            // 删除
183
            Cookie::delete($name);
184
        } elseif ('' === $value) {
185
            // 获取
186
            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

186
            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

186
            return 0 === strpos($name, '?') ? Cookie::has(substr($name, 1)) : Cookie::/** @scrutinizer ignore-call */ get($name);
Loading history...
187
        } else {
188
            // 设置
189
            return Cookie::set($name, $value, $option);
190
        }
191
    }
192
}
193
194
if (!function_exists('download')) {
195
    /**
196
     * 获取\think\response\Download对象实例
197
     * @param  string $filename 要下载的文件
198
     * @param  string $name     显示文件名
199
     * @param  bool   $content  是否为内容
200
     * @param  int    $expire   有效期(秒)
201
     * @return \think\response\File
202
     */
203
    function download(string $filename, string $name = '', bool $content = false, int $expire = 180)
204
    {
205
        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

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

412
        return Response::create($url, 'redirect', $code)->/** @scrutinizer ignore-call */ params($params);
Loading history...
413
    }
414
}
415
416
if (!function_exists('request')) {
417
    /**
418
     * 获取当前Request对象实例
419
     * @return Request
420
     */
421
    function request()
422
    {
423
        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...
424
    }
425
}
426
427
if (!function_exists('response')) {
428
    /**
429
     * 创建普通 Response 对象实例
430
     * @param  mixed      $data   输出数据
431
     * @param  int|string $code   状态码
432
     * @param  array      $header 头信息
433
     * @param  string     $type
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
434
     * @return Response
435
     */
436
    function response($data = '', $code = 200, $header = [], $type = 'html')
437
    {
438
        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

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

605
        return Response::create($template, 'view', $code)->/** @scrutinizer ignore-call */ assign($vars)->filter($filter);
Loading history...
606
    }
607
}
608
609
if (!function_exists('display')) {
610
    /**
611
     * 渲染模板输出
612
     * @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...
613
     * @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...
614
     * @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...
615
     * @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...
616
     * @return \think\response\View
617
     */
618
    function display(string $content, $vars = [], $code = 200, $filter = null)
619
    {
620
        return Response::create($template, 'view', $code)->isContent(true)->assign($vars)->filter($filter);
0 ignored issues
show
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

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