Passed
Pull Request — 5.1 (#1327)
by
unknown
08:41 queued 03:50
created

trace_service()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 16
rs 9.9666
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 $common       公共模块名,默认core
465
     * @param string    $layer 模型层名称
466
467
     * @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...
468
     */
469
    function model($name = '', $common = 'core', $layer = 'model', $appendSuffix = false)
470
    {
471
        return app()->model($name, $layer, $appendSuffix, $common);
472
    }
473
}
474
475
if (!function_exists('logic')) {
476
    /**
477
     * 实例化Logic
478
     * @param string    $name Logic名称
479
     * @param  string $common       公共模块名,默认core
480
     * @param string    $layer 业务层名称
481
482
     * @return mixed
483
     */
484
    function logic($name = '', $common = 'core', $layer = 'logic', $appendSuffix = true)
485
    {
486
        return app()->logic($name, $layer, $appendSuffix, $common);
487
    }
488
}
489
490
if (!function_exists('service')) {
491
    /**
492
     * 实例化Service
493
     * @param string    $name Service名称
494
     * @param  string $common       公共模块名,默认core
495
     * @param string    $layer 服务层名称
496
497
     * @return mixed
498
     */
499
    function service($name = '', $common = 'core', $layer = 'service', $appendSuffix = true)
500
    {
501
        return app()->service($name, $layer, $appendSuffix, $common);
502
    }
503
}
504
505
506
if (!function_exists('parse_name')) {
507
    /**
508
     * 字符串命名风格转换
509
     * type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格
510
     * @param string  $name 字符串
511
     * @param integer $type 转换类型
512
     * @param bool    $ucfirst 首字母是否大写(驼峰规则)
513
     * @return string
514
     */
515
    function parse_name($name, $type = 0, $ucfirst = true)
516
    {
517
        if ($type) {
518
            $name = preg_replace_callback('/_([a-zA-Z])/', function ($match) {
519
                return strtoupper($match[1]);
520
            }, $name);
521
522
            return $ucfirst ? ucfirst($name) : lcfirst($name);
523
        } else {
524
            return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
525
        }
526
    }
527
}
528
529
if (!function_exists('redirect')) {
530
    /**
531
     * 获取\think\response\Redirect对象实例
532
     * @param mixed         $url 重定向地址 支持Url::build方法的地址
533
     * @param array|integer $params 额外参数
534
     * @param integer       $code 状态码
535
     * @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...
536
     */
537
    function redirect($url = [], $params = [], $code = 302)
538
    {
539
        if (is_integer($params)) {
540
            $code   = $params;
541
            $params = [];
542
        }
543
544
        return Response::create($url, 'redirect', $code)->params($params);
545
    }
546
}
547
548
if (!function_exists('request')) {
549
    /**
550
     * 获取当前Request对象实例
551
     * @return Request
552
     */
553
    function request()
554
    {
555
        return app('request');
556
    }
557
}
558
559
if (!function_exists('response')) {
560
    /**
561
     * 创建普通 Response 对象实例
562
     * @param mixed      $data   输出数据
563
     * @param int|string $code   状态码
564
     * @param array      $header 头信息
565
     * @param string     $type
566
     * @return Response
567
     */
568
    function response($data = [], $code = 200, $header = [], $type = 'html')
569
    {
570
        return Response::create($data, $type, $code, $header);
571
    }
572
}
573
574
if (!function_exists('route')) {
575
    /**
576
     * 路由注册
577
     * @param  string    $rule       路由规则
578
     * @param  mixed     $route      路由地址
579
     * @param  array     $option     路由参数
580
     * @param  array     $pattern    变量规则
581
     * @return RuleItem
582
     */
583
    function route($rule, $route, $option = [], $pattern = [])
584
    {
585
        return Route::rule($rule, $route, '*', $option, $pattern);
586
    }
587
}
588
589
if (!function_exists('session')) {
590
    /**
591
     * Session管理
592
     * @param string|array  $name session名称,如果为数组表示进行session设置
593
     * @param mixed         $value session值
594
     * @param string        $prefix 前缀
595
     * @return mixed
596
     */
597
    function session($name, $value = '', $prefix = null)
598
    {
599
        if (is_array($name)) {
600
            // 初始化
601
            Session::init($name);
602
        } elseif (is_null($name)) {
0 ignored issues
show
introduced by
The condition is_null($name) is always false.
Loading history...
603
            // 清除
604
            Session::clear($value);
605
        } elseif ('' === $value) {
606
            // 判断或获取
607
            return 0 === strpos($name, '?') ? Session::has(substr($name, 1), $prefix) : Session::get($name, $prefix);
608
        } elseif (is_null($value)) {
609
            // 删除
610
            return Session::delete($name, $prefix);
611
        } else {
612
            // 设置
613
            return Session::set($name, $value, $prefix);
614
        }
615
    }
616
}
617
618
if (!function_exists('token')) {
619
    /**
620
     * 生成表单令牌
621
     * @param string $name 令牌名称
622
     * @param mixed  $type 令牌生成方法
623
     * @return string
624
     */
625
    function token($name = '__token__', $type = 'md5')
626
    {
627
        $token = Request::token($name, $type);
628
629
        return '<input type="hidden" name="' . $name . '" value="' . $token . '" />';
630
    }
631
}
632
633
if (!function_exists('trace')) {
634
    /**
635
     * 记录日志信息
636
     * @param mixed     $log log信息 支持字符串和数组
637
     * @param string    $level 日志级别
638
     * @return array|void
639
     */
640
    function trace($log = '[think]', $level = 'log')
641
    {
642
        if ('[think]' === $log) {
643
            return Log::getLog();
644
        } else {
645
            Log::record($log, $level);
646
        }
647
    }
648
}
649
650
if (!function_exists('trait_uses_recursive')) {
651
    /**
652
     * 获取一个trait里所有引用到的trait
653
     *
654
     * @param  string $trait
655
     * @return array
656
     */
657
    function trait_uses_recursive($trait)
658
    {
659
        $traits = class_uses($trait);
660
        foreach ($traits as $trait) {
0 ignored issues
show
introduced by
$trait is overwriting one of the parameters of this function.
Loading history...
661
            $traits += trait_uses_recursive($trait);
662
        }
663
664
        return $traits;
665
    }
666
}
667
668
if (!function_exists('url')) {
669
    /**
670
     * Url生成
671
     * @param string        $url 路由地址
672
     * @param string|array  $vars 变量
673
     * @param bool|string   $suffix 生成的URL后缀
674
     * @param bool|string   $domain 域名
675
     * @return string
676
     */
677
    function url($url = '', $vars = '', $suffix = true, $domain = false)
678
    {
679
        return Url::build($url, $vars, $suffix, $domain);
680
    }
681
}
682
683
if (!function_exists('validate')) {
684
    /**
685
     * 实例化验证器
686
     * @param string    $name 验证器名称
687
     * @param  string $common       公共模块名,默认core
688
     * @param string    $layer 业务层名称
689
     * @param bool      $appendSuffix 是否添加类名后缀
690
     * @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...
691
     */
692
    function validate($name = '', $common = 'core', $layer = 'validate', $appendSuffix = true)
693
    {
694
        return app()->validate($name, $layer, $appendSuffix, $common);
695
    }
696
}
697
698
if (!function_exists('view')) {
699
    /**
700
     * 渲染模板输出
701
     * @param string    $template 模板文件
702
     * @param array     $vars 模板变量
703
     * @param integer   $code 状态码
704
     * @param callable  $filter 内容过滤
705
     * @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...
706
     */
707
    function view($template = '', $vars = [], $code = 200, $filter = null)
708
    {
709
        return Response::create($template, 'view', $code)->assign($vars)->filter($filter);
710
    }
711
}
712
713
if (!function_exists('widget')) {
714
    /**
715
     * 渲染输出Widget
716
     * @param string    $name Widget名称
717
     * @param array     $data 传入的参数
718
     * @return mixed
719
     */
720
    function widget($name, $data = [])
721
    {
722
        return app()->action($name, $data, 'widget');
723
    }
724
}
725
726
if (!function_exists('xml')) {
727
    /**
728
     * 获取\think\response\Xml对象实例
729
     * @param mixed   $data    返回的数据
730
     * @param integer $code    状态码
731
     * @param array   $header  头部
732
     * @param array   $options 参数
733
     * @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...
734
     */
735
    function xml($data = [], $code = 200, $header = [], $options = [])
736
    {
737
        return Response::create($data, 'xml', $code, $header, $options);
738
    }
739
}
740
741
if (!function_exists('yaconf')) {
742
    /**
743
     * 获取yaconf配置
744
     *
745
     * @param  string    $name 配置参数名
746
     * @param  mixed     $default   默认值
747
     * @return mixed
748
     */
749
    function yaconf($name, $default = null)
750
    {
751
        return Config::yaconf($name, $default);
752
    }
753
}
754
755
if (!function_exists('is_json')) {
756
    /**
757
     * 判断字符串是不是json格式
758
     * @param $str
759
     * @return bool
760
     */
761
    function is_json($str)
762
    {
763
        json_decode($str);
764
765
        return (json_last_error() == JSON_ERROR_NONE);
766
    }
767
}
768
769
if (!function_exists('trace_service')) {
770
    function trace_service($trace,  $type = 'info', $debug = false)
771
    {
772
        // 追踪代码信息,给函数埋雷,检测业务代码信息
773
        if(!$debug){
774
            $trace = [
775
                'trace' => [
776
                    'class' => $trace[0]['class'],
777
                    'file' => $trace[0]['file'],
778
                    'function' => $trace[0]['function'],
779
                    'line' => $trace[0]['line'],
780
                    'args' => $trace[0]['args']
781
                ]
782
            ];
783
        }
784
785
        Log::record(json_encode($trace, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE), $type);
786
    }
787
}