Passed
Push — 5.1 ( 9596e8...d5bdc2 )
by liu
09:21
created

yaconf()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2018 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
12
//------------------------
13
// ThinkPHP 助手函数
14
//-------------------------
15
16
use think\Container;
0 ignored issues
show
Bug introduced by
The type think\Container was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use think\Db;
0 ignored issues
show
Bug introduced by
The type think\Db was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use think\exception\HttpException;
0 ignored issues
show
Bug introduced by
The type think\exception\HttpException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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...
19
use think\exception\HttpResponseException;
0 ignored issues
show
Bug introduced by
The type think\exception\HttpResponseException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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...
20
use think\facade\Cache;
0 ignored issues
show
Bug introduced by
The type think\facade\Cache was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use think\facade\Config;
0 ignored issues
show
Bug introduced by
The type think\facade\Config was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use think\facade\Cookie;
0 ignored issues
show
Bug introduced by
The type think\facade\Cookie was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use think\facade\Debug;
0 ignored issues
show
Bug introduced by
The type think\facade\Debug was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use think\facade\Env;
0 ignored issues
show
Bug introduced by
The type think\facade\Env was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use think\facade\Hook;
0 ignored issues
show
Bug introduced by
The type think\facade\Hook was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use think\facade\Lang;
0 ignored issues
show
Bug introduced by
The type think\facade\Lang was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use think\facade\Log;
0 ignored issues
show
Bug introduced by
The type think\facade\Log was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use think\facade\Request;
0 ignored issues
show
Bug introduced by
The type think\facade\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
use think\facade\Route;
0 ignored issues
show
Bug introduced by
The type think\facade\Route was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
use think\facade\Session;
0 ignored issues
show
Bug introduced by
The type think\facade\Session was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
use think\facade\Url;
0 ignored issues
show
Bug introduced by
The type think\facade\Url was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
use think\Response;
0 ignored issues
show
Bug introduced by
The type think\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
33
use think\route\RuleItem;
0 ignored issues
show
Bug introduced by
The type think\route\RuleItem was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
34
35
if (!function_exists('abort')) {
36
    /**
37
     * 抛出HTTP异常
38
     * @param integer|Response      $code 状态码 或者 Response对象实例
39
     * @param string                $message 错误信息
40
     * @param array                 $header 参数
41
     */
42
    function abort($code, $message = null, $header = [])
43
    {
44
        if ($code instanceof Response) {
45
            throw new HttpResponseException($code);
46
        } else {
47
            throw new HttpException($code, $message, null, $header);
48
        }
49
    }
50
}
51
52
if (!function_exists('action')) {
53
    /**
54
     * 调用模块的操作方法 参数格式 [模块/控制器/]操作
55
     * @param string        $url 调用地址
56
     * @param string|array  $vars 调用参数 支持字符串和数组
57
     * @param string        $layer 要调用的控制层名称
58
     * @param bool          $appendSuffix 是否添加类名后缀
59
     * @return mixed
60
     */
61
    function action($url, $vars = [], $layer = 'controller', $appendSuffix = false)
62
    {
63
        return app()->action($url, $vars, $layer, $appendSuffix);
64
    }
65
}
66
67
if (!function_exists('app')) {
68
    /**
69
     * 快速获取容器中的实例 支持依赖注入
70
     * @param string    $name 类名或标识 默认获取当前应用实例
71
     * @param array     $args 参数
72
     * @param bool      $newInstance    是否每次创建新的实例
73
     * @return mixed|\think\App
0 ignored issues
show
Bug introduced by
The type think\App was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
74
     */
75
    function app($name = 'think\App', $args = [], $newInstance = false)
76
    {
77
        return Container::get($name, $args, $newInstance);
78
    }
79
}
80
81
if (!function_exists('behavior')) {
82
    /**
83
     * 执行某个行为(run方法) 支持依赖注入
84
     * @param mixed $behavior   行为类名或者别名
85
     * @param mixed $args       参数
86
     * @return mixed
87
     */
88
    function behavior($behavior, $args = null)
89
    {
90
        return Hook::exec($behavior, $args);
91
    }
92
}
93
94
if (!function_exists('bind')) {
95
    /**
96
     * 绑定一个类到容器
97
     * @access public
98
     * @param string  $abstract    类标识、接口
99
     * @param mixed   $concrete    要绑定的类、闭包或者实例
100
     * @return Container
101
     */
102
    function bind($abstract, $concrete = null)
103
    {
104
        return Container::getInstance()->bindTo($abstract, $concrete);
105
    }
106
}
107
108
if (!function_exists('cache')) {
109
    /**
110
     * 缓存管理
111
     * @param mixed     $name 缓存名称,如果为数组表示进行缓存设置
112
     * @param mixed     $value 缓存值
113
     * @param mixed     $options 缓存参数
114
     * @param string    $tag 缓存标签
115
     * @return mixed
116
     */
117
    function cache($name, $value = '', $options = null, $tag = null)
118
    {
119
        if (is_array($options)) {
120
            // 缓存操作的同时初始化
121
            Cache::connect($options);
122
        } elseif (is_array($name)) {
123
            // 缓存初始化
124
            return Cache::connect($name);
125
        }
126
127
        if ('' === $value) {
128
            // 获取缓存
129
            return 0 === strpos($name, '?') ? Cache::has(substr($name, 1)) : Cache::get($name);
130
        } elseif (is_null($value)) {
131
            // 删除缓存
132
            return Cache::rm($name);
133
        } else {
134
            // 缓存数据
135
            if (is_array($options)) {
136
                $expire = isset($options['expire']) ? $options['expire'] : null; //修复查询缓存无法设置过期时间
137
            } else {
138
                $expire = is_numeric($options) ? $options : null; //默认快捷缓存设置过期时间
139
            }
140
141
            if (is_null($tag)) {
142
                return Cache::set($name, $value, $expire);
143
            } else {
144
                return Cache::tag($tag)->set($name, $value, $expire);
145
            }
146
        }
147
    }
148
}
149
150
if (!function_exists('call')) {
151
    /**
152
     * 调用反射执行callable 支持依赖注入
153
     * @param mixed $callable   支持闭包等callable写法
154
     * @param array $args       参数
155
     * @return mixed
156
     */
157
    function call($callable, $args = [])
158
    {
159
        return Container::getInstance()->invoke($callable, $args);
160
    }
161
}
162
163
if (!function_exists('class_basename')) {
164
    /**
165
     * 获取类名(不包含命名空间)
166
     *
167
     * @param  string|object $class
168
     * @return string
169
     */
170
    function class_basename($class)
171
    {
172
        $class = is_object($class) ? get_class($class) : $class;
173
        return basename(str_replace('\\', '/', $class));
174
    }
175
}
176
177
if (!function_exists('class_uses_recursive')) {
178
    /**
179
     *获取一个类里所有用到的trait,包括父类的
180
     *
181
     * @param $class
182
     * @return array
183
     */
184
    function class_uses_recursive($class)
185
    {
186
        if (is_object($class)) {
187
            $class = get_class($class);
188
        }
189
190
        $results = [];
191
        $classes = array_merge([$class => $class], class_parents($class));
192
        foreach ($classes as $class) {
0 ignored issues
show
introduced by
$class is overwriting one of the parameters of this function.
Loading history...
193
            $results += trait_uses_recursive($class);
194
        }
195
196
        return array_unique($results);
197
    }
198
}
199
200
if (!function_exists('config')) {
201
    /**
202
     * 获取和设置配置参数
203
     * @param string|array  $name 参数名
204
     * @param mixed         $value 参数值
205
     * @return mixed
206
     */
207
    function config($name = '', $value = null)
208
    {
209
        if (is_null($value) && is_string($name)) {
210
            if ('.' == substr($name, -1)) {
211
                return Config::pull(substr($name, 0, -1));
212
            }
213
214
            return 0 === strpos($name, '?') ? Config::has(substr($name, 1)) : Config::get($name);
215
        } else {
216
            return Config::set($name, $value);
217
        }
218
    }
219
}
220
221
if (!function_exists('container')) {
222
    /**
223
     * 获取容器对象实例
224
     * @return Container
225
     */
226
    function container()
227
    {
228
        return Container::getInstance();
229
    }
230
}
231
232
if (!function_exists('controller')) {
233
    /**
234
     * 实例化控制器 格式:[模块/]控制器
235
     * @param string    $name 资源地址
236
     * @param string    $layer 控制层名称
237
     * @param bool      $appendSuffix 是否添加类名后缀
238
     * @return \think\Controller
0 ignored issues
show
Bug introduced by
The type think\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
239
     */
240
    function controller($name, $layer = 'controller', $appendSuffix = false)
241
    {
242
        return app()->controller($name, $layer, $appendSuffix);
243
    }
244
}
245
246
if (!function_exists('cookie')) {
247
    /**
248
     * Cookie管理
249
     * @param string|array  $name cookie名称,如果为数组表示进行cookie设置
250
     * @param mixed         $value cookie值
251
     * @param mixed         $option 参数
252
     * @return mixed
253
     */
254
    function cookie($name, $value = '', $option = null)
255
    {
256
        if (is_array($name)) {
257
            // 初始化
258
            Cookie::init($name);
259
        } elseif (is_null($name)) {
0 ignored issues
show
introduced by
The condition is_null($name) is always false.
Loading history...
260
            // 清除
261
            Cookie::clear($value);
262
        } elseif ('' === $value) {
263
            // 获取
264
            return 0 === strpos($name, '?') ? Cookie::has(substr($name, 1), $option) : Cookie::get($name);
265
        } elseif (is_null($value)) {
266
            // 删除
267
            return Cookie::delete($name);
268
        } else {
269
            // 设置
270
            return Cookie::set($name, $value, $option);
271
        }
272
    }
273
}
274
275
if (!function_exists('db')) {
276
    /**
277
     * 实例化数据库类
278
     * @param string        $name 操作的数据表名称(不含前缀)
279
     * @param array|string  $config 数据库配置参数
280
     * @param bool          $force 是否强制重新连接
281
     * @return \think\db\Query
0 ignored issues
show
Bug introduced by
The type think\db\Query was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
282
     */
283
    function db($name = '', $config = [], $force = true)
284
    {
285
        return Db::connect($config, $force)->name($name);
286
    }
287
}
288
289
if (!function_exists('debug')) {
290
    /**
291
     * 记录时间(微秒)和内存使用情况
292
     * @param string            $start 开始标签
293
     * @param string            $end 结束标签
294
     * @param integer|string    $dec 小数位 如果是m 表示统计内存占用
295
     * @return mixed
296
     */
297
    function debug($start, $end = '', $dec = 6)
298
    {
299
        if ('' == $end) {
300
            Debug::remark($start);
301
        } else {
302
            return 'm' == $dec ? Debug::getRangeMem($start, $end) : Debug::getRangeTime($start, $end, $dec);
303
        }
304
    }
305
}
306
307
if (!function_exists('download')) {
308
    /**
309
     * 获取\think\response\Download对象实例
310
     * @param string  $filename 要下载的文件
311
     * @param string  $name 显示文件名
312
     * @param bool    $content 是否为内容
313
     * @param integer $expire 有效期(秒)
314
     * @return \think\response\Download
0 ignored issues
show
Bug introduced by
The type think\response\Download was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
315
     */
316
    function download($filename, $name = '', $content = false, $expire = 180)
317
    {
318
        return Response::create($filename, 'download')->name($name)->isContent($content)->expire($expire);
319
    }
320
}
321
322
if (!function_exists('dump')) {
323
    /**
324
     * 浏览器友好的变量输出
325
     * @param mixed     $var 变量
326
     * @param boolean   $echo 是否输出 默认为true 如果为false 则返回输出字符串
327
     * @param string    $label 标签 默认为空
328
     * @return void|string
329
     */
330
    function dump($var, $echo = true, $label = null)
331
    {
332
        return Debug::dump($var, $echo, $label);
333
    }
334
}
335
336
if (!function_exists('env')) {
337
    /**
338
     * 获取环境变量值
339
     * @access public
340
     * @param  string    $name 环境变量名(支持二级 .号分割)
341
     * @param  string    $default  默认值
342
     * @return mixed
343
     */
344
    function env($name = null, $default = null)
345
    {
346
        return Env::get($name, $default);
347
    }
348
}
349
350
if (!function_exists('exception')) {
351
    /**
352
     * 抛出异常处理
353
     *
354
     * @param string    $msg  异常消息
355
     * @param integer   $code 异常代码 默认为0
356
     * @param string    $exception 异常类
357
     *
358
     * @throws Exception
359
     */
360
    function exception($msg, $code = 0, $exception = '')
361
    {
362
        $e = $exception ?: '\think\Exception';
363
        throw new $e($msg, $code);
364
    }
365
}
366
367
if (!function_exists('halt')) {
368
    /**
369
     * 调试变量并且中断输出
370
     * @param mixed      $var 调试变量或者信息
371
     */
372
    function halt($var)
373
    {
374
        dump($var);
375
376
        throw new HttpResponseException(new Response);
377
    }
378
}
379
380
if (!function_exists('input')) {
381
    /**
382
     * 获取输入数据 支持默认值和过滤
383
     * @param string    $key 获取的变量名
384
     * @param mixed     $default 默认值
385
     * @param string    $filter 过滤方法
386
     * @return mixed
387
     */
388
    function input($key = '', $default = null, $filter = '')
389
    {
390
        if (0 === strpos($key, '?')) {
391
            $key = substr($key, 1);
392
            $has = true;
393
        }
394
395
        if ($pos = strpos($key, '.')) {
396
            // 指定参数来源
397
            $method = substr($key, 0, $pos);
398
            if (in_array($method, ['get', 'post', 'put', 'patch', 'delete', 'route', 'param', 'request', 'session', 'cookie', 'server', 'env', 'path', 'file'])) {
399
                $key = substr($key, $pos + 1);
400
            } else {
401
                $method = 'param';
402
            }
403
        } else {
404
            // 默认为自动判断
405
            $method = 'param';
406
        }
407
408
        if (isset($has)) {
409
            return request()->has($key, $method, $default);
410
        } else {
411
            return request()->$method($key, $default, $filter);
412
        }
413
    }
414
}
415
416
if (!function_exists('json')) {
417
    /**
418
     * 获取\think\response\Json对象实例
419
     * @param mixed   $data 返回的数据
420
     * @param integer $code 状态码
421
     * @param array   $header 头部
422
     * @param array   $options 参数
423
     * @return \think\response\Json
0 ignored issues
show
Bug introduced by
The type think\response\Json was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
424
     */
425
    function json($data = [], $code = 200, $header = [], $options = [])
426
    {
427
        return Response::create($data, 'json', $code, $header, $options);
428
    }
429
}
430
431
if (!function_exists('jsonp')) {
432
    /**
433
     * 获取\think\response\Jsonp对象实例
434
     * @param mixed   $data    返回的数据
435
     * @param integer $code    状态码
436
     * @param array   $header 头部
437
     * @param array   $options 参数
438
     * @return \think\response\Jsonp
0 ignored issues
show
Bug introduced by
The type think\response\Jsonp was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
439
     */
440
    function jsonp($data = [], $code = 200, $header = [], $options = [])
441
    {
442
        return Response::create($data, 'jsonp', $code, $header, $options);
443
    }
444
}
445
446
if (!function_exists('lang')) {
447
    /**
448
     * 获取语言变量值
449
     * @param string    $name 语言变量名
450
     * @param array     $vars 动态变量值
451
     * @param string    $lang 语言
452
     * @return mixed
453
     */
454
    function lang($name, $vars = [], $lang = '')
455
    {
456
        return Lang::get($name, $vars, $lang);
457
    }
458
}
459
460
if (!function_exists('model')) {
461
    /**
462
     * 实例化Model
463
     * @param string    $name Model名称
464
     * @param string    $layer 业务层名称
465
     * @param bool      $appendSuffix 是否添加类名后缀
466
     * @return \think\Model
0 ignored issues
show
Bug introduced by
The type think\Model was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
467
     */
468
    function model($name = '', $layer = 'model', $appendSuffix = false)
469
    {
470
        return app()->model($name, $layer, $appendSuffix);
471
    }
472
}
473
474
if (!function_exists('parse_name')) {
475
    /**
476
     * 字符串命名风格转换
477
     * type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格
478
     * @param string  $name 字符串
479
     * @param integer $type 转换类型
480
     * @param bool    $ucfirst 首字母是否大写(驼峰规则)
481
     * @return string
482
     */
483
    function parse_name($name, $type = 0, $ucfirst = true)
484
    {
485
        if ($type) {
486
            $name = preg_replace_callback('/_([a-zA-Z])/', function ($match) {
487
                return strtoupper($match[1]);
488
            }, $name);
489
490
            return $ucfirst ? ucfirst($name) : lcfirst($name);
491
        } else {
492
            return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
493
        }
494
    }
495
}
496
497
if (!function_exists('redirect')) {
498
    /**
499
     * 获取\think\response\Redirect对象实例
500
     * @param mixed         $url 重定向地址 支持Url::build方法的地址
501
     * @param array|integer $params 额外参数
502
     * @param integer       $code 状态码
503
     * @return \think\response\Redirect
0 ignored issues
show
Bug introduced by
The type think\response\Redirect was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
504
     */
505
    function redirect($url = [], $params = [], $code = 302)
506
    {
507
        if (is_integer($params)) {
508
            $code   = $params;
509
            $params = [];
510
        }
511
512
        return Response::create($url, 'redirect', $code)->params($params);
513
    }
514
}
515
516
if (!function_exists('request')) {
517
    /**
518
     * 获取当前Request对象实例
519
     * @return Request
520
     */
521
    function request()
522
    {
523
        return app('request');
524
    }
525
}
526
527
if (!function_exists('response')) {
528
    /**
529
     * 创建普通 Response 对象实例
530
     * @param mixed      $data   输出数据
531
     * @param int|string $code   状态码
532
     * @param array      $header 头信息
533
     * @param string     $type
534
     * @return Response
535
     */
536
    function response($data = [], $code = 200, $header = [], $type = 'html')
537
    {
538
        return Response::create($data, $type, $code, $header);
539
    }
540
}
541
542
if (!function_exists('route')) {
543
    /**
544
     * 路由注册
545
     * @param  string    $rule       路由规则
546
     * @param  mixed     $route      路由地址
547
     * @param  array     $option     路由参数
548
     * @param  array     $pattern    变量规则
549
     * @return RuleItem
550
     */
551
    function route($rule, $route, $option = [], $pattern = [])
552
    {
553
        return Route::rule($rule, $route, '*', $option, $pattern);
554
    }
555
}
556
557
if (!function_exists('session')) {
558
    /**
559
     * Session管理
560
     * @param string|array  $name session名称,如果为数组表示进行session设置
561
     * @param mixed         $value session值
562
     * @param string        $prefix 前缀
563
     * @return mixed
564
     */
565
    function session($name, $value = '', $prefix = null)
566
    {
567
        if (is_array($name)) {
568
            // 初始化
569
            Session::init($name);
570
        } elseif (is_null($name)) {
0 ignored issues
show
introduced by
The condition is_null($name) is always false.
Loading history...
571
            // 清除
572
            Session::clear($value);
573
        } elseif ('' === $value) {
574
            // 判断或获取
575
            return 0 === strpos($name, '?') ? Session::has(substr($name, 1), $prefix) : Session::get($name, $prefix);
576
        } elseif (is_null($value)) {
577
            // 删除
578
            return Session::delete($name, $prefix);
579
        } else {
580
            // 设置
581
            return Session::set($name, $value, $prefix);
582
        }
583
    }
584
}
585
586
if (!function_exists('token')) {
587
    /**
588
     * 生成表单令牌
589
     * @param string $name 令牌名称
590
     * @param mixed  $type 令牌生成方法
591
     * @return string
592
     */
593
    function token($name = '__token__', $type = 'md5')
594
    {
595
        $token = Request::token($name, $type);
596
597
        return '<input type="hidden" name="' . $name . '" value="' . $token . '" />';
598
    }
599
}
600
601
if (!function_exists('trace')) {
602
    /**
603
     * 记录日志信息
604
     * @param mixed     $log log信息 支持字符串和数组
605
     * @param string    $level 日志级别
606
     * @return array|void
607
     */
608
    function trace($log = '[think]', $level = 'log')
609
    {
610
        if ('[think]' === $log) {
611
            return Log::getLog();
612
        } else {
613
            Log::record($log, $level);
614
        }
615
    }
616
}
617
618
if (!function_exists('trait_uses_recursive')) {
619
    /**
620
     * 获取一个trait里所有引用到的trait
621
     *
622
     * @param  string $trait
623
     * @return array
624
     */
625
    function trait_uses_recursive($trait)
626
    {
627
        $traits = class_uses($trait);
628
        foreach ($traits as $trait) {
0 ignored issues
show
introduced by
$trait is overwriting one of the parameters of this function.
Loading history...
629
            $traits += trait_uses_recursive($trait);
630
        }
631
632
        return $traits;
633
    }
634
}
635
636
if (!function_exists('url')) {
637
    /**
638
     * Url生成
639
     * @param string        $url 路由地址
640
     * @param string|array  $vars 变量
641
     * @param bool|string   $suffix 生成的URL后缀
642
     * @param bool|string   $domain 域名
643
     * @return string
644
     */
645
    function url($url = '', $vars = '', $suffix = true, $domain = false)
646
    {
647
        return Url::build($url, $vars, $suffix, $domain);
648
    }
649
}
650
651
if (!function_exists('validate')) {
652
    /**
653
     * 实例化验证器
654
     * @param string    $name 验证器名称
655
     * @param string    $layer 业务层名称
656
     * @param bool      $appendSuffix 是否添加类名后缀
657
     * @return \think\Validate
0 ignored issues
show
Bug introduced by
The type think\Validate was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
658
     */
659
    function validate($name = '', $layer = 'validate', $appendSuffix = false)
660
    {
661
        return app()->validate($name, $layer, $appendSuffix);
662
    }
663
}
664
665
if (!function_exists('view')) {
666
    /**
667
     * 渲染模板输出
668
     * @param string    $template 模板文件
669
     * @param array     $vars 模板变量
670
     * @param integer   $code 状态码
671
     * @param callable  $filter 内容过滤
672
     * @return \think\response\View
0 ignored issues
show
Bug introduced by
The type think\response\View was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
673
     */
674
    function view($template = '', $vars = [], $code = 200, $filter = null)
675
    {
676
        return Response::create($template, 'view', $code)->assign($vars)->filter($filter);
677
    }
678
}
679
680
if (!function_exists('widget')) {
681
    /**
682
     * 渲染输出Widget
683
     * @param string    $name Widget名称
684
     * @param array     $data 传入的参数
685
     * @return mixed
686
     */
687
    function widget($name, $data = [])
688
    {
689
        return app()->action($name, $data, 'widget');
690
    }
691
}
692
693
if (!function_exists('xml')) {
694
    /**
695
     * 获取\think\response\Xml对象实例
696
     * @param mixed   $data    返回的数据
697
     * @param integer $code    状态码
698
     * @param array   $header  头部
699
     * @param array   $options 参数
700
     * @return \think\response\Xml
0 ignored issues
show
Bug introduced by
The type think\response\Xml was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
701
     */
702
    function xml($data = [], $code = 200, $header = [], $options = [])
703
    {
704
        return Response::create($data, 'xml', $code, $header, $options);
705
    }
706
}
707
708
if (!function_exists('yaconf')) {
709
    /**
710
     * 获取yaconf配置
711
     *
712
     * @param  string    $name 配置参数名
713
     * @param  mixed     $default   默认值
714
     * @return mixed
715
     */
716
    function yaconf($name, $default = null)
717
    {
718
        return Config::yaconf($name, $default);
719
    }
720
}
721