Completed
Push — 6.0 ( 82fa34...f22458 )
by liu
03:44
created

yaconf()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
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\Validate;
33
34
if (!function_exists('abort')) {
35
    /**
36
     * 抛出HTTP异常
37
     * @param integer|Response $code    状态码 或者 Response对象实例
38
     * @param string           $message 错误信息
39
     * @param array            $header  参数
40
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
41
    function abort($code, string $message = null, array $header = [])
42
    {
43
        if ($code instanceof Response) {
44
            throw new HttpResponseException($code);
45
        } else {
46
            throw new HttpException($code, $message, null, $header);
47
        }
48
    }
49
}
50
51
if (!function_exists('app')) {
52
    /**
53
     * 快速获取容器中的实例 支持依赖注入
54
     * @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...
55
     * @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...
56
     * @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...
57
     * @return object|App
58
     */
59
    function app(string $name = '', array $args = [], bool $newInstance = false)
60
    {
61
        return Container::getInstance()->make($name ?: App::class, $args, $newInstance);
62
    }
63
}
64
65
if (!function_exists('bind')) {
66
    /**
67
     * 绑定一个类到容器
68
     * @param  string|array $abstract 类标识、接口(支持批量绑定)
69
     * @param  mixed        $concrete 要绑定的类、闭包或者实例
70
     * @return Container
71
     */
72
    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...
73
    {
74
        return Container::getInstance()->bind($abstract, $concrete);
75
    }
76
}
77
78
if (!function_exists('cache')) {
79
    /**
80
     * 缓存管理
81
     * @param  mixed  $name    缓存名称,如果为数组表示进行缓存设置
82
     * @param  mixed  $value   缓存值
83
     * @param  mixed  $options 缓存参数
84
     * @param  string $tag     缓存标签
85
     * @return mixed
86
     */
87
    function cache($name, $value = '', $options = null, $tag = null)
88
    {
89
        if (is_array($name)) {
90
            // 缓存初始化
91
            return Cache::connect($name);
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::rm($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
            } else {
297
                $method = 'param';
298
            }
299
        } else {
300
            // 默认为自动判断
301
            $method = 'param';
302
        }
303
304
        return isset($has) ?
305
        request()->has($key, $method) :
306
        request()->$method($key, $default, $filter);
307
    }
308
}
309
310
if (!function_exists('invoke')) {
311
    /**
312
     * 调用反射实例化对象或者执行方法 支持依赖注入
313
     * @param  mixed $call 类名或者callable
314
     * @param  array $args 参数
315
     * @return mixed
316
     */
317
    function invoke($call, array $args = [])
318
    {
319
        if (is_callable($call)) {
320
            return Container::getInstance()->invoke($call, $args);
321
        }
322
323
        return Container::getInstance()->invokeClass($call, $args);
324
    }
325
}
326
327
if (!function_exists('json')) {
328
    /**
329
     * 获取\think\response\Json对象实例
330
     * @param  mixed $data    返回的数据
331
     * @param  int   $code    状态码
332
     * @param  array $header  头部
333
     * @param  array $options 参数
334
     * @return \think\response\Json
335
     */
336
    function json($data = [], $code = 200, $header = [], $options = [])
337
    {
338
        return Response::create($data, 'json', $code)->header($header)->options($options);
339
    }
340
}
341
342
if (!function_exists('jsonp')) {
343
    /**
344
     * 获取\think\response\Jsonp对象实例
345
     * @param  mixed $data    返回的数据
346
     * @param  int   $code    状态码
347
     * @param  array $header  头部
348
     * @param  array $options 参数
349
     * @return \think\response\Jsonp
350
     */
351
    function jsonp($data = [], $code = 200, $header = [], $options = [])
352
    {
353
        return Response::create($data, 'jsonp', $code)->header($header)->options($options);
354
    }
355
}
356
357
if (!function_exists('lang')) {
358
    /**
359
     * 获取语言变量值
360
     * @param  string $name 语言变量名
361
     * @param  array  $vars 动态变量值
362
     * @param  string $lang 语言
363
     * @return mixed
364
     */
365
    function lang(string $name, array $vars = [], string $lang = '')
366
    {
367
        return Lang::get($name, $vars, $lang);
368
    }
369
}
370
371
if (!function_exists('parse_name')) {
372
    /**
373
     * 字符串命名风格转换
374
     * type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格
375
     * @param  string $name    字符串
376
     * @param  int    $type    转换类型
377
     * @param  bool   $ucfirst 首字母是否大写(驼峰规则)
378
     * @return string
379
     */
380
    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...
381
    {
382
        if ($type) {
383
            $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...
384
                return strtoupper($match[1]);
385
            }, $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...
386
387
            return $ucfirst ? ucfirst($name) : lcfirst($name);
388
        }
389
390
        return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
391
    }
392
}
393
394
if (!function_exists('redirect')) {
395
    /**
396
     * 获取\think\response\Redirect对象实例
397
     * @param  mixed         $url    重定向地址 支持Url::build方法的地址
398
     * @param  array|integer $params 额外参数
399
     * @param  int           $code   状态码
400
     * @return \think\response\Redirect
401
     */
402
    function redirect($url = [], $params = [], $code = 302)
403
    {
404
        if (is_integer($params)) {
405
            $code   = $params;
406
            $params = [];
407
        }
408
409
        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

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

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

602
        return Response::create($template, 'view', $code)->/** @scrutinizer ignore-call */ assign($vars)->filter($filter);
Loading history...
603
    }
604
}
605
606
if (!function_exists('xml')) {
607
    /**
608
     * 获取\think\response\Xml对象实例
609
     * @param  mixed $data    返回的数据
610
     * @param  int   $code    状态码
611
     * @param  array $header  头部
612
     * @param  array $options 参数
613
     * @return \think\response\Xml
614
     */
615
    function xml($data = [], $code = 200, $header = [], $options = [])
616
    {
617
        return Response::create($data, 'xml', $code)->header($header)->options($options);
618
    }
619
}
620