727 lines
20 KiB
PHP
Executable File
727 lines
20 KiB
PHP
Executable File
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||
// +----------------------------------------------------------------------
|
||
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
|
||
// +----------------------------------------------------------------------
|
||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||
// +----------------------------------------------------------------------
|
||
// | Author: liu21st <liu21st@gmail.com>
|
||
// +----------------------------------------------------------------------
|
||
|
||
//------------------------
|
||
// ThinkPHP 助手函数
|
||
//-------------------------
|
||
|
||
use think\Container;
|
||
use think\Db;
|
||
use think\exception\HttpException;
|
||
use think\exception\HttpResponseException;
|
||
use think\facade\Cache;
|
||
use think\facade\Config;
|
||
use think\facade\Cookie;
|
||
use think\facade\Debug;
|
||
use think\facade\Env;
|
||
use think\facade\Hook;
|
||
use think\facade\Lang;
|
||
use think\facade\Log;
|
||
use think\facade\Request;
|
||
use think\facade\Route;
|
||
use think\facade\Session;
|
||
use think\facade\Url;
|
||
use think\Response;
|
||
use think\route\RuleItem;
|
||
|
||
if (!function_exists('abort')) {
|
||
/**
|
||
* 抛出HTTP异常
|
||
* @param integer|Response $code 状态码 或者 Response对象实例
|
||
* @param string $message 错误信息
|
||
* @param array $header 参数
|
||
*/
|
||
function abort($code, $message = null, $header = [])
|
||
{
|
||
if ($code instanceof Response) {
|
||
throw new HttpResponseException($code);
|
||
} else {
|
||
throw new HttpException($code, $message, null, $header);
|
||
}
|
||
}
|
||
}
|
||
|
||
if (!function_exists('action')) {
|
||
/**
|
||
* 调用模块的操作方法 参数格式 [模块/控制器/]操作
|
||
* @param string $url 调用地址
|
||
* @param string|array $vars 调用参数 支持字符串和数组
|
||
* @param string $layer 要调用的控制层名称
|
||
* @param bool $appendSuffix 是否添加类名后缀
|
||
* @return mixed
|
||
*/
|
||
function action($url, $vars = [], $layer = 'controller', $appendSuffix = false)
|
||
{
|
||
return app()->action($url, $vars, $layer, $appendSuffix);
|
||
}
|
||
}
|
||
|
||
if (!function_exists('app')) {
|
||
/**
|
||
* 快速获取容器中的实例 支持依赖注入
|
||
* @param string $name 类名或标识 默认获取当前应用实例
|
||
* @param array $args 参数
|
||
* @param bool $newInstance 是否每次创建新的实例
|
||
* @return mixed|\think\App
|
||
*/
|
||
function app($name = 'think\App', $args = [], $newInstance = false)
|
||
{
|
||
return Container::get($name, $args, $newInstance);
|
||
}
|
||
}
|
||
|
||
if (!function_exists('behavior')) {
|
||
/**
|
||
* 执行某个行为(run方法) 支持依赖注入
|
||
* @param mixed $behavior 行为类名或者别名
|
||
* @param mixed $args 参数
|
||
* @return mixed
|
||
*/
|
||
function behavior($behavior, $args = null)
|
||
{
|
||
return Hook::exec($behavior, $args);
|
||
}
|
||
}
|
||
|
||
if (!function_exists('bind')) {
|
||
/**
|
||
* 绑定一个类到容器
|
||
* @access public
|
||
* @param string $abstract 类标识、接口
|
||
* @param mixed $concrete 要绑定的类、闭包或者实例
|
||
* @return Container
|
||
*/
|
||
function bind($abstract, $concrete = null)
|
||
{
|
||
return Container::getInstance()->bindTo($abstract, $concrete);
|
||
}
|
||
}
|
||
|
||
if (!function_exists('cache')) {
|
||
/**
|
||
* 缓存管理
|
||
* @param mixed $name 缓存名称,如果为数组表示进行缓存设置
|
||
* @param mixed $value 缓存值
|
||
* @param mixed $options 缓存参数
|
||
* @param string $tag 缓存标签
|
||
* @return mixed
|
||
*/
|
||
function cache($name, $value = '', $options = null, $tag = null)
|
||
{
|
||
if (is_array($options)) {
|
||
// 缓存操作的同时初始化
|
||
Cache::connect($options);
|
||
} elseif (is_array($name)) {
|
||
// 缓存初始化
|
||
return Cache::connect($name);
|
||
}
|
||
|
||
if ('' === $value) {
|
||
// 获取缓存
|
||
return 0 === strpos($name, '?') ? Cache::has(substr($name, 1)) : Cache::get($name);
|
||
} elseif (is_null($value)) {
|
||
// 删除缓存
|
||
return Cache::rm($name);
|
||
}
|
||
|
||
// 缓存数据
|
||
if (is_array($options)) {
|
||
$expire = isset($options['expire']) ? $options['expire'] : null; //修复查询缓存无法设置过期时间
|
||
} else {
|
||
$expire = is_numeric($options) ? $options : null; //默认快捷缓存设置过期时间
|
||
}
|
||
|
||
if (is_null($tag)) {
|
||
return Cache::set($name, $value, $expire);
|
||
} else {
|
||
return Cache::tag($tag)->set($name, $value, $expire);
|
||
}
|
||
}
|
||
}
|
||
|
||
if (!function_exists('call')) {
|
||
/**
|
||
* 调用反射执行callable 支持依赖注入
|
||
* @param mixed $callable 支持闭包等callable写法
|
||
* @param array $args 参数
|
||
* @return mixed
|
||
*/
|
||
function call($callable, $args = [])
|
||
{
|
||
return Container::getInstance()->invoke($callable, $args);
|
||
}
|
||
}
|
||
|
||
if (!function_exists('class_basename')) {
|
||
/**
|
||
* 获取类名(不包含命名空间)
|
||
*
|
||
* @param string|object $class
|
||
* @return string
|
||
*/
|
||
function class_basename($class)
|
||
{
|
||
$class = is_object($class) ? get_class($class) : $class;
|
||
return basename(str_replace('\\', '/', $class));
|
||
}
|
||
}
|
||
|
||
if (!function_exists('class_uses_recursive')) {
|
||
/**
|
||
*获取一个类里所有用到的trait,包括父类的
|
||
*
|
||
* @param $class
|
||
* @return array
|
||
*/
|
||
function class_uses_recursive($class)
|
||
{
|
||
if (is_object($class)) {
|
||
$class = get_class($class);
|
||
}
|
||
|
||
$results = [];
|
||
$classes = array_merge([$class => $class], class_parents($class));
|
||
foreach ($classes as $class) {
|
||
$results += trait_uses_recursive($class);
|
||
}
|
||
|
||
return array_unique($results);
|
||
}
|
||
}
|
||
|
||
if (!function_exists('config')) {
|
||
/**
|
||
* 获取和设置配置参数
|
||
* @param string|array $name 参数名
|
||
* @param mixed $value 参数值
|
||
* @return mixed
|
||
*/
|
||
function config($name = '', $value = null)
|
||
{
|
||
if (is_null($value) && is_string($name)) {
|
||
if ('.' == substr($name, -1)) {
|
||
return Config::pull(substr($name, 0, -1));
|
||
}
|
||
|
||
return 0 === strpos($name, '?') ? Config::has(substr($name, 1)) : Config::get($name);
|
||
} else {
|
||
return Config::set($name, $value);
|
||
}
|
||
}
|
||
}
|
||
|
||
if (!function_exists('container')) {
|
||
/**
|
||
* 获取容器对象实例
|
||
* @return Container
|
||
*/
|
||
function container()
|
||
{
|
||
return Container::getInstance();
|
||
}
|
||
}
|
||
|
||
if (!function_exists('controller')) {
|
||
/**
|
||
* 实例化控制器 格式:[模块/]控制器
|
||
* @param string $name 资源地址
|
||
* @param string $layer 控制层名称
|
||
* @param bool $appendSuffix 是否添加类名后缀
|
||
* @return \think\Controller
|
||
*/
|
||
function controller($name, $layer = 'controller', $appendSuffix = false)
|
||
{
|
||
return app()->controller($name, $layer, $appendSuffix);
|
||
}
|
||
}
|
||
|
||
if (!function_exists('cookie')) {
|
||
/**
|
||
* Cookie管理
|
||
* @param string|array $name cookie名称,如果为数组表示进行cookie设置
|
||
* @param mixed $value cookie值
|
||
* @param mixed $option 参数
|
||
* @return mixed
|
||
*/
|
||
function cookie($name, $value = '', $option = null)
|
||
{
|
||
if (is_array($name)) {
|
||
// 初始化
|
||
Cookie::init($name);
|
||
} elseif (is_null($name)) {
|
||
// 清除
|
||
Cookie::clear($value);
|
||
} elseif ('' === $value) {
|
||
// 获取
|
||
return 0 === strpos($name, '?') ? Cookie::has(substr($name, 1), $option) : Cookie::get($name);
|
||
} elseif (is_null($value)) {
|
||
// 删除
|
||
return Cookie::delete($name);
|
||
} else {
|
||
// 设置
|
||
return Cookie::set($name, $value, $option);
|
||
}
|
||
}
|
||
}
|
||
|
||
if (!function_exists('db')) {
|
||
/**
|
||
* 实例化数据库类
|
||
* @param string $name 操作的数据表名称(不含前缀)
|
||
* @param array|string $config 数据库配置参数
|
||
* @param bool $force 是否强制重新连接
|
||
* @return \think\db\Query
|
||
*/
|
||
function db($name = '', $config = [], $force = true)
|
||
{
|
||
return Db::connect($config, $force)->name($name);
|
||
}
|
||
}
|
||
|
||
if (!function_exists('debug')) {
|
||
/**
|
||
* 记录时间(微秒)和内存使用情况
|
||
* @param string $start 开始标签
|
||
* @param string $end 结束标签
|
||
* @param integer|string $dec 小数位 如果是m 表示统计内存占用
|
||
* @return mixed
|
||
*/
|
||
function debug($start, $end = '', $dec = 6)
|
||
{
|
||
if ('' == $end) {
|
||
Debug::remark($start);
|
||
} else {
|
||
return 'm' == $dec ? Debug::getRangeMem($start, $end) : Debug::getRangeTime($start, $end, $dec);
|
||
}
|
||
}
|
||
}
|
||
|
||
if (!function_exists('download')) {
|
||
/**
|
||
* 获取\think\response\Download对象实例
|
||
* @param string $filename 要下载的文件
|
||
* @param string $name 显示文件名
|
||
* @param bool $content 是否为内容
|
||
* @param integer $expire 有效期(秒)
|
||
* @return \think\response\Download
|
||
*/
|
||
function download($filename, $name = '', $content = false, $expire = 360, $openinBrowser = false)
|
||
{
|
||
return Response::create($filename, 'download')->name($name)->isContent($content)->expire($expire)->openinBrowser($openinBrowser);
|
||
}
|
||
}
|
||
|
||
if (!function_exists('dump')) {
|
||
/**
|
||
* 浏览器友好的变量输出
|
||
* @param mixed $var 变量
|
||
* @param boolean $echo 是否输出 默认为true 如果为false 则返回输出字符串
|
||
* @param string $label 标签 默认为空
|
||
* @return void|string
|
||
*/
|
||
function dump($var, $echo = true, $label = null)
|
||
{
|
||
return Debug::dump($var, $echo, $label);
|
||
}
|
||
}
|
||
|
||
if (!function_exists('env')) {
|
||
/**
|
||
* 获取环境变量值
|
||
* @access public
|
||
* @param string $name 环境变量名(支持二级 .号分割)
|
||
* @param string $default 默认值
|
||
* @return mixed
|
||
*/
|
||
function env($name = null, $default = null)
|
||
{
|
||
return Env::get($name, $default);
|
||
}
|
||
}
|
||
|
||
if (!function_exists('exception')) {
|
||
/**
|
||
* 抛出异常处理
|
||
*
|
||
* @param string $msg 异常消息
|
||
* @param integer $code 异常代码 默认为0
|
||
* @param string $exception 异常类
|
||
*
|
||
* @throws Exception
|
||
*/
|
||
function exception($msg, $code = 0, $exception = '')
|
||
{
|
||
$e = $exception ?: '\think\Exception';
|
||
throw new $e($msg, $code);
|
||
}
|
||
}
|
||
|
||
if (!function_exists('halt')) {
|
||
/**
|
||
* 调试变量并且中断输出
|
||
* @param mixed $var 调试变量或者信息
|
||
*/
|
||
function halt($var)
|
||
{
|
||
dump($var);
|
||
|
||
throw new HttpResponseException(new Response);
|
||
}
|
||
}
|
||
|
||
if (!function_exists('input')) {
|
||
/**
|
||
* 获取输入数据 支持默认值和过滤
|
||
* @param string $key 获取的变量名
|
||
* @param mixed $default 默认值
|
||
* @param string $filter 过滤方法
|
||
* @return mixed
|
||
*/
|
||
function input($key = '', $default = null, $filter = '')
|
||
{
|
||
if (0 === strpos($key, '?')) {
|
||
$key = substr($key, 1);
|
||
$has = true;
|
||
}
|
||
|
||
if ($pos = strpos($key, '.')) {
|
||
// 指定参数来源
|
||
$method = substr($key, 0, $pos);
|
||
if (in_array($method, ['get', 'post', 'put', 'patch', 'delete', 'route', 'param', 'request', 'session', 'cookie', 'server', 'env', 'path', 'file'])) {
|
||
$key = substr($key, $pos + 1);
|
||
} else {
|
||
$method = 'param';
|
||
}
|
||
} else {
|
||
// 默认为自动判断
|
||
$method = 'param';
|
||
}
|
||
|
||
if (isset($has)) {
|
||
return request()->has($key, $method, $default);
|
||
} else {
|
||
return request()->$method($key, $default, $filter);
|
||
}
|
||
}
|
||
}
|
||
|
||
if (!function_exists('json')) {
|
||
/**
|
||
* 获取\think\response\Json对象实例
|
||
* @param mixed $data 返回的数据
|
||
* @param integer $code 状态码
|
||
* @param array $header 头部
|
||
* @param array $options 参数
|
||
* @return \think\response\Json
|
||
*/
|
||