Completed
Push — 6.0 ( ed6998...c2c2ec )
by liu
03:19
created

raw()   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
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
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\Model;
32
use think\model\Collection as ModelCollection;
33
use think\Response;
34
use think\Validate;
35
36
if (!function_exists('abort')) {
37
    /**
38
     * 抛出HTTP异常
39
     * @param integer|Response $code    状态码 或者 Response对象实例
40
     * @param string           $message 错误信息
41
     * @param array            $header  参数
42
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
43
    function abort($code, string $message = null, array $header = [])
44
    {
45
        if ($code instanceof Response) {
46
            throw new HttpResponseException($code);
47
        } else {
48
            throw new HttpException($code, $message, null, $header);
49
        }
50
    }
51
}
52
53
if (!function_exists('app')) {
54
    /**
55
     * 快速获取容器中的实例 支持依赖注入
56
     * @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...
57
     * @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...
58
     * @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...
59
     * @return object|App
60
     */
61
    function app(string $name = '', array $args = [], bool $newInstance = false)
62
    {
63
        return Container::getInstance()->make($name ?: App::class, $args, $newInstance);
64
    }
65
}
66
67
if (!function_exists('bind')) {
68
    /**
69
     * 绑定一个类到容器
70
     * @param  string|array $abstract 类标识、接口(支持批量绑定)
71
     * @param  mixed        $concrete 要绑定的类、闭包或者实例
72
     * @return Container
73
     */
74
    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...
75
    {
76
        return Container::getInstance()->bind($abstract, $concrete);
77
    }
78
}
79
80
if (!function_exists('cache')) {
81
    /**
82
     * 缓存管理
83
     * @param  mixed  $name    缓存名称,如果为数组表示进行缓存设置
84
     * @param  mixed  $value   缓存值
85
     * @param  mixed  $options 缓存参数
86
     * @param  string $tag     缓存标签
87
     * @return mixed
88
     */
89
    function cache($name, $value = '', $options = null, $tag = null)
90
    {
91
        if (is_array($name)) {
92
            // 缓存初始化
93
            return Cache::connect($name);
94
        }
95
96
        if ('' === $value) {
97
            // 获取缓存
98
            return 0 === strpos($name, '?') ? Cache::has(substr($name, 1)) : Cache::get($name);
99
        } elseif (is_null($value)) {
100
            // 删除缓存
101
            return Cache::rm($name);
102
        }
103
104
        // 缓存数据
105
        if (is_array($options)) {
106
            $expire = $options['expire'] ?? null; //修复查询缓存无法设置过期时间
107
        } else {
108
            $expire = $options;
109
        }
110
111
        if (is_null($tag)) {
112
            return Cache::set($name, $value, $expire);
113
        } else {
114
            return Cache::tag($tag)->set($name, $value, $expire);
115
        }
116
    }
117
}
118
119
if (!function_exists('class_basename')) {
120
    /**
121
     * 获取类名(不包含命名空间)
122
     *
123
     * @param  mixed $class 类名
124
     * @return string
125
     */
126
    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...
127
    {
128
        $class = is_object($class) ? get_class($class) : $class;
129
        return basename(str_replace('\\', '/', $class));
130
    }
131
}
132
133
if (!function_exists('class_uses_recursive')) {
134
    /**
135
     *获取一个类里所有用到的trait,包括父类的
0 ignored issues
show
Coding Style introduced by
Expected 1 space after asterisk; 0 found
Loading history...
136
     *
137
     * @param  mixed $class 类名
138
     * @return array
139
     */
140
    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...
141
    {
142
        if (is_object($class)) {
143
            $class = get_class($class);
144
        }
145
146
        $results = [];
147
        $classes = array_merge([$class => $class], class_parents($class));
148
        foreach ($classes as $class) {
0 ignored issues
show
introduced by
$class is overwriting one of the parameters of this function.
Loading history...
149
            $results += trait_uses_recursive($class);
150
        }
151
152
        return array_unique($results);
153
    }
154
}
155
156
if (!function_exists('config')) {
157
    /**
158
     * 获取和设置配置参数
159
     * @param  string|array $name  参数名
160
     * @param  mixed        $value 参数值
161
     * @return mixed
162
     */
163
    function config($name = '', $value = null)
164
    {
165
        if (is_array($name)) {
166
            return Config::set($name, $value);
167
        }
168
169
        return 0 === strpos($name, '?') ? Config::has(substr($name, 1)) : Config::get($name, $value);
170
    }
171
}
172
173
if (!function_exists('cookie')) {
174
    /**
175
     * Cookie管理
176
     * @param  string $name   cookie名称
177
     * @param  mixed  $value  cookie值
178
     * @param  mixed  $option 参数
179
     * @return mixed
180
     */
181
    function cookie(string $name, $value = '', $option = null)
182
    {
183
        if (is_null($value)) {
184
            // 删除
185
            Cookie::delete($name);
186
        } elseif ('' === $value) {
187
            // 获取
188
            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

188
            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

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

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

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

449
        return Response::create($data, $type, /** @scrutinizer ignore-type */ $code)->header($header);
Loading history...
450
    }
451
}
452
453
if (!function_exists('session')) {
454
    /**
455
     * Session管理
456
     * @param  string $name  session名称
457
     * @param  mixed  $value session值
458
     * @return mixed
459
     */
460
    function session(string $name = null, $value = '')
461
    {
462
        if (is_null($name)) {
463
            // 清除
464
            Session::clear();
465
        } elseif (is_null($value)) {
466
            // 删除
467
            Session::delete($name);
468
        } elseif ('' === $value) {
469
            // 判断或获取
470
            return 0 === strpos($name, '?') ? Session::has(substr($name, 1)) : Session::get($name);
471
        } else {
472
            // 设置
473
            Session::set($name, $value);
474
        }
475
    }
476
}
477
478
if (!function_exists('token')) {
479
    /**
480
     * 获取Token令牌
481
     * @param  string $name 令牌名称
482
     * @param  mixed  $type 令牌生成方法
483
     * @return string
484
     */
485
    function token(string $name = '__token__', string $type = 'md5'): string
486
    {
487
        return Request::buildToken($name, $type);
0 ignored issues
show
Bug introduced by
The method buildToken() does not exist on think\facade\Request. 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

487
        return Request::/** @scrutinizer ignore-call */ buildToken($name, $type);
Loading history...
488
    }
489
}
490
491
if (!function_exists('token_field')) {
492
    /**
493
     * 生成令牌隐藏表单
494
     * @param  string $name 令牌名称
495
     * @param  mixed  $type 令牌生成方法
496
     * @return string
497
     */
498
    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...
499
    {
500
        $token = Request::buildToken($name, $type);
501
502
        return '<input type="hidden" name="' . $name . '" value="' . $token . '" />';
503
    }
504
}
505
506
if (!function_exists('token_meta')) {
507
    /**
508
     * 生成令牌meta
509
     * @param  string $name 令牌名称
510
     * @param  mixed  $type 令牌生成方法
511
     * @return string
512
     */
513
    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...
514
    {
515
        $token = Request::buildToken($name, $type);
516
517
        return '<meta name="csrf-token" content="' . $token . '">';
518
    }
519
}
520
521
if (!function_exists('trace')) {
522
    /**
523
     * 记录日志信息
524
     * @param  mixed  $log   log信息 支持字符串和数组
525
     * @param  string $level 日志级别
526
     * @return array|void
527
     */
528
    function trace($log = '[think]', string $level = 'log')
529
    {
530
        if ('[think]' === $log) {
531
            return Log::getLog();
532
        }
533
534
        Log::record($log, $level);
535
    }
536
}
537
538
if (!function_exists('trait_uses_recursive')) {
539
    /**
540
     * 获取一个trait里所有引用到的trait
541
     *
542
     * @param  string $trait Trait
543
     * @return array
544
     */
545
    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...
546
    {
547
        $traits = class_uses($trait);
548
        foreach ($traits as $trait) {
549
            $traits += trait_uses_recursive($trait);
550
        }
551
552
        return $traits;
553
    }
554
}
555
556
if (!function_exists('url')) {
557
    /**
558
     * Url生成
559
     * @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...
560
     * @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...
561
     * @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...
562
     * @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...
563
     * @return string
564
     */
565
    function url(string $url = '', array $vars = [], $suffix = true, $domain = false): string
566
    {
567
        return Route::buildUrl($url, $vars, $suffix, $domain);
568
    }
569
}
570
571
if (!function_exists('validate')) {
572
    /**
573
     * 验证数据
574
     * @param  array        $data     数据
575
     * @param  string|array $validate 验证器名或者验证规则数组
576
     * @param  array        $message  提示信息
577
     * @param  bool         $batch    是否批量验证
578
     * @return bool
579
     * @throws ValidateException
580
     */
581
    function validate(array $data, $validate, array $message = [], bool $batch = false): bool
582
    {
583
        if (is_array($validate)) {
584
            $v = new Validate();
585
            $v->rule($validate);
586
        } else {
587
            if (strpos($validate, '.')) {
588
                // 支持场景
589
                list($validate, $scene) = explode('.', $validate);
590
            }
591
592
            $class = false !== strpos($validate, '\\') ? $validate : app()->parseClass('validate', $validate);
593
594
            $v = new $class();
595
596
            if (!empty($scene)) {
597
                $v->scene($scene);
598
            }
599
        }
600
601
        return $v->message($message)->batch($batch)->failException(true)->check($data);
602
    }
603
}
604
605
if (!function_exists('view')) {
606
    /**
607
     * 渲染模板输出
608
     * @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...
609
     * @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...
610
     * @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...
611
     * @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...
612
     * @return \think\response\View
613
     */
614
    function view(string $template = '', $vars = [], $code = 200, $filter = null)
615
    {
616
        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

616
        return Response::create($template, 'view', $code)->/** @scrutinizer ignore-call */ 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
635
if (!function_exists('yaconf')) {
636
    /**
637
     * 获取yaconf配置
638
     *
639
     * @param  string $name    配置参数名
640
     * @param  mixed  $default 默认值
641
     * @return mixed
642
     */
643
    function yaconf(string $name, $default = null)
644
    {
645
        return Config::yaconf($name, $default);
646
    }
647
}
648