Merge pull request #176 from chenjxlg/master

助手函数 是否定义判断,方便用户重写helper函数
This commit is contained in:
ThinkPHP
2016-07-01 13:53:32 +08:00
committed by GitHub

View File

@@ -33,9 +33,11 @@ use think\View;
* @param string $ext 类库后缀 * @param string $ext 类库后缀
* @return boolean * @return boolean
*/ */
function load_trait($class, $ext = EXT) if (!function_exists('load_trait')) {
{ function load_trait($class, $ext = EXT)
{
return Loader::import($class, TRAIT_PATH, $ext); return Loader::import($class, TRAIT_PATH, $ext);
}
} }
/** /**
@@ -47,10 +49,12 @@ function load_trait($class, $ext = EXT)
* *
* @throws Exception * @throws Exception
*/ */
function exception($msg, $code = 0, $exception = '') if (!function_exists('exception')) {
{ function exception($msg, $code = 0, $exception = '')
{
$e = $exception ?: '\think\Exception'; $e = $exception ?: '\think\Exception';
throw new $e($msg, $code); throw new $e($msg, $code);
}
} }
/** /**
@@ -60,13 +64,15 @@ function exception($msg, $code = 0, $exception = '')
* @param integer|string $dec 小数位 如果是m 表示统计内存占用 * @param integer|string $dec 小数位 如果是m 表示统计内存占用
* @return mixed * @return mixed
*/ */
function debug($start, $end = '', $dec = 6) if (!function_exists('debug')) {
{ function debug($start, $end = '', $dec = 6)
{
if ('' == $end) { if ('' == $end) {
Debug::remark($start); Debug::remark($start);
} else { } else {
return 'm' == $dec ? Debug::getRangeMem($start, $end) : Debug::getRangeTime($start, $end, $dec); return 'm' == $dec ? Debug::getRangeMem($start, $end) : Debug::getRangeTime($start, $end, $dec);
} }
}
} }
/** /**
@@ -76,9 +82,11 @@ function debug($start, $end = '', $dec = 6)
* @param string $lang 语言 * @param string $lang 语言
* @return mixed * @return mixed
*/ */
function lang($name, $vars = [], $lang = '') if (!function_exists('lang')) {
{ function lang($name, $vars = [], $lang = '')
{
return Lang::get($name, $vars, $lang); return Lang::get($name, $vars, $lang);
}
} }
/** /**
@@ -88,13 +96,15 @@ function lang($name, $vars = [], $lang = '')
* @param string $range 作用域 * @param string $range 作用域
* @return mixed * @return mixed
*/ */
function config($name = '', $value = null, $range = '') if (!function_exists('config')) {
{ function config($name = '', $value = null, $range = '')
{
if (is_null($value) && is_string($name)) { if (is_null($value) && is_string($name)) {
return Config::get($name, $range); return Config::get($name, $range);
} else { } else {
return Config::set($name, $value, $range); return Config::set($name, $value, $range);
} }
}
} }
/** /**
@@ -104,8 +114,9 @@ function config($name = '', $value = null, $range = '')
* @param string $filter 过滤方法 * @param string $filter 过滤方法
* @return mixed * @return mixed
*/ */
function input($key, $default = null, $filter = null) if (!function_exists('input')) {
{ function input($key, $default = null, $filter = null)
{
if (0 === strpos($key, '?')) { if (0 === strpos($key, '?')) {
$key = substr($key, 1); $key = substr($key, 1);
$has = true; $has = true;
@@ -127,6 +138,7 @@ function input($key, $default = null, $filter = null)
} else { } else {
return request()->$method($key, $default, $filter); return request()->$method($key, $default, $filter);
} }
}
} }
/** /**
@@ -135,9 +147,11 @@ function input($key, $default = null, $filter = null)
* @param array $data 传人的参数 * @param array $data 传人的参数
* @return mixed * @return mixed
*/ */
function widget($name, $data = []) if (!function_exists('widget')) {
{ function widget($name, $data = [])
{
return Loader::action($name, $data, 'widget'); return Loader::action($name, $data, 'widget');
}
} }
/** /**
@@ -147,9 +161,11 @@ function widget($name, $data = [])
* @param bool $appendSuffix 是否添加类名后缀 * @param bool $appendSuffix 是否添加类名后缀
* @return \think\Model * @return \think\Model
*/ */
function model($name = '', $layer = 'model', $appendSuffix = false) if (!function_exists('model')) {
{ function model($name = '', $layer = 'model', $appendSuffix = false)
{
return Loader::model($name, $layer, $appendSuffix); return Loader::model($name, $layer, $appendSuffix);
}
} }
/** /**
@@ -159,9 +175,11 @@ function model($name = '', $layer = 'model', $appendSuffix = false)
* @param bool $appendSuffix 是否添加类名后缀 * @param bool $appendSuffix 是否添加类名后缀
* @return \think\Validate * @return \think\Validate
*/ */
function validate($name = '', $layer = 'validate', $appendSuffix = false) if (!function_exists('validate')) {
{ function validate($name = '', $layer = 'validate', $appendSuffix = false)
{
return Loader::validate($name, $layer, $appendSuffix); return Loader::validate($name, $layer, $appendSuffix);
}
} }
/** /**
@@ -170,9 +188,11 @@ function validate($name = '', $layer = 'validate', $appendSuffix = false)
* @param array|string $config 数据库配置参数 * @param array|string $config 数据库配置参数
* @return \think\db\Query * @return \think\db\Query
*/ */
function db($name = '', $config = []) if (!function_exists('db')) {
{ function db($name = '', $config = [])
{
return Db::connect($config)->name($name); return Db::connect($config)->name($name);
}
} }
/** /**
@@ -182,9 +202,11 @@ function db($name = '', $config = [])
* @param bool $appendSuffix 是否添加类名后缀 * @param bool $appendSuffix 是否添加类名后缀
* @return \think\Controller * @return \think\Controller
*/ */
function controller($name, $layer = 'controller', $appendSuffix = false) if (!function_exists('controller')) {
{ function controller($name, $layer = 'controller', $appendSuffix = false)
{
return Loader::controller($name, $layer, $appendSuffix); return Loader::controller($name, $layer, $appendSuffix);
}
} }
/** /**
@@ -195,9 +217,11 @@ function controller($name, $layer = 'controller', $appendSuffix = false)
* @param bool $appendSuffix 是否添加类名后缀 * @param bool $appendSuffix 是否添加类名后缀
* @return mixed * @return mixed
*/ */
function action($url, $vars = [], $layer = 'controller', $appendSuffix = false) if (!function_exists('action')) {
{ function action($url, $vars = [], $layer = 'controller', $appendSuffix = false)
{
return Loader::action($url, $vars, $layer, $appendSuffix); return Loader::action($url, $vars, $layer, $appendSuffix);
}
} }
/** /**
@@ -207,9 +231,11 @@ function action($url, $vars = [], $layer = 'controller', $appendSuffix = false)
* @param string $ext 导入的文件扩展名 * @param string $ext 导入的文件扩展名
* @return boolean * @return boolean
*/ */
function import($class, $baseUrl = '', $ext = EXT) if (!function_exists('import')) {
{ function import($class, $baseUrl = '', $ext = EXT)
{
return Loader::import($class, $baseUrl, $ext); return Loader::import($class, $baseUrl, $ext);
}
} }
/** /**
@@ -218,9 +244,11 @@ function import($class, $baseUrl = '', $ext = EXT)
* @param string $ext 类库后缀 * @param string $ext 类库后缀
* @return boolean * @return boolean
*/ */
function vendor($class, $ext = EXT) if (!function_exists('vendor')) {
{ function vendor($class, $ext = EXT)
{
return Loader::import($class, VENDOR_PATH, $ext); return Loader::import($class, VENDOR_PATH, $ext);
}
} }
/** /**
@@ -230,9 +258,11 @@ function vendor($class, $ext = EXT)
* @param string $label 标签 默认为空 * @param string $label 标签 默认为空
* @return void|string * @return void|string
*/ */
function dump($var, $echo = true, $label = null) if (!function_exists('dump')) {
{ function dump($var, $echo = true, $label = null)
{
return Debug::dump($var, $echo, $label); return Debug::dump($var, $echo, $label);
}
} }
/** /**
@@ -243,9 +273,11 @@ function dump($var, $echo = true, $label = null)
* @param bool|string $domain 域名 * @param bool|string $domain 域名
* @return string * @return string
*/ */
function url($url = '', $vars = '', $suffix = true, $domain = false) if (!function_exists('url')) {
{ function url($url = '', $vars = '', $suffix = true, $domain = false)
{
return Url::build($url, $vars, $suffix, $domain); return Url::build($url, $vars, $suffix, $domain);
}
} }
/** /**
@@ -255,8 +287,9 @@ function url($url = '', $vars = '', $suffix = true, $domain = false)
* @param string $prefix 前缀 * @param string $prefix 前缀
* @return mixed * @return mixed
*/ */
function session($name, $value = '', $prefix = null) if (!function_exists('session')) {
{ function session($name, $value = '', $prefix = null)
{
if (is_array($name)) { if (is_array($name)) {
// 初始化 // 初始化
Session::init($name); Session::init($name);
@@ -273,6 +306,7 @@ function session($name, $value = '', $prefix = null)
// 设置 // 设置
return Session::set($name, $value, $prefix); return Session::set($name, $value, $prefix);
} }
}
} }
/** /**
@@ -282,8 +316,9 @@ function session($name, $value = '', $prefix = null)
* @param mixed $option 参数 * @param mixed $option 参数
* @return mixed * @return mixed
*/ */
function cookie($name, $value = '', $option = null) if (!function_exists('cookie')) {
{ function cookie($name, $value = '', $option = null)
{
if (is_array($name)) { if (is_array($name)) {
// 初始化 // 初始化
Cookie::init($name); Cookie::init($name);
@@ -300,6 +335,7 @@ function cookie($name, $value = '', $option = null)
// 设置 // 设置
return Cookie::set($name, $value, $option); return Cookie::set($name, $value, $option);
} }
}
} }
/** /**
@@ -309,8 +345,9 @@ function cookie($name, $value = '', $option = null)
* @param mixed $options 缓存参数 * @param mixed $options 缓存参数
* @return mixed * @return mixed
*/ */
function cache($name, $value = '', $options = null) if (!function_exists('cache')) {
{ function cache($name, $value = '', $options = null)
{
if (is_array($options)) { if (is_array($options)) {
// 缓存操作的同时初始化 // 缓存操作的同时初始化
Cache::connect($options); Cache::connect($options);
@@ -333,6 +370,7 @@ function cache($name, $value = '', $options = null)
} }
return Cache::set($name, $value, $expire); return Cache::set($name, $value, $expire);
} }
}
} }
/** /**
@@ -341,22 +379,26 @@ function cache($name, $value = '', $options = null)
* @param string $level 日志级别 * @param string $level 日志级别
* @return void|array * @return void|array
*/ */
function trace($log = '[think]', $level = 'log') if (!function_exists('trace')) {
{ function trace($log = '[think]', $level = 'log')
{
if ('[think]' === $log) { if ('[think]' === $log) {
return Log::getLog(); return Log::getLog();
} else { } else {
Log::record($log, $level); Log::record($log, $level);
} }
}
} }
/** /**
* 获取当前Request对象实例 * 获取当前Request对象实例
* @return Request * @return Request
*/ */
function request() if (!function_exists('request')) {
{ function request()
{
return Request::instance(); return Request::instance();
}
} }
/** /**
@@ -367,9 +409,11 @@ function request()
* @param string $type * @param string $type
* @return Response * @return Response
*/ */
function response($data = [], $code = 200, $header = [], $type = 'html') if (!function_exists('response')) {
{ function response($data = [], $code = 200, $header = [], $type = 'html')
{
return Response::create($data, $type, $code, $header); return Response::create($data, $type, $code, $header);
}
} }
/** /**
@@ -379,9 +423,11 @@ function response($data = [], $code = 200, $header = [], $type = 'html')
* @param integer $code 状态码 * @param integer $code 状态码
* @return \think\response\View * @return \think\response\View
*/ */
function view($template = '', $vars = [], $code = 200) if (!function_exists('view')) {
{ function view($template = '', $vars = [], $code = 200)
{
return Response::create($template, 'view', $code)->vars($vars); return Response::create($template, 'view', $code)->vars($vars);
}
} }
/** /**
@@ -392,9 +438,11 @@ function view($template = '', $vars = [], $code = 200)
* @param array $options 参数 * @param array $options 参数
* @return \think\response\Json * @return \think\response\Json
*/ */
function json($data = [], $code = 200, $header = [], $options = []) if (!function_exists('json')) {
{ function json($data = [], $code = 200, $header = [], $options = [])
{
return Response::create($data, 'json', $code, $header, $options); return Response::create($data, 'json', $code, $header, $options);
}
} }
/** /**
@@ -405,9 +453,11 @@ function json($data = [], $code = 200, $header = [], $options = [])
* @param array $options 参数 * @param array $options 参数
* @return \think\response\Jsonp * @return \think\response\Jsonp
*/ */
function jsonp($data = [], $code = 200, $header = [], $options = []) if (!function_exists('jsonp')) {
{ function jsonp($data = [], $code = 200, $header = [], $options = [])
{
return Response::create($data, 'jsonp', $code, $header, $options); return Response::create($data, 'jsonp', $code, $header, $options);
}
} }
/** /**
@@ -418,9 +468,11 @@ function jsonp($data = [], $code = 200, $header = [], $options = [])
* @param array $options 参数 * @param array $options 参数
* @return \think\response\Xml * @return \think\response\Xml
*/ */
function xml($data = [], $code = 200, $header = [], $options = []) if (!function_exists('xml')) {
{ function xml($data = [], $code = 200, $header = [], $options = [])
{
return Response::create($data, 'xml', $code, $header, $options); return Response::create($data, 'xml', $code, $header, $options);
}
} }
/** /**
@@ -430,13 +482,15 @@ function xml($data = [], $code = 200, $header = [], $options = [])
* @param integer $code 状态码 * @param integer $code 状态码
* @return \think\response\Redirect * @return \think\response\Redirect
*/ */
function redirect($url = [], $params = [], $code = 302) if (!function_exists('redirect')) {
{ function redirect($url = [], $params = [], $code = 302)
{
if (is_integer($params)) { if (is_integer($params)) {
$code = $params; $code = $params;
$params = []; $params = [];
} }
return Response::create($url, 'redirect', $code)->params($params); return Response::create($url, 'redirect', $code)->params($params);
}
} }
/** /**
@@ -445,7 +499,9 @@ function redirect($url = [], $params = [], $code = 302)
* @param string $message 错误信息 * @param string $message 错误信息
* @param array $header 参数 * @param array $header 参数
*/ */
function abort($code, $message = null, $header = []) if (!function_exists('abort')) {
{ function abort($code, $message = null, $header = [])
{
throw new \think\exception\HttpException($code, $message, null, $header); throw new \think\exception\HttpException($code, $message, null, $header);
}
} }