Completed
Push — 6.0 ( 48e19c...35fa0f )
by yun
05:01
created

app_path()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 3
rs 10
1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
//------------------------
14
// ThinkPHP 助手函数
15
//-------------------------
16
17
use think\App;
18
use think\Container;
19
use think\exception\HttpException;
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\response\File;
33
use think\response\Json;
34
use think\response\Jsonp;
35
use think\response\Redirect;
36
use think\response\View;
37
use think\response\Xml;
38
use think\route\Url as UrlBuild;
39
use think\Validate;
40
41
if (!function_exists('abort')) {
42
    /**
43
     * 抛出HTTP异常
44
     * @param integer|Response $code    状态码 或者 Response对象实例
45
     * @param string           $message 错误信息
46
     * @param array            $header  参数
47
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
48
    function abort($code, string $message = null, array $header = [])
49
    {
50
        if ($code instanceof Response) {
51
            throw new HttpResponseException($code);
52
        } else {
53
            throw new HttpException($code, $message, null, $header);
54
        }
55
    }
56
}
57
58
if (!function_exists('app')) {
59
    /**
60
     * 快速获取容器中的实例 支持依赖注入
61
     * @param string $name        类名或标识 默认获取当前应用实例
62
     * @param array  $args        参数
63
     * @param bool   $newInstance 是否每次创建新的实例
64
     * @return object|App
65
     */
66
    function app(string $name = '', array $args = [], bool $newInstance = false)
67
    {
68
        return Container::getInstance()->make($name ?: App::class, $args, $newInstance);
69
    }
70
}
71
72
if (!function_exists('bind')) {
73
    /**
74
     * 绑定一个类到容器
75
     * @param string|array $abstract 类标识、接口(支持批量绑定)
76
     * @param mixed        $concrete 要绑定的类、闭包或者实例
77
     * @return Container
78
     */
79
    function bind($abstract, $concrete = null)
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...
80
    {
81
        return Container::getInstance()->bind($abstract, $concrete);
82
    }
83
}
84
85
if (!function_exists('cache')) {
86
    /**
87
     * 缓存管理
88
     * @param string $name    缓存名称
89
     * @param mixed  $value   缓存值
90
     * @param mixed  $options 缓存参数
91
     * @param string $tag     缓存标签
92
     * @return mixed
93
     */
94
    function cache(string $name = null, $value = '', $options = null, $tag = null)
95
    {
96
        if (is_null($name)) {
97
            return app('cache');
98
        }
99
100
        if ('' === $value) {
101
            // 获取缓存
102
            return 0 === strpos($name, '?') ? Cache::has(substr($name, 1)) : Cache::get($name);
0 ignored issues
show
Bug introduced by
The method get() does not exist on think\facade\Cache. 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

102
            return 0 === strpos($name, '?') ? Cache::has(substr($name, 1)) : Cache::/** @scrutinizer ignore-call */ get($name);
Loading history...
Bug introduced by
The method has() does not exist on think\facade\Cache. 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

102
            return 0 === strpos($name, '?') ? Cache::/** @scrutinizer ignore-call */ has(substr($name, 1)) : Cache::get($name);
Loading history...
103
        } elseif (is_null($value)) {
104
            // 删除缓存
105
            return Cache::delete($name);
0 ignored issues
show
Bug introduced by
The method delete() does not exist on think\facade\Cache. 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

105
            return Cache::/** @scrutinizer ignore-call */ delete($name);
Loading history...
106
        }
107
108
        // 缓存数据
109
        if (is_array($options)) {
110
            $expire = $options['expire'] ?? null; //修复查询缓存无法设置过期时间
111
        } else {
112
            $expire = $options;
113
        }
114
115
        if (is_null($tag)) {
116
            return Cache::set($name, $value, $expire);
0 ignored issues
show
Bug introduced by
The method set() does not exist on think\facade\Cache. 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

116
            return Cache::/** @scrutinizer ignore-call */ set($name, $value, $expire);
Loading history...
117
        } else {
118
            return Cache::tag($tag)->set($name, $value, $expire);
0 ignored issues
show
Bug introduced by
The method tag() does not exist on think\facade\Cache. 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

118
            return Cache::/** @scrutinizer ignore-call */ tag($tag)->set($name, $value, $expire);
Loading history...
119
        }
120
    }
121
}
122
123
if (!function_exists('class_basename')) {
124
    /**
125
     * 获取类名(不包含命名空间)
126
     *
127
     * @param mixed $class 类名
128
     * @return string
129
     */
130
    function class_basename($class): string
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...
131
    {
132
        $class = is_object($class) ? get_class($class) : $class;
133
        return basename(str_replace('\\', '/', $class));
134
    }
135
}
136
137
if (!function_exists('class_uses_recursive')) {
138
    /**
139
     *获取一个类里所有用到的trait,包括父类的
0 ignored issues
show
Coding Style introduced by
Expected 1 space after asterisk; 0 found
Loading history...
140
     *
141
     * @param mixed $class 类名
142
     * @return array
143
     */
144
    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...
145
    {
146
        if (is_object($class)) {
147
            $class = get_class($class);
148
        }
149
150
        $results = [];
151
        $classes = array_merge([$class => $class], class_parents($class));
152
        foreach ($classes as $class) {
0 ignored issues
show
introduced by
$class is overwriting one of the parameters of this function.
Loading history...
153
            $results += trait_uses_recursive($class);
154
        }
155
156
        return array_unique($results);
157
    }
158
}
159
160
if (!function_exists('config')) {
161
    /**
162
     * 获取和设置配置参数
163
     * @param string|array $name  参数名
164
     * @param mixed        $value 参数值
165
     * @return mixed
166
     */
167
    function config($name = '', $value = null)
168
    {
169
        if (is_array($name)) {
170
            return Config::set($name, $value);
0 ignored issues
show
Bug introduced by
The method set() does not exist on think\facade\Config. 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

170
            return Config::/** @scrutinizer ignore-call */ set($name, $value);
Loading history...
171
        }
172
173
        return 0 === strpos($name, '?') ? Config::has(substr($name, 1)) : Config::get($name, $value);
0 ignored issues
show
Bug introduced by
The method has() does not exist on think\facade\Config. 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

173
        return 0 === strpos($name, '?') ? Config::/** @scrutinizer ignore-call */ has(substr($name, 1)) : Config::get($name, $value);
Loading history...
Bug introduced by
The method get() does not exist on think\facade\Config. 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

173
        return 0 === strpos($name, '?') ? Config::has(substr($name, 1)) : Config::/** @scrutinizer ignore-call */ get($name, $value);
Loading history...
174
    }
175
}
176
177
if (!function_exists('cookie')) {
178
    /**
179
     * Cookie管理
180
     * @param string $name   cookie名称
181
     * @param mixed  $value  cookie值
182
     * @param mixed  $option 参数
183
     * @return mixed
184
     */
185
    function cookie(string $name, $value = '', $option = null)
186
    {
187
        if (is_null($value)) {
188
            // 删除
189
            Cookie::delete($name);
0 ignored issues
show
Bug introduced by
The method delete() 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

189
            Cookie::/** @scrutinizer ignore-call */ 
190
                    delete($name);
Loading history...
190
        } elseif ('' === $value) {
191
            // 获取
192
            return 0 === strpos($name, '?') ? Cookie::has(substr($name, 1)) : Cookie::get($name);
0 ignored issues
show
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

192
            return 0 === strpos($name, '?') ? Cookie::has(substr($name, 1)) : Cookie::/** @scrutinizer ignore-call */ get($name);
Loading history...
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

192
            return 0 === strpos($name, '?') ? Cookie::/** @scrutinizer ignore-call */ has(substr($name, 1)) : Cookie::get($name);
Loading history...
193
        } else {
194
            // 设置
195
            return Cookie::set($name, $value, $option);
0 ignored issues
show
Bug introduced by
The method set() 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

195
            return Cookie::/** @scrutinizer ignore-call */ set($name, $value, $option);
Loading history...
196
        }
197
    }
198
}
199
200
if (!function_exists('download')) {
201
    /**
202
     * 获取\think\response\Download对象实例
203
     * @param string $filename 要下载的文件
204
     * @param string $name     显示文件名
205
     * @param bool   $content  是否为内容
206
     * @param int    $expire   有效期(秒)
207
     * @return \think\response\File
208
     */
209
    function download(string $filename, string $name = '', bool $content = false, int $expire = 180): File
210
    {
211
        return Response::create($filename, 'file')->name($name)->isContent($content)->expire($expire);
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

211
        return Response::create($filename, 'file')->/** @scrutinizer ignore-call */ name($name)->isContent($content)->expire($expire);
Loading history...
212
    }
213
}
214
215
if (!function_exists('dump')) {
216
    /**
0 ignored issues
show
Coding Style introduced by
Parameter ...$vars should have a doc-comment as per coding-style.
Loading history...
217
     * 浏览器友好的变量输出
218
     * @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...
219
     * @return void
220
     */
221
    function dump(...$vars)
222
    {
223
        ob_start();
224
        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...
225
226
        $output = ob_get_clean();
227
        $output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output);
228
229
        if (PHP_SAPI == 'cli') {
230
            $output = PHP_EOL . $output . PHP_EOL;
231
        } else {
232
            if (!extension_loaded('xdebug')) {
233
                $output = htmlspecialchars($output, ENT_SUBSTITUTE);
234
            }
235
            $output = '<pre>' . $output . '</pre>';
236
        }
237
238
        echo $output;
239
    }
240
}
241
242
if (!function_exists('env')) {
243
    /**
244
     * 获取环境变量值
245
     * @access public
246
     * @param string $name    环境变量名(支持二级 .号分割)
247
     * @param string $default 默认值
248
     * @return mixed
249
     */
250
    function env(string $name = null, $default = null)
251
    {
252
        return Env::get($name, $default);
0 ignored issues
show
Bug introduced by
The method get() does not exist on think\facade\Env. 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

252
        return Env::/** @scrutinizer ignore-call */ get($name, $default);
Loading history...
253
    }
254
}
255
256
if (!function_exists('event')) {
257
    /**
258
     * 触发事件
259
     * @param mixed $event 事件名(或者类名)
260
     * @param mixed $args  参数
261
     * @return mixed
262
     */
263
    function event($event, $args = null)
264
    {
265
        return Event::trigger($event, $args);
0 ignored issues
show
Bug introduced by
The method trigger() does not exist on think\facade\Event. 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

265
        return Event::/** @scrutinizer ignore-call */ trigger($event, $args);
Loading history...
266
    }
267
}
268
269
if (!function_exists('halt')) {
270
    /**
0 ignored issues
show
Coding Style introduced by
Parameter ...$vars should have a doc-comment as per coding-style.
Loading history...
271
     * 调试变量并且中断输出
272
     * @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...
273
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
274
    function halt(...$vars)
275
    {
276
        dump(...$vars);
277
278
        throw new HttpResponseException(new Response);
279
    }
280
}
281
282
if (!function_exists('input')) {
283
    /**
284
     * 获取输入数据 支持默认值和过滤
285
     * @param string $key     获取的变量名
286
     * @param mixed  $default 默认值
287
     * @param string $filter  过滤方法
288
     * @return mixed
289
     */
290
    function input(string $key = '', $default = null, $filter = '')
291
    {
292
        if (0 === strpos($key, '?')) {
293
            $key = substr($key, 1);
294
            $has = true;
295
        }
296
297
        if ($pos = strpos($key, '.')) {
298
            // 指定参数来源
299
            $method = substr($key, 0, $pos);
300
            if (in_array($method, ['get', 'post', 'put', 'patch', 'delete', 'route', 'param', 'request', 'session', 'cookie', 'server', 'env', 'path', 'file'])) {
301
                $key = substr($key, $pos + 1);
302
                if ('server' == $method && is_null($default)) {
303
                    $default = '';
304
                }
305
            } else {
306
                $method = 'param';
307
            }
308
        } else {
309
            // 默认为自动判断
310
            $method = 'param';
311
        }
312
313
        return isset($has) ?
314
            request()->has($key, $method) :
315
            request()->$method($key, $default, $filter);
316
    }
317
}
318
319
if (!function_exists('invoke')) {
320
    /**
321
     * 调用反射实例化对象或者执行方法 支持依赖注入
322
     * @param mixed $call 类名或者callable
323
     * @param array $args 参数
324
     * @return mixed
325
     */
326
    function invoke($call, array $args = [])
327
    {
328
        if (is_callable($call)) {
329
            return Container::getInstance()->invoke($call, $args);
330
        }
331
332
        return Container::getInstance()->invokeClass($call, $args);
333
    }
334
}
335
336
if (!function_exists('json')) {
337
    /**
338
     * 获取\think\response\Json对象实例
339
     * @param mixed $data    返回的数据
340
     * @param int   $code    状态码
341
     * @param array $header  头部
342
     * @param array $options 参数
343
     * @return \think\response\Json
344
     */
345
    function json($data = [], $code = 200, $header = [], $options = []): Json
346
    {
347
        return Response::create($data, 'json', $code)->header($header)->options($options);
0 ignored issues
show
Bug Best Practice introduced by
The expression return think\Response::c...der)->options($options) returns the type think\Response which includes types incompatible with the type-hinted return think\response\Json.
Loading history...
348
    }
349
}
350
351
if (!function_exists('jsonp')) {
352
    /**
353
     * 获取\think\response\Jsonp对象实例
354
     * @param mixed $data    返回的数据
355
     * @param int   $code    状态码
356
     * @param array $header  头部
357
     * @param array $options 参数
358
     * @return \think\response\Jsonp
359
     */
360
    function jsonp($data = [], $code = 200, $header = [], $options = []): Jsonp
361
    {
362
        return Response::create($data, 'jsonp', $code)->header($header)->options($options);
0 ignored issues
show
Bug Best Practice introduced by
The expression return think\Response::c...der)->options($options) returns the type think\Response which includes types incompatible with the type-hinted return think\response\Jsonp.
Loading history...
363
    }
364
}
365
366
if (!function_exists('lang')) {
367
    /**
368
     * 获取语言变量值
369
     * @param string $name 语言变量名
370
     * @param array  $vars 动态变量值
371
     * @param string $lang 语言
372
     * @return mixed
373
     */
374
    function lang(string $name, array $vars = [], string $lang = '')
375
    {
376
        return Lang::get($name, $vars, $lang);
0 ignored issues
show
Bug introduced by
The method get() does not exist on think\facade\Lang. 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

376
        return Lang::/** @scrutinizer ignore-call */ get($name, $vars, $lang);
Loading history...
377
    }
378
}
379
380
if (!function_exists('parse_name')) {
381
    /**
382
     * 字符串命名风格转换
383
     * type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格
384
     * @param string $name    字符串
385
     * @param int    $type    转换类型
386
     * @param bool   $ucfirst 首字母是否大写(驼峰规则)
387
     * @return string
388
     */
389
    function parse_name(string $name, int $type = 0, bool $ucfirst = true): string
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...
390
    {
391
        if ($type) {
392
            $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...
393
                return strtoupper($match[1]);
394
            }, $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...
395
396
            return $ucfirst ? ucfirst($name) : lcfirst($name);
397
        }
398
399
        return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
400
    }
401
}
402
403
if (!function_exists('redirect')) {
404
    /**
405
     * 获取\think\response\Redirect对象实例
406
     * @param mixed         $url    重定向地址 支持Url::build方法的地址
407
     * @param array|integer $params 额外参数
408
     * @param int           $code   状态码
409
     * @return \think\response\Redirect
410
     */
411
    function redirect($url = [], $params = [], $code = 302): Redirect
412
    {
413
        if (is_integer($params)) {
414
            $code   = $params;
415
            $params = [];
416
        }
417
418
        return Response::create($url, 'redirect', $code)->params($params);
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

418
        return Response::create($url, 'redirect', $code)->/** @scrutinizer ignore-call */ params($params);
Loading history...
419
    }
420
}
421
422
if (!function_exists('request')) {
423
    /**
424
     * 获取当前Request对象实例
425
     * @return Request
426
     */
427
    function request(): \think\Request
428
    {
429
        return app('request');
0 ignored issues
show
Bug Best Practice introduced by
The expression return app('request') could return the type think\App which is incompatible with the type-hinted return think\Request. Consider adding an additional type-check to rule them out.
Loading history...
430
    }
431
}
432
433
if (!function_exists('response')) {
434
    /**
435
     * 创建普通 Response 对象实例
436
     * @param mixed      $data   输出数据
437
     * @param int|string $code   状态码
438
     * @param array      $header 头信息
439
     * @param string     $type
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
440
     * @return Response
441
     */
442
    function response($data = '', $code = 200, $header = [], $type = 'html'): Response
443
    {
444
        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

444
        return Response::create($data, $type, /** @scrutinizer ignore-type */ $code)->header($header);
Loading history...
445
    }
446
}
447
448
if (!function_exists('session')) {
449
    /**
450
     * Session管理
451
     * @param string $name  session名称
452
     * @param mixed  $value session值
453
     * @return mixed
454
     */
455
    function session(string $name = null, $value = '')
456
    {
457
        if (is_null($name)) {
458
            // 清除
459
            Session::clear();
0 ignored issues
show
Bug introduced by
The method clear() does not exist on think\facade\Session. 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

459
            Session::/** @scrutinizer ignore-call */ 
460
                     clear();
Loading history...
460
        } elseif (is_null($value)) {
461
            // 删除
462
            Session::delete($name);
0 ignored issues
show
Bug introduced by
The method delete() does not exist on think\facade\Session. 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

462
            Session::/** @scrutinizer ignore-call */ 
463
                     delete($name);
Loading history...
463
        } elseif ('' === $value) {
464
            // 判断或获取
465
            return 0 === strpos($name, '?') ? Session::has(substr($name, 1)) : Session::get($name);
0 ignored issues
show
Bug introduced by
The method get() does not exist on think\facade\Session. 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

465
            return 0 === strpos($name, '?') ? Session::has(substr($name, 1)) : Session::/** @scrutinizer ignore-call */ get($name);
Loading history...
Bug introduced by
The method has() does not exist on think\facade\Session. 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

465
            return 0 === strpos($name, '?') ? Session::/** @scrutinizer ignore-call */ has(substr($name, 1)) : Session::get($name);
Loading history...
466
        } else {
467
            // 设置
468
            Session::set($name, $value);
0 ignored issues
show
Bug introduced by
The method set() does not exist on think\facade\Session. 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

468
            Session::/** @scrutinizer ignore-call */ 
469
                     set($name, $value);
Loading history...
469
        }
470
    }
471
}
472
473
if (!function_exists('token')) {
474
    /**
475
     * 获取Token令牌
476
     * @param string $name 令牌名称
477
     * @param mixed  $type 令牌生成方法
478
     * @return string
479
     */
480
    function token(string $name = '__token__', string $type = 'md5'): string
481
    {
482
        return Request::buildToken($name, $type);
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

482
        return Request::/** @scrutinizer ignore-call */ buildToken($name, $type);
Loading history...
483
    }
484
}
485
486
if (!function_exists('token_field')) {
487
    /**
488
     * 生成令牌隐藏表单
489
     * @param string $name 令牌名称
490
     * @param mixed  $type 令牌生成方法
491
     * @return string
492
     */
493
    function token_field(string $name = '__token__', string $type = 'md5'): string
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...
494
    {
495
        $token = Request::buildToken($name, $type);
496
497
        return '<input type="hidden" name="' . $name . '" value="' . $token . '" />';
498
    }
499
}
500
501
if (!function_exists('token_meta')) {
502
    /**
503
     * 生成令牌meta
504
     * @param string $name 令牌名称
505
     * @param mixed  $type 令牌生成方法
506
     * @return string
507
     */
508
    function token_meta(string $name = '__token__', string $type = 'md5'): string
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...
509
    {
510
        $token = Request::buildToken($name, $type);
511
512
        return '<meta name="csrf-token" content="' . $token . '">';
513
    }
514
}
515
516
if (!function_exists('trace')) {
517
    /**
518
     * 记录日志信息
519
     * @param mixed  $log   log信息 支持字符串和数组
520
     * @param string $level 日志级别
521
     * @return array|void
522
     */
523
    function trace($log = '[think]', string $level = 'log')
524
    {
525
        if ('[think]' === $log) {
526
            return Log::getLog();
0 ignored issues
show
Bug introduced by
The method getLog() does not exist on think\facade\Log. 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

526
            return Log::/** @scrutinizer ignore-call */ getLog();
Loading history...
527
        }
528
529
        Log::record($log, $level);
0 ignored issues
show
Bug introduced by
The method record() does not exist on think\facade\Log. 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

529
        Log::/** @scrutinizer ignore-call */ 
530
             record($log, $level);
Loading history...
530
    }
531
}
532
533
if (!function_exists('trait_uses_recursive')) {
534
    /**
535
     * 获取一个trait里所有引用到的trait
536
     *
537
     * @param string $trait Trait
538
     * @return array
539
     */
540
    function trait_uses_recursive(string $trait): array
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...
541
    {
542
        $traits = class_uses($trait);
543
        foreach ($traits as $trait) {
544
            $traits += trait_uses_recursive($trait);
545
        }
546
547
        return $traits;
548
    }
549
}
550
551
if (!function_exists('url')) {
552
    /**
553
     * Url生成
554
     * @param string      $url    路由地址
555
     * @param array       $vars   变量
556
     * @param bool|string $suffix 生成的URL后缀
557
     * @param bool|string $domain 域名
558
     * @return UrlBuild
559
     */
560
    function url(string $url = '', array $vars = [], $suffix = true, $domain = false): UrlBuild
561
    {
562
        return Route::buildUrl($url, $vars)->suffix($suffix)->domain($domain);
0 ignored issues
show
Bug introduced by
The method buildUrl() does not exist on think\facade\Route. 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

562
        return Route::/** @scrutinizer ignore-call */ buildUrl($url, $vars)->suffix($suffix)->domain($domain);
Loading history...
563
    }
564
}
565
566
if (!function_exists('validate')) {
567
    /**
568
     * 生成验证对象
569
     * @param string|array $validate 验证器类名或者验证规则数组
570
     * @param array        $message  错误提示信息
571
     * @param bool         $batch    是否批量验证
572
     * @return Validate
573
     */
574
    function validate($validate = '', array $message = [], bool $batch = false): Validate
575
    {
576
        if (is_array($validate) || '' === $validate) {
577
            $v = new Validate();
578
            if (is_array($validate)) {
579
                $v->rule($validate);
580
            }
581
        } else {
582
            if (strpos($validate, '.')) {
583
                // 支持场景
584
                list($validate, $scene) = explode('.', $validate);
585
            }
586
587
            $class = false !== strpos($validate, '\\') ? $validate : app()->parseClass('validate', $validate);
588
589
            $v = new $class();
590
591
            if (!empty($scene)) {
592
                $v->scene($scene);
593
            }
594
        }
595
596
        return $v->message($message)->batch($batch)->failException(true);
597
    }
598
}
599
600
if (!function_exists('view')) {
601
    /**
602
     * 渲染模板输出
603
     * @param string   $template 模板文件
604
     * @param array    $vars     模板变量
605
     * @param int      $code     状态码
606
     * @param callable $filter   内容过滤
607
     * @return \think\response\View
608
     */
609
    function view(string $template = '', $vars = [], $code = 200, $filter = null): View
610
    {
611
        return Response::create($template, 'view', $code)->assign($vars)->filter($filter);
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

611
        return Response::create($template, 'view', $code)->/** @scrutinizer ignore-call */ assign($vars)->filter($filter);
Loading history...
612
    }
613
}
614
615
if (!function_exists('display')) {
616
    /**
617
     * 渲染模板输出
618
     * @param string   $content 渲染内容
619
     * @param array    $vars    模板变量
620
     * @param int      $code    状态码
621
     * @param callable $filter  内容过滤
622
     * @return \think\response\View
623
     */
624
    function display(string $content, $vars = [], $code = 200, $filter = null): View
625
    {
626
        return Response::create($content, 'view', $code)->isContent(true)->assign($vars)->filter($filter);
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

626
        return Response::create($content, 'view', $code)->/** @scrutinizer ignore-call */ isContent(true)->assign($vars)->filter($filter);
Loading history...
627
    }
628
}
629
630
if (!function_exists('xml')) {
631
    /**
632
     * 获取\think\response\Xml对象实例
633
     * @param mixed $data    返回的数据
634
     * @param int   $code    状态码
635
     * @param array $header  头部
636
     * @param array $options 参数
637
     * @return \think\response\Xml
638
     */
639
    function xml($data = [], $code = 200, $header = [], $options = []): Xml
640
    {
641
        return Response::create($data, 'xml', $code)->header($header)->options($options);
0 ignored issues
show
Bug Best Practice introduced by
The expression return think\Response::c...der)->options($options) returns the type think\Response which includes types incompatible with the type-hinted return think\response\Xml.
Loading history...
642
    }
643
}
644
645
if (!function_exists('app_path')) {
646
    /**
647
     * 获取当前应用目录
648
     *
649
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
650
     * @return string
651
     */
652
    function app_path($path = '')
0 ignored issues
show
Coding Style introduced by
Function name "app_path" is prefixed with a package name but does not begin with a capital letter
Loading history...
Coding Style introduced by
Function name "app_path" is invalid; consider "App_path" instead
Loading history...
653
    {
654
        return app()->getAppPath() . ($path ? $path . DIRECTORY_SEPARATOR : $path);
655
    }
656
}
657
658
if (!function_exists('base_path')) {
659
    /**
660
     * 获取应用基础目录
661
     *
662
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
663
     * @return string
664
     */
665
    function base_path($path = '')
0 ignored issues
show
Coding Style introduced by
Function name "base_path" is prefixed with a package name but does not begin with a capital letter
Loading history...
Coding Style introduced by
Function name "base_path" is invalid; consider "Base_path" instead
Loading history...
666
    {
667
        return app()->getBasePath() . ($path ? $path . DIRECTORY_SEPARATOR : $path);
668
    }
669
}
670
671
if (!function_exists('config_path')) {
672
    /**
673
     * 获取应用配置目录
674
     *
675
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
676
     * @return string
677
     */
678
    function config_path($path = '')
0 ignored issues
show
Coding Style introduced by
Function name "config_path" is prefixed with a package name but does not begin with a capital letter
Loading history...
Coding Style introduced by
Function name "config_path" is invalid; consider "Config_path" instead
Loading history...
679
    {
680
        return app()->getConfigPath() . ($path ? $path . DIRECTORY_SEPARATOR : $path);
681
    }
682
}
683
684
if (!function_exists('public_path')) {
685
    /**
686
     * 获取web根目录
687
     *
688
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
689
     * @return string
690
     */
691
    function public_path($path = '')
0 ignored issues
show
Coding Style introduced by
Function name "public_path" is prefixed with a package name but does not begin with a capital letter
Loading history...
Coding Style introduced by
Function name "public_path" is invalid; consider "Public_path" instead
Loading history...
692
    {
693
        return app()->getRootPath() . ($path ? ltrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR : $path);
694
    }
695
}
696
697
if (!function_exists('runtime_path')) {
698
    /**
699
     * 获取web根目录
700
     *
701
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
702
     * @return string
703
     */
704
    function runtime_path($path = '')
0 ignored issues
show
Coding Style introduced by
Function name "runtime_path" is prefixed with a package name but does not begin with a capital letter
Loading history...
Coding Style introduced by
Function name "runtime_path" is invalid; consider "Runtime_path" instead
Loading history...
705
    {
706
        return app()->getRuntimePath() . ($path ? $path . DIRECTORY_SEPARATOR : $path);
707
    }
708
}
709
710
if (!function_exists('root_path')) {
711
    /**
712
     * 获取项目根目录
713
     *
714
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
715
     * @return string
716
     */
717
    function root_path($path = '')
0 ignored issues
show
Coding Style introduced by
Function name "root_path" is prefixed with a package name but does not begin with a capital letter
Loading history...
Coding Style introduced by
Function name "root_path" is invalid; consider "Root_path" instead
Loading history...
718
    {
719
        return app()->getRootPath() . ($path ? $path . DIRECTORY_SEPARATOR : $path);
720
    }
721
}
722