123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726 |
- <?php
- 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')) {
-
- 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')) {
-
- function action($url, $vars = [], $layer = 'controller', $appendSuffix = false)
- {
- return app()->action($url, $vars, $layer, $appendSuffix);
- }
- }
- if (!function_exists('app')) {
-
- function app($name = 'think\App', $args = [], $newInstance = false)
- {
- return Container::get($name, $args, $newInstance);
- }
- }
- if (!function_exists('behavior')) {
-
- function behavior($behavior, $args = null)
- {
- return Hook::exec($behavior, $args);
- }
- }
- if (!function_exists('bind')) {
-
- function bind($abstract, $concrete = null)
- {
- return Container::getInstance()->bindTo($abstract, $concrete);
- }
- }
- if (!function_exists('cache')) {
-
- 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')) {
-
- function call($callable, $args = [])
- {
- return Container::getInstance()->invoke($callable, $args);
- }
- }
- if (!function_exists('class_basename')) {
-
- function class_basename($class)
- {
- $class = is_object($class) ? get_class($class) : $class;
- return basename(str_replace('\\', '/', $class));
- }
- }
- if (!function_exists('class_uses_recursive')) {
-
- 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')) {
-
- 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')) {
-
- function container()
- {
- return Container::getInstance();
- }
- }
- if (!function_exists('controller')) {
-
- function controller($name, $layer = 'controller', $appendSuffix = false)
- {
- return app()->controller($name, $layer, $appendSuffix);
- }
- }
- 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);
- } elseif (is_null($value)) {
-
- return Cookie::delete($name);
- } else {
-
- return Cookie::set($name, $value, $option);
- }
- }
- }
- if (!function_exists('db')) {
-
- function db($name = '', $config = [], $force = true)
- {
- return Db::connect($config, $force)->name($name);
- }
- }
- 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('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')) {
-
- function dump($var, $echo = true, $label = null)
- {
- return Debug::dump($var, $echo, $label);
- }
- }
- if (!function_exists('env')) {
-
- function env($name = null, $default = null)
- {
- return Env::get($name, $default);
- }
- }
- if (!function_exists('exception')) {
-
- function exception($msg, $code = 0, $exception = '')
- {
- $e = $exception ?: '\think\Exception';
- throw new $e($msg, $code);
- }
- }
- if (!function_exists('halt')) {
-
- function halt($var)
- {
- dump($var);
- throw new HttpResponseException(new Response);
- }
- }
- if (!function_exists('input')) {
-
- 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')) {
-
- 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('lang')) {
-
- function lang($name, $vars = [], $lang = '')
- {
- return Lang::get($name, $vars, $lang);
- }
- }
- if (!function_exists('model')) {
-
- function model($name = '', $layer = 'model', $appendSuffix = false)
- {
- return app()->model($name, $layer, $appendSuffix);
- }
- }
- if (!function_exists('parse_name')) {
-
- function parse_name($name, $type = 0, $ucfirst = true)
- {
- if ($type) {
- $name = preg_replace_callback('/_([a-zA-Z])/', function ($match) {
- return strtoupper($match[1]);
- }, $name);
- return $ucfirst ? ucfirst($name) : lcfirst($name);
- } else {
- return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
- }
- }
- }
- if (!function_exists('redirect')) {
-
- function redirect($url = [], $params = [], $code = 302)
- {
- if (is_integer($params)) {
- $code = $params;
- $params = [];
- }
- return Response::create($url, 'redirect', $code)->params($params);
- }
- }
- if (!function_exists('request')) {
-
- function request()
- {
- return app('request');
- }
- }
- if (!function_exists('response')) {
-
- function response($data = '', $code = 200, $header = [], $type = 'html')
- {
- return Response::create($data, $type, $code, $header);
- }
- }
- if (!function_exists('route')) {
-
- function route($rule, $route, $option = [], $pattern = [])
- {
- return Route::rule($rule, $route, '*', $option, $pattern);
- }
- }
- if (!function_exists('session')) {
-
- function session($name, $value = '', $prefix = null)
- {
- if (is_array($name)) {
-
- Session::init($name);
- } elseif (is_null($name)) {
-
- Session::clear($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('token')) {
-
- function token($name = '__token__', $type = 'md5')
- {
- $token = Request::token($name, $type);
- return '<input type="hidden" name="' . $name . '" value="' . $token . '" />';
- }
- }
- if (!function_exists('trace')) {
-
- function trace($log = '[think]', $level = 'log')
- {
- if ('[think]' === $log) {
- return Log::getLog();
- } else {
- Log::record($log, $level);
- }
- }
- }
- if (!function_exists('trait_uses_recursive')) {
-
- function trait_uses_recursive($trait)
- {
- $traits = class_uses($trait);
- foreach ($traits as $trait) {
- $traits += trait_uses_recursive($trait);
- }
- return $traits;
- }
- }
- if (!function_exists('url')) {
-
- function url($url = '', $vars = '', $suffix = true, $domain = false)
- {
- return Url::build($url, $vars, $suffix, $domain);
- }
- }
- if (!function_exists('validate')) {
-
- function validate($name = '', $layer = 'validate', $appendSuffix = false)
- {
- return app()->validate($name, $layer, $appendSuffix);
- }
- }
- if (!function_exists('view')) {
-
- function view($template = '', $vars = [], $code = 200, $filter = null)
- {
- return Response::create($template, 'view', $code)->assign($vars)->filter($filter);
- }
- }
- if (!function_exists('widget')) {
-
- function widget($name, $data = [])
- {
- $result = app()->action($name, $data, 'widget');
- if (is_object($result)) {
- $result = $result->getContent();
- }
- return $result;
- }
- }
- if (!function_exists('xml')) {
-
- function xml($data = [], $code = 200, $header = [], $options = [])
- {
- return Response::create($data, 'xml', $code, $header, $options);
- }
- }
- if (!function_exists('yaconf')) {
-
- function yaconf($name, $default = null)
- {
- return Config::yaconf($name, $default);
- }
- }
|