123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611 |
- <?php
- use think\Cache;
- use think\Config;
- use think\Cookie;
- use think\Db;
- use think\Debug;
- use think\exception\HttpException;
- use think\exception\HttpResponseException;
- use think\Lang;
- use think\Loader;
- use think\Log;
- use think\Model;
- use think\Request;
- use think\Response;
- use think\Session;
- use think\Url;
- use think\View;
- if (!function_exists('load_trait')) {
-
- function load_trait($class, $ext = EXT)
- {
- return Loader::import($class, TRAIT_PATH, $ext);
- }
- }
- if (!function_exists('exception')) {
-
- function exception($msg, $code = 0, $exception = '')
- {
- $e = $exception ?: '\think\Exception';
- throw new $e($msg, $code);
- }
- }
- if (!function_exists('debug')) {
-
- 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('lang')) {
-
- function lang($name, $vars = [], $lang = '')
- {
- return Lang::get($name, $vars, $lang);
- }
- }
- if (!function_exists('config')) {
-
- function config($name = '', $value = null, $range = '')
- {
- if (is_null($value) && is_string($name)) {
- return 0 === strpos($name, '?') ? Config::has(substr($name, 1), $range) : Config::get($name, $range);
- } else {
- return Config::set($name, $value, $range);
- }
- }
- }
- if (!function_exists('input')) {
-
- function input($key = '', $default = null, $filter = '')
- {
- if (0 === strpos($key, '?')) {
- $key = substr($key, 1);
- $has = true;
- }
- if ($pos = strpos($key, '.')) {
-
- list($method, $key) = explode('.', $key, 2);
- if (!in_array($method, ['get', 'post', 'put', 'patch', 'delete', 'route', 'param', 'request', 'session', 'cookie', 'server', 'env', 'path', 'file'])) {
- $key = $method . '.' . $key;
- $method = 'param';
- }
- } else {
-
- $method = 'param';
- }
- if (isset($has)) {
- return request()->has($key, $method, $default);
- } else {
- return request()->$method($key, $default, $filter);
- }
- }
- }
- if (!function_exists('input_post')) {
-
- function input_post($key = '', $default = null, $filter = '')
- {
- return Request::instance()->request($key, $default, $filter);
- }
- }
- if (!function_exists('widget')) {
-
- function widget($name, $data = [])
- {
- return Loader::action($name, $data, 'widget');
- }
- }
- if (!function_exists('model')) {
-
- function model($name = '', $layer = 'model', $appendSuffix = false)
- {
- return Loader::model($name, $layer, $appendSuffix);
- }
- }
- if (!function_exists('validate')) {
-
- function validate($name = '', $layer = 'validate', $appendSuffix = false)
- {
- return Loader::validate($name, $layer, $appendSuffix);
- }
- }
- if (!function_exists('db')) {
-
- function db($name = '', $config = [], $force = false)
- {
- return Db::connect($config, $force)->name($name);
- }
- }
- if (!function_exists('controller')) {
-
- function controller($name, $layer = 'controller', $appendSuffix = false)
- {
- return Loader::controller($name, $layer, $appendSuffix);
- }
- }
- if (!function_exists('action')) {
-
- function action($url, $vars = [], $layer = 'controller', $appendSuffix = false)
- {
- return Loader::action($url, $vars, $layer, $appendSuffix);
- }
- }
- if (!function_exists('import')) {
-
- function import($class, $baseUrl = '', $ext = EXT)
- {
- return Loader::import($class, $baseUrl, $ext);
- }
- }
- if (!function_exists('vendor')) {
-
- function vendor($class, $ext = EXT)
- {
- return Loader::import($class, VENDOR_PATH, $ext);
- }
- }
- if (!function_exists('dump')) {
-
- function dump($var, $echo = true, $label = null)
- {
- return Debug::dump($var, $echo, $label);
- }
- }
- if (!function_exists('url')) {
-
- function url($url = '', $vars = '', $suffix = true, $domain = false)
- {
- return Url::build($url, $vars, $suffix, $domain);
- }
- }
- if (!function_exists('session')) {
-
- function session($name, $value = '', $prefix = null)
- {
- if (is_array($name)) {
-
- Session::init($name);
- } elseif (is_null($name)) {
-
- Session::clear('' === $value ? null : $value);
- } elseif ('' === $value) {
-
- return 0 === strpos($name, '?') ? Session::has(substr($name, 1), $prefix) : Session::get($name, $prefix);
- } elseif (is_null($value)) {
-
- return Session::delete($name, $prefix);
- } else {
-
- return Session::set($name, $value, $prefix);
- }
- }
- }
- if (!function_exists('cookie')) {
-
- 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, $option);
- } elseif (is_null($value)) {
-
- return Cookie::delete($name);
- } else {
-
- return Cookie::set($name, $value, $option);
- }
- }
- }
- if (!function_exists('cache')) {
-
- function redis_matching_set($name,$value,$expire = null){
- $cache = Cache::connect(['type'=>'Redis']);
- return $cache->set('mt_matching_uid_'.$name, $value, $expire);
- }
- function redis_matching_get($name){
- $cache = Cache::connect(['type'=>'Redis']);
- return $cache->get('mt_matching_uid_'.$name);
- }
- function cache($name, $value = '', $options = null, $tag = null)
- {
- if (is_array($options)) {
-
- $cache = Cache::connect($options);
- } elseif (is_array($name)) {
-
- return Cache::connect($name);
- } else {
- $cache = Cache::init();
- }
- if (is_null($name)) {
- return $cache->clear($value);
- } elseif ('' === $value) {
-
- return 0 === strpos($name, '?') ? $cache->has(substr($name, 1)) : $cache->get($name);
- } elseif (is_null($value)) {
-
- return $cache->rm($name);
- } elseif (0 === strpos($name, '?') && '' !== $value) {
- $expire = is_numeric($options) ? $options : null;
- return $cache->remember(substr($name, 1), $value, $expire);
- } else {
-
- 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('trace')) {
-
- function trace($log = '[think]', $level = 'log')
- {
- if ('[think]' === $log) {
- return Log::getLog();
- } else {
- Log::record($log, $level);
- }
- }
- }
- if (!function_exists('request')) {
-
- function request()
- {
- return Request::instance();
- }
- }
- if (!function_exists('response')) {
-
- function response($data = [], $code = 200, $header = [], $type = 'html')
- {
- return Response::create($data, $type, $code, $header);
- }
- }
- if (!function_exists('view')) {
-
- function view($template = '', $vars = [], $replace = [], $code = 200)
- {
- return Response::create($template, 'view', $code)->replace($replace)->assign($vars);
- }
- }
- if (!function_exists('json')) {
-
- function json($data = [], $code = 200, $header = [], $options = [])
- {
- return Response::create($data, 'json', $code, $header, $options);
- }
- }
- if (!function_exists('jsonp')) {
-
- function jsonp($data = [], $code = 200, $header = [], $options = [])
- {
- return Response::create($data, 'jsonp', $code, $header, $options);
- }
- }
- if (!function_exists('xml')) {
-
- function xml($data = [], $code = 200, $header = [], $options = [])
- {
- return Response::create($data, 'xml', $code, $header, $options);
- }
- }
- if (!function_exists('redirect')) {
-
- function redirect($url = [], $params = [], $code = 302, $with = [])
- {
- if (is_integer($params)) {
- $code = $params;
- $params = [];
- }
- return Response::create($url, 'redirect', $code)->params($params)->with($with);
- }
- }
- if (!function_exists('abort')) {
-
- 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('halt')) {
-
- function halt($var)
- {
- dump($var);
- throw new HttpResponseException(new Response);
- }
- }
- if (!function_exists('token')) {
-
- function token($name = '__token__', $type = 'md5')
- {
- $token = Request::instance()->token($name, $type);
- return '<input type="hidden" name="' . $name . '" value="' . $token . '" />';
- }
- }
- if (!function_exists('load_relation')) {
-
- function load_relation($resultSet, $relation)
- {
- $item = current($resultSet);
- if ($item instanceof Model) {
- $item->eagerlyResultSet($resultSet, $relation);
- }
- return $resultSet;
- }
- }
- if (!function_exists('collection')) {
-
- function collection($resultSet)
- {
- $item = current($resultSet);
- if ($item instanceof Model) {
- return \think\model\Collection::make($resultSet);
- } else {
- return \think\Collection::make($resultSet);
- }
- }
- }
|