Completed
Pull Request — 6.0 (#2351)
by
unknown
11:09
created

cookie()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 4
nop 3
dl 0
loc 11
rs 10
c 0
b 0
f 0
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
     */
48
    function abort($code, string $message = '', 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);
103
        } elseif (is_null($value)) {
104
            // 删除缓存
105
            return Cache::delete($name);
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);
117
        } else {
118
            return Cache::tag($tag)->set($name, $value, $expire);
119
        }
120
    }
121
}
122
123
if (!function_exists('config')) {
124
    /**
125
     * 获取和设置配置参数
126
     * @param string|array $name  参数名
127
     * @param mixed        $value 参数值
128
     * @return mixed
129
     */
130
    function config($name = '', $value = null)
131
    {
132
        if (is_array($name)) {
133
            return Config::set($name, $value);
134
        }
135
136
        return 0 === strpos($name, '?') ? Config::has(substr($name, 1)) : Config::get($name, $value);
137
    }
138
}
139
140
if (!function_exists('cookie')) {
141
    /**
142
     * Cookie管理
143
     * @param string $name   cookie名称
144
     * @param mixed  $value  cookie值
145
     * @param mixed  $option 参数
146
     * @return mixed
147
     */
148
    function cookie(string $name, $value = '', $option = null)
149
    {
150
        if (is_null($value)) {
151
            // 删除
152
            Cookie::delete($name);
153
        } elseif ('' === $value) {
154
            // 获取
155
            return 0 === strpos($name, '?') ? Cookie::has(substr($name, 1)) : Cookie::get($name);
156
        } else {
157
            // 设置
158
            return Cookie::set($name, $value, $option);
0 ignored issues
show
Bug introduced by
Are you sure the usage of think\facade\Cookie::set($name, $value, $option) targeting think\facade\Cookie::set() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
159
        }
160
    }
161
}
162
163
if (!function_exists('download')) {
164
    /**
165
     * 获取\think\response\Download对象实例
166
     * @param string $filename 要下载的文件
167
     * @param string $name     显示文件名
168
     * @param bool   $content  是否为内容
169
     * @param int    $expire   有效期(秒)
170
     * @return \think\response\File
171
     */
172
    function download(string $filename, string $name = '', bool $content = false, int $expire = 180): File
173
    {
174
        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

174
        return Response::create($filename, 'file')->/** @scrutinizer ignore-call */ name($name)->isContent($content)->expire($expire);
Loading history...
175
    }
176
}
177
178
if (!function_exists('dump')) {
179
    /**
180
     * 浏览器友好的变量输出
181
     * @param mixed $vars 要输出的变量
182
     * @return void
183
     */
184
    function dump(...$vars)
185
    {
186
        ob_start();
187
        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...
188
189
        $output = ob_get_clean();
190
        $output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output);
191
192
        if (PHP_SAPI == 'cli') {
193
            $output = PHP_EOL . $output . PHP_EOL;
194
        } else {
195
            if (!extension_loaded('xdebug')) {
196
                $output = htmlspecialchars($output, ENT_SUBSTITUTE);
197
            }
198
            $output = '<pre>' . $output . '</pre>';
199
        }
200
201
        echo $output;
202
    }
203
}
204
205
if (!function_exists('env')) {
206
    /**
207
     * 获取环境变量值
208
     * @access public
209
     * @param string $name    环境变量名(支持二级 .号分割)
210
     * @param string $default 默认值
211
     * @return mixed
212
     */
213
    function env(string $name = null, $default = null)
214
    {
215
        return Env::get($name, $default);
216
    }
217
}
218
219
if (!function_exists('event')) {
220
    /**
221
     * 触发事件
222
     * @param mixed $event 事件名(或者类名)
223
     * @param mixed $args  参数
224
     * @return mixed
225
     */
226
    function event($event, $args = null)
227
    {
228
        return Event::trigger($event, $args);
229
    }
230
}
231
232
if (!function_exists('halt')) {
233
    /**
234
     * 调试变量并且中断输出
235
     * @param mixed $vars 调试变量或者信息
236
     */
237
    function halt(...$vars)
238
    {
239
        dump(...$vars);
240
241
        throw new HttpResponseException(Response::create());
242
    }
243
}
244
245
if (!function_exists('input')) {
246
    /**
247
     * 获取输入数据 支持默认值和过滤
248
     * @param string $key     获取的变量名
249
     * @param mixed  $default 默认值
250
     * @param string $filter  过滤方法
251
     * @return mixed
252
     */
253
    function input(string $key = '', $default = null, $filter = '')
254
    {
255
        if (0 === strpos($key, '?')) {
256
            $key = substr($key, 1);
257
            $has = true;
258
        }
259
260
        if ($pos = strpos($key, '.')) {
261
            // 指定参数来源
262
            $method = substr($key, 0, $pos);
263
            if (in_array($method, ['get', 'post', 'put', 'patch', 'delete', 'route', 'param', 'request', 'session', 'cookie', 'server', 'env', 'path', 'file'])) {
264
                $key = substr($key, $pos + 1);
265
                if ('server' == $method && is_null($default)) {
266
                    $default = '';
267
                }
268
            } else {
269
                $method = 'param';
270
            }
271
        } else {
272
            // 默认为自动判断
273
            $method = 'param';
274
        }
275
276
        return isset($has) ?
277
        request()->has($key, $method) :
278
        request()->$method($key, $default, $filter);
279
    }
280
}
281
282
if (!function_exists('invoke')) {
283
    /**
284
     * 调用反射实例化对象或者执行方法 支持依赖注入
285
     * @param mixed $call 类名或者callable
286
     * @param array $args 参数
287
     * @return mixed
288
     */
289
    function invoke($call, array $args = [])
290
    {
291
        if (is_callable($call)) {
292
            return Container::getInstance()->invoke($call, $args);
293
        }
294
295
        return Container::getInstance()->invokeClass($call, $args);
296
    }
297
}
298
299
if (!function_exists('json')) {
300
    /**
301
     * 获取\think\response\Json对象实例
302
     * @param mixed $data    返回的数据
303
     * @param int   $code    状态码
304
     * @param array $header  头部
305
     * @param array $options 参数
306
     * @return \think\response\Json
307
     */
308
    function json($data = [], $code = 200, $header = [], $options = []): Json
309
    {
310
        return Response::create($data, 'json', $code)->header($header)->options($options);
311
    }
312
}
313
314
if (!function_exists('jsonp')) {
315
    /**
316
     * 获取\think\response\Jsonp对象实例
317
     * @param mixed $data    返回的数据
318
     * @param int   $code    状态码
319
     * @param array $header  头部
320
     * @param array $options 参数
321
     * @return \think\response\Jsonp
322
     */
323
    function jsonp($data = [], $code = 200, $header = [], $options = []): Jsonp
324
    {
325
        return Response::create($data, 'jsonp', $code)->header($header)->options($options);
326
    }
327
}
328
329
if (!function_exists('lang')) {
330
    /**
331
     * 获取语言变量值
332
     * @param string $name 语言变量名
333
     * @param array  $vars 动态变量值
334
     * @param string $lang 语言
335
     * @return mixed
336
     */
337
    function lang(string $name, array $vars = [], string $lang = '')
338
    {
339
        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

339
        return Lang::/** @scrutinizer ignore-call */ get($name, $vars, $lang);
Loading history...
340
    }
341
}
342
343
if (!function_exists('parse_name')) {
344
    /**
345
     * 字符串命名风格转换
346
     * type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格
347
     * @param string $name    字符串
348
     * @param int    $type    转换类型
349
     * @param bool   $ucfirst 首字母是否大写(驼峰规则)
350
     * @return string
351
     */
352
    function parse_name(string $name, int $type = 0, bool $ucfirst = true): string
353
    {
354
        if ($type) {
355
            $name = preg_replace_callback('/_([a-zA-Z])/', function ($match) {
356
                return strtoupper($match[1]);
357
            }, $name);
358
359
            return $ucfirst ? ucfirst($name) : lcfirst($name);
360
        }
361
362
        return strtolower(trim(preg_replace('/[A-Z]/', '_\\0', $name), '_'));
363
    }
364
}
365
366
if (!function_exists('redirect')) {
367
    /**
368
     * 获取\think\response\Redirect对象实例
369
     * @param string $url  重定向地址
370
     * @param int    $code 状态码
371
     * @return \think\response\Redirect
372
     */
373
    function redirect(string $url = '', int $code = 302): Redirect
374
    {
375
        return Response::create($url, 'redirect', $code);
376
    }
377
}
378
379
if (!function_exists('request')) {
380
    /**
381
     * 获取当前Request对象实例
382
     * @return Request
383
     */
384
    function request(): \think\Request
385
    {
386
        return app('request');
387
    }
388
}
389
390
if (!function_exists('response')) {
391
    /**
392
     * 创建普通 Response 对象实例
393
     * @param mixed      $data   输出数据
394
     * @param int|string $code   状态码
395
     * @param array      $header 头信息
396
     * @param string     $type
397
     * @return Response
398
     */
399
    function response($data = '', $code = 200, $header = [], $type = 'html'): Response
400
    {
401
        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

401
        return Response::create($data, $type, /** @scrutinizer ignore-type */ $code)->header($header);
Loading history...
402
    }
403
}
404
405
if (!function_exists('session')) {
406
    /**
407
     * Session管理
408
     * @param string $name  session名称
409
     * @param mixed  $value session值
410
     * @return mixed
411
     */
412
    function session($name = '', $value = '')
413
    {
414
        if (is_null($name)) {
0 ignored issues
show
introduced by
The condition is_null($name) is always false.
Loading history...
415
            // 清除
416
            Session::clear();
417
        } elseif ('' === $name) {
418
            return Session::all();
0 ignored issues
show
Bug introduced by
The method all() 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

418
            return Session::/** @scrutinizer ignore-call */ all();
Loading history...
419
        } elseif (is_null($value)) {
420
            // 删除
421
            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

421
            Session::/** @scrutinizer ignore-call */ 
422
                     delete($name);
Loading history...
422
        } elseif ('' === $value) {
423
            // 判断或获取
424
            return 0 === strpos($name, '?') ? Session::has(substr($name, 1)) : Session::get($name);
0 ignored issues
show
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

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

424
            return 0 === strpos($name, '?') ? Session::has(substr($name, 1)) : Session::/** @scrutinizer ignore-call */ get($name);
Loading history...
425
        } else {
426
            // 设置
427
            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

427
            Session::/** @scrutinizer ignore-call */ 
428
                     set($name, $value);
Loading history...
428
        }
429
    }
430
}
431
432
if (!function_exists('token')) {
433
    /**
434
     * 获取Token令牌
435
     * @param string $name 令牌名称
436
     * @param mixed  $type 令牌生成方法
437
     * @return string
438
     */
439
    function token(string $name = '__token__', string $type = 'md5'): string
440
    {
441
        return Request::buildToken($name, $type);
442
    }
443
}
444
445
if (!function_exists('token_field')) {
446
    /**
447
     * 生成令牌隐藏表单
448
     * @param string $name 令牌名称
449
     * @param mixed  $type 令牌生成方法
450
     * @return string
451
     */
452
    function token_field(string $name = '__token__', string $type = 'md5'): string
453
    {
454
        $token = Request::buildToken($name, $type);
455
456
        return '<input type="hidden" name="' . $name . '" value="' . $token . '" />';
457
    }
458
}
459
460
if (!function_exists('token_meta')) {
461
    /**
462
     * 生成令牌meta
463
     * @param string $name 令牌名称
464
     * @param mixed  $type 令牌生成方法
465
     * @return string
466
     */
467
    function token_meta(string $name = '__token__', string $type = 'md5'): string
468
    {
469
        $token = Request::buildToken($name, $type);
470
471
        return '<meta name="csrf-token" content="' . $token . '">';
472
    }
473
}
474
475
if (!function_exists('trace')) {
476
    /**
477
     * 记录日志信息
478
     * @param mixed  $log   log信息 支持字符串和数组
479
     * @param string $level 日志级别
480
     * @return array|void
481
     */
482
    function trace($log = '[think]', string $level = 'log')
483
    {
484
        if ('[think]' === $log) {
485
            return Log::getLog();
486
        }
487
488
        Log::record($log, $level);
489
    }
490
}
491
492
if (!function_exists('url')) {
493
    /**
494
     * Url生成
495
     * @param string      $url    路由地址
496
     * @param array       $vars   变量
497
     * @param bool|string $suffix 生成的URL后缀
498
     * @param bool|string $domain 域名
499
     * @return UrlBuild
500
     */
501
    function url(string $url = '', array $vars = [], $suffix = true, $domain = false): UrlBuild
502
    {
503
        return Route::buildUrl($url, $vars)->suffix($suffix)->domain($domain);
504
    }
505
}
506
507
if (!function_exists('validate')) {
508
    /**
509
     * 生成验证对象
510
     * @param string|array $validate      验证器类名或者验证规则数组
511
     * @param array        $message       错误提示信息
512
     * @param bool         $batch         是否批量验证
513
     * @param bool         $failException 是否抛出异常
514
     * @return Validate
515
     */
516
    function validate($validate = '', array $message = [], bool $batch = false, bool $failException = true): Validate
517
    {
518
        if (is_array($validate) || '' === $validate) {
519
            $v = new Validate();
520
            if (is_array($validate)) {
521
                $v->rule($validate);
522
            }
523
        } else {
524
            if (strpos($validate, '.')) {
525
                // 支持场景
526
                [$validate, $scene] = explode('.', $validate);
527
            }
528
529
            $class = false !== strpos($validate, '\\') ? $validate : app()->parseClass('validate', $validate);
530
531
            $v = new $class();
532
533
            if (!empty($scene)) {
534
                $v->scene($scene);
535
            }
536
        }
537
538
        return $v->message($message)->batch($batch)->failException($failException);
539
    }
540
}
541
542
if (!function_exists('view')) {
543
    /**
544
     * 渲染模板输出
545
     * @param string   $template 模板文件
546
     * @param array    $vars     模板变量
547
     * @param int      $code     状态码
548
     * @param callable $filter   内容过滤
549
     * @return \think\response\View
550
     */
551
    function view(string $template = '', $vars = [], $code = 200, $filter = null): View
552
    {
553
        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

553
        return Response::create($template, 'view', $code)->/** @scrutinizer ignore-call */ assign($vars)->filter($filter);
Loading history...
554
    }
555
}
556
557
if (!function_exists('display')) {
558
    /**
559
     * 渲染模板输出
560
     * @param string   $content 渲染内容
561
     * @param array    $vars    模板变量
562
     * @param int      $code    状态码
563
     * @param callable $filter  内容过滤
564
     * @return \think\response\View
565
     */
566
    function display(string $content, $vars = [], $code = 200, $filter = null): View
567
    {
568
        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

568
        return Response::create($content, 'view', $code)->/** @scrutinizer ignore-call */ isContent(true)->assign($vars)->filter($filter);
Loading history...
569
    }
570
}
571
572
if (!function_exists('xml')) {
573
    /**
574
     * 获取\think\response\Xml对象实例
575
     * @param mixed $data    返回的数据
576
     * @param int   $code    状态码
577
     * @param array $header  头部
578
     * @param array $options 参数
579
     * @return \think\response\Xml
580
     */
581
    function xml($data = [], $code = 200, $header = [], $options = []): Xml
582
    {
583
        return Response::create($data, 'xml', $code)->header($header)->options($options);
584
    }
585
}
586
587
if (!function_exists('app_path')) {
588
    /**
589
     * 获取当前应用目录
590
     *
591
     * @param string $path
592
     * @return string
593
     */
594
    function app_path($path = '')
595
    {
596
        return app()->getAppPath() . ($path ? $path . DIRECTORY_SEPARATOR : $path);
597
    }
598
}
599
600
if (!function_exists('base_path')) {
601
    /**
602
     * 获取应用基础目录
603
     *
604
     * @param string $path
605
     * @return string
606
     */
607
    function base_path($path = '')
608
    {
609
        return app()->getBasePath() . ($path ? $path . DIRECTORY_SEPARATOR : $path);
610
    }
611
}
612
613
if (!function_exists('config_path')) {
614
    /**
615
     * 获取应用配置目录
616
     *
617
     * @param string $path
618
     * @return string
619
     */
620
    function config_path($path = '')
621
    {
622
        return app()->getConfigPath() . ($path ? $path . DIRECTORY_SEPARATOR : $path);
623
    }
624
}
625
626
if (!function_exists('public_path')) {
627
    /**
628
     * 获取web根目录
629
     *
630
     * @param string $path
631
     * @return string
632
     */
633
    function public_path($path = '')
634
    {
635
        return app()->getRootPath() . 'public' . DIRECTORY_SEPARATOR . ($path ? ltrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR : $path);
636
    }
637
}
638
639
if (!function_exists('runtime_path')) {
640
    /**
641
     * 获取应用运行时目录
642
     *
643
     * @param string $path
644
     * @return string
645
     */
646
    function runtime_path($path = '')
647
    {
648
        return app()->getRuntimePath() . ($path ? $path . DIRECTORY_SEPARATOR : $path);
649
    }
650
}
651
652
if (!function_exists('root_path')) {
653
    /**
654
     * 获取项目根目录
655
     *
656
     * @param string $path
657
     * @return string
658
     */
659
    function root_path($path = '')
660
    {
661
        return app()->getRootPath() . ($path ? $path . DIRECTORY_SEPARATOR : $path);
662
    }
663
}
664