mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 20:52:48 +08:00
Merge branch 'master' of https://github.com/top-think/think
This commit is contained in:
75
base.php
75
base.php
@@ -39,6 +39,7 @@ defined('APP_HOOK') or define('APP_HOOK', false); // 是否开启HOOK
|
||||
defined('ENV_PREFIX') or define('ENV_PREFIX', 'T_'); // 环境变量的配置前缀
|
||||
defined('IS_API') or define('IS_API', false); // 是否API接口
|
||||
defined('IN_UNIT_TEST') or define('IN_UNIT_TEST', false); // 是否为单元测试
|
||||
defined('APP_AUTO_BUILD') or define('APP_AUTO_BUILD', false); // 是否自动生成应用模块
|
||||
|
||||
// 应用模式 默认为普通模式
|
||||
defined('APP_MODE') or define('APP_MODE', function_exists('saeAutoLoader') ? 'sae' : 'common');
|
||||
@@ -58,16 +59,16 @@ define('IS_DELETE', REQUEST_METHOD == 'DELETE' ? true : false);
|
||||
// 获取多语言变量
|
||||
function L($name, $vars = [], $lang = '')
|
||||
{
|
||||
return think\Lang::get($name, $vars, $lang);
|
||||
return \think\Lang::get($name, $vars, $lang);
|
||||
}
|
||||
|
||||
// 获取配置参数
|
||||
function C($name = '', $value = null, $range = '')
|
||||
{
|
||||
if (is_null($value) && is_string($name)) {
|
||||
return think\Config::get($name, $range);
|
||||
return \think\Config::get($name, $range);
|
||||
} else {
|
||||
return think\Config::set($name, $value, $range);
|
||||
return \think\Config::set($name, $value, $range);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,7 +82,7 @@ function I($key, $default = null, $filter = '')
|
||||
// 默认为自动判断
|
||||
$method = 'param';
|
||||
}
|
||||
return think\Input::$method($key, $default, $filter);
|
||||
return \think\Input::$method($key, $default, $filter);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,9 +95,9 @@ function I($key, $default = null, $filter = '')
|
||||
function G($start, $end = '', $dec = 6)
|
||||
{
|
||||
if ('' == $end) {
|
||||
think\Debug::remark($start);
|
||||
\think\Debug::remark($start);
|
||||
} else {
|
||||
return 'm' == $dec ? think\Debug::getRangeMem($start, $end) : think\Debug::getRangeTime($start, $end, $dec);
|
||||
return 'm' == $dec ? \think\Debug::getRangeMem($start, $end) : \think\Debug::getRangeTime($start, $end, $dec);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,7 +110,7 @@ function G($start, $end = '', $dec = 6)
|
||||
*/
|
||||
function M($name = '', $tablePrefix = '', $connection = '')
|
||||
{
|
||||
return think\Loader::table($name, ['prefix' => $tablePrefix, 'connection' => $connection]);
|
||||
return \think\Loader::table($name, ['prefix' => $tablePrefix, 'connection' => $connection]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -120,7 +121,7 @@ function M($name = '', $tablePrefix = '', $connection = '')
|
||||
*/
|
||||
function D($name = '', $layer = MODEL_LAYER)
|
||||
{
|
||||
return think\Loader::model($name, $layer);
|
||||
return \think\Loader::model($name, $layer);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -131,7 +132,7 @@ function D($name = '', $layer = MODEL_LAYER)
|
||||
*/
|
||||
function db($config = [], $lite = false)
|
||||
{
|
||||
return think\Db::instance($config, $lite);
|
||||
return \think\Db::instance($config, $lite);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -142,7 +143,7 @@ function db($config = [], $lite = false)
|
||||
*/
|
||||
function A($name, $layer = CONTROLLER_LAYER)
|
||||
{
|
||||
return think\Loader::controller($name, $layer);
|
||||
return \think\Loader::controller($name, $layer);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -154,7 +155,7 @@ function A($name, $layer = CONTROLLER_LAYER)
|
||||
*/
|
||||
function R($url, $vars = [], $layer = CONTROLLER_LAYER)
|
||||
{
|
||||
return think\Loader::action($url, $vars, $layer);
|
||||
return \think\Loader::action($url, $vars, $layer);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -166,7 +167,7 @@ function R($url, $vars = [], $layer = CONTROLLER_LAYER)
|
||||
*/
|
||||
function import($class, $baseUrl = '', $ext = EXT)
|
||||
{
|
||||
return think\Loader::import($class, $baseUrl, $ext);
|
||||
return \think\Loader::import($class, $baseUrl, $ext);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -177,7 +178,7 @@ function import($class, $baseUrl = '', $ext = EXT)
|
||||
*/
|
||||
function vendor($class, $ext = EXT)
|
||||
{
|
||||
return think\Loader::import($class, VENDOR_PATH, $ext);
|
||||
return \think\Loader::import($class, VENDOR_PATH, $ext);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -188,7 +189,7 @@ function vendor($class, $ext = EXT)
|
||||
*/
|
||||
function T($class, $ext = EXT)
|
||||
{
|
||||
return think\Loader::import($class, TRAIT_PATH, $ext);
|
||||
return \think\Loader::import($class, TRAIT_PATH, $ext);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -201,7 +202,7 @@ function T($class, $ext = EXT)
|
||||
*/
|
||||
function E($msg, $code = 0)
|
||||
{
|
||||
throw new think\Exception($msg, $code);
|
||||
throw new \think\Exception($msg, $code);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -213,7 +214,7 @@ function E($msg, $code = 0)
|
||||
*/
|
||||
function dump($var, $echo = true, $label = null)
|
||||
{
|
||||
return think\Debug::dump($var, $echo, $label);
|
||||
return \think\Debug::dump($var, $echo, $label);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -224,31 +225,31 @@ function dump($var, $echo = true, $label = null)
|
||||
*/
|
||||
function W($name, $data = [])
|
||||
{
|
||||
return think\Loader::action($name, $data, 'Widget');
|
||||
return \think\Loader::action($name, $data, 'Widget');
|
||||
}
|
||||
|
||||
function U($url, $vars = '', $suffix = true, $domain = false)
|
||||
{
|
||||
return think\Url::build($url, $vars, $suffix, $domain);
|
||||
return \think\Url::build($url, $vars, $suffix, $domain);
|
||||
}
|
||||
|
||||
function session($name, $value = '')
|
||||
{
|
||||
if (is_array($name)) {
|
||||
// 初始化
|
||||
think\Session::init($name);
|
||||
\think\Session::init($name);
|
||||
} elseif (is_null($name)) {
|
||||
// 清除
|
||||
think\Session::clear($value);
|
||||
\think\Session::clear($value);
|
||||
} elseif ('' === $value) {
|
||||
// 获取
|
||||
return think\Session::get($name);
|
||||
return \think\Session::get($name);
|
||||
} elseif (is_null($value)) {
|
||||
// 删除session
|
||||
return think\Session::delete($name);
|
||||
return \think\Session::delete($name);
|
||||
} else {
|
||||
// 设置session
|
||||
return think\Session::set($name, $value);
|
||||
return \think\Session::set($name, $value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -256,19 +257,19 @@ function cookie($name, $value = '')
|
||||
{
|
||||
if (is_array($name)) {
|
||||
// 初始化
|
||||
think\Cookie::init($name);
|
||||
\think\Cookie::init($name);
|
||||
} elseif (is_null($name)) {
|
||||
// 清除
|
||||
think\Cookie::clear($value);
|
||||
\think\Cookie::clear($value);
|
||||
} elseif ('' === $value) {
|
||||
// 获取
|
||||
return think\Cookie::get($name);
|
||||
return \think\Cookie::get($name);
|
||||
} elseif (is_null($value)) {
|
||||
// 删除session
|
||||
return think\Cookie::delete($name);
|
||||
return \think\Cookie::delete($name);
|
||||
} else {
|
||||
// 设置session
|
||||
return think\Cookie::set($name, $value);
|
||||
return \think\Cookie::set($name, $value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,17 +284,17 @@ function S($name, $value = '', $options = null)
|
||||
{
|
||||
if (is_array($options)) {
|
||||
// 缓存操作的同时初始化
|
||||
think\Cache::connect($options);
|
||||
\think\Cache::connect($options);
|
||||
} elseif (is_array($name)) {
|
||||
// 缓存初始化
|
||||
return think\Cache::connect($name);
|
||||
return \think\Cache::connect($name);
|
||||
}
|
||||
if ('' === $value) {
|
||||
// 获取缓存
|
||||
return think\Cache::get($name);
|
||||
return \think\Cache::get($name);
|
||||
} elseif (is_null($value)) {
|
||||
// 删除缓存
|
||||
return think\Cache::rm($name);
|
||||
return \think\Cache::rm($name);
|
||||
} else {
|
||||
// 缓存数据
|
||||
if (is_array($options)) {
|
||||
@@ -301,7 +302,7 @@ function S($name, $value = '', $options = null)
|
||||
} else {
|
||||
$expire = is_numeric($options) ? $options : null; //默认快捷缓存设置过期时间
|
||||
}
|
||||
return think\Cache::set($name, $value, $expire);
|
||||
return \think\Cache::set($name, $value, $expire);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -311,11 +312,11 @@ function S($name, $value = '', $options = null)
|
||||
* @param string $level 日志级别
|
||||
* @return void|array
|
||||
*/
|
||||
function trace($log='[think]', $level = 'log')
|
||||
function trace($log = '[think]', $level = 'log')
|
||||
{
|
||||
if('[think]'==$log){
|
||||
if ('[think]' == $log) {
|
||||
return \think\Log::getLog();
|
||||
}else{
|
||||
\think\Log::record($log,$level);
|
||||
} else {
|
||||
\think\Log::record($log, $level);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,8 +13,8 @@ return [
|
||||
'default_return_type' => 'html',
|
||||
// 默认语言
|
||||
'default_lang' => 'zh-cn',
|
||||
// response输出终止执行
|
||||
'response_exit' => true,
|
||||
// response是否返回方式
|
||||
'response_return' => false,
|
||||
// 默认AJAX 数据返回格式,可选JSON XML ...
|
||||
'default_ajax_return' => 'JSON',
|
||||
// 默认JSONP格式返回的处理方法
|
||||
|
||||
@@ -47,7 +47,7 @@ class Tencent extends Driver
|
||||
'oauth_consumer_key' => $this->AppKey,
|
||||
'access_token' => $this->token['access_token'],
|
||||
'openid' => $this->openid(),
|
||||
'clientip' => get_client_ip(),
|
||||
'clientip' => $_SERVER['REMOTE_ADDR'],
|
||||
'oauth_version' => '2.a',
|
||||
'scope' => 'all',
|
||||
'format' => 'json',
|
||||
|
||||
@@ -23,21 +23,21 @@ class App
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public static function run(array $config = [])
|
||||
public static function run()
|
||||
{
|
||||
// 初始化公共模块
|
||||
self::initModule(COMMON_MODULE, $config);
|
||||
|
||||
// 获取配置参数
|
||||
$config = Config::get();
|
||||
self::initModule(COMMON_MODULE, Config::get());
|
||||
|
||||
// 读取扩展配置文件
|
||||
if ($config['extra_config_list']) {
|
||||
foreach ($config['extra_config_list'] as $file) {
|
||||
if (Config::get('extra_config_list')) {
|
||||
foreach (Config::get('extra_config_list') as $file) {
|
||||
Config::load($file, $file);
|
||||
}
|
||||
}
|
||||
|
||||
// 获取配置参数
|
||||
$config = Config::get();
|
||||
|
||||
// 日志初始化
|
||||
Log::init($config['log']);
|
||||
// 缓存初始化
|
||||
@@ -110,12 +110,8 @@ class App
|
||||
}
|
||||
// 操作方法执行完成监听
|
||||
APP_HOOK && Hook::listen('action_end', $data);
|
||||
// 返回数据
|
||||
if (IN_UNIT_TEST) {
|
||||
return $data;
|
||||
} else {
|
||||
Response::returnData($data, Config::get('default_return_type'), Config::get('response_exit'));
|
||||
}
|
||||
// 输出数据
|
||||
return Response::send($data, '', Config::get('response_return'));
|
||||
} else {
|
||||
// 操作方法不是Public 抛出异常
|
||||
throw new \ReflectionException();
|
||||
@@ -127,12 +123,8 @@ class App
|
||||
$data = $method->invokeArgs($instance, [$action, '']);
|
||||
// 操作方法执行完成监听
|
||||
APP_HOOK && Hook::listen('action_end', $data);
|
||||
// 返回数据
|
||||
if (IN_UNIT_TEST) {
|
||||
return $data;
|
||||
} else {
|
||||
Response::returnData($data, Config::get('default_return_type'), Config::get('response_exit'));
|
||||
}
|
||||
// 输出数据
|
||||
return Response::send($data, '', Config::get('response_return'));
|
||||
} else {
|
||||
throw new Exception('method [ ' . (new \ReflectionClass($instance))->getName() . '->' . $action . ' ] not exists ', 10002);
|
||||
}
|
||||
@@ -251,7 +243,10 @@ class App
|
||||
|
||||
// 检测域名部署
|
||||
if (!IS_CLI && !empty($config['url_domain_deploy'])) {
|
||||
if ($match = Route::checkDomain($config['url_domain_rules'])) {
|
||||
if ($config['url_domain_rules']) {
|
||||
Route::domain($config['url_domain_rules']);
|
||||
}
|
||||
if ($match = Route::checkDomain()) {
|
||||
(!defined('BIND_MODULE') && !empty($match[0])) && define('BIND_MODULE', $match[0]);
|
||||
(!defined('BIND_CONTROLLER') && !empty($match[1])) && define('BIND_CONTROLLER', $match[1]);
|
||||
(!defined('BIND_ACTION') && !empty($match[2])) && define('BIND_ACTION', $match[2]);
|
||||
@@ -292,15 +287,15 @@ class App
|
||||
$depr = $config['pathinfo_depr'];
|
||||
// 还原劫持后真实pathinfo
|
||||
$path_info =
|
||||
(defined('BIND_MODULE') ? BIND_MODULE . $depr : '') .
|
||||
(defined('BIND_CONTROLLER') ? BIND_CONTROLLER . $depr : '') .
|
||||
(defined('BIND_ACTION') ? BIND_ACTION . $depr : '') .
|
||||
$_SERVER['PATH_INFO'];
|
||||
(defined('BIND_MODULE') ? BIND_MODULE . $depr : '') .
|
||||
(defined('BIND_CONTROLLER') ? BIND_CONTROLLER . $depr : '') .
|
||||
(defined('BIND_ACTION') ? BIND_ACTION . $depr : '') .
|
||||
$_SERVER['PATH_INFO'];
|
||||
|
||||
// 路由检测
|
||||
if (!empty($config['url_route_on'])) {
|
||||
// 开启路由 则检测路由配置
|
||||
Route::register($config['route']);
|
||||
Route::register(!empty($config['route']) ? $config['route'] : null);
|
||||
$result = Route::check($path_info, $depr);
|
||||
if (false === $result) {
|
||||
// 路由无效
|
||||
|
||||
@@ -8,15 +8,14 @@
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think;
|
||||
|
||||
class Create
|
||||
class Build
|
||||
{
|
||||
public static function build($build)
|
||||
public static function run($build)
|
||||
{
|
||||
// 锁定
|
||||
$lockfile = APP_PATH . 'create.lock';
|
||||
$lockfile = APP_PATH . 'build.lock';
|
||||
if (is_writable($lockfile)) {
|
||||
return;
|
||||
} else {
|
||||
4
library/think/cache/driver/apc.php
vendored
4
library/think/cache/driver/apc.php
vendored
@@ -25,7 +25,9 @@ class Apc
|
||||
'prefix' => '',
|
||||
'length' => 0,
|
||||
];
|
||||
|
||||
/*****************************
|
||||
需要支持apc_cli模式
|
||||
******************************/
|
||||
/**
|
||||
* 架构函数
|
||||
*
|
||||
|
||||
@@ -34,7 +34,7 @@ abstract class Hprose
|
||||
}
|
||||
|
||||
//导入类库
|
||||
think\Loader::import('vendor.Hprose.HproseHttpServer');
|
||||
\think\Loader::import('vendor.Hprose.HproseHttpServer');
|
||||
//实例化HproseHttpServer
|
||||
$server = new \HproseHttpServer();
|
||||
if ($this->allowMethodList) {
|
||||
|
||||
@@ -28,7 +28,7 @@ abstract class Jsonrpc
|
||||
}
|
||||
|
||||
//导入类库
|
||||
think\Loader::import('vendor.jsonrpc.jsonRPCServer');
|
||||
\think\Loader::import('vendor.jsonrpc.jsonRPCServer');
|
||||
// 启动server
|
||||
\jsonRPCServer::handle($this);
|
||||
}
|
||||
|
||||
@@ -93,7 +93,10 @@ abstract class Rest
|
||||
protected function response($data, $type = '', $code = 200)
|
||||
{
|
||||
Response::sendHttpStatus($code);
|
||||
Response::returnData($data, strtolower($type));
|
||||
Response::data($data);
|
||||
if ($type) {
|
||||
Response::type($type);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -131,7 +134,7 @@ abstract class Rest
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ abstract class Rpc
|
||||
}
|
||||
|
||||
//导入类库
|
||||
think\Loader::import('vendor.phprpc.phprpc_server');
|
||||
\think\Loader::import('vendor.phprpc.phprpc_server');
|
||||
//实例化phprpc
|
||||
$server = new \PHPRPC_Server();
|
||||
if ($this->allowMethodList) {
|
||||
|
||||
@@ -1148,7 +1148,7 @@ abstract class Driver
|
||||
*/
|
||||
protected function debug($start)
|
||||
{
|
||||
if ($this->config['debug']) {
|
||||
if (!empty($this->config['debug'])) {
|
||||
// 开启数据库调试模式
|
||||
if ($start) {
|
||||
Debug::remark('queryStartTime', 'time');
|
||||
|
||||
@@ -61,7 +61,7 @@ class Mongo extends Driver
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public function connect($config = '', $linkNum = 0)
|
||||
public function connect($config = '', $linkNum = 0, $autoConnection = false)
|
||||
{
|
||||
if (!isset($this->linkID[$linkNum])) {
|
||||
if (empty($config)) {
|
||||
@@ -70,7 +70,7 @@ class Mongo extends Driver
|
||||
|
||||
$host = 'mongodb://' . ($config['username'] ? "{$config['username']}" : '') . ($config['password'] ? ":{$config['password']}@" : '') . $config['hostname'] . ($config['hostport'] ? ":{$config['hostport']}" : '') . '/' . ($config['database'] ? "{$config['database']}" : '');
|
||||
try {
|
||||
$this->linkID[$linkNum] = new \mongoClient($host, $this->config['params']);
|
||||
$this->linkID[$linkNum] = new \mongoClient($host, !empty($this->config['params'])?$this->config['params']:array());
|
||||
} catch (\MongoConnectionException $e) {
|
||||
throw new Exception($e->getmessage());
|
||||
}
|
||||
@@ -105,7 +105,7 @@ class Mongo extends Driver
|
||||
$this->_mongo = $this->_linkID->selectDb($db);
|
||||
}
|
||||
// 当前MongoCollection对象
|
||||
if ($this->config['debug']) {
|
||||
if (!empty($this->config['debug'])) {
|
||||
$this->queryStr = $this->_dbName . '.getCollection(' . $collection . ')';
|
||||
}
|
||||
if ($this->_collectionName != $collection) {
|
||||
@@ -249,7 +249,7 @@ class Mongo extends Driver
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function insertAll($dataList, $options = [])
|
||||
public function insertAll($dataList, $options = [], $replace = false)
|
||||
{
|
||||
if (isset($options['table'])) {
|
||||
$this->switchCollection($options['table']);
|
||||
@@ -483,9 +483,9 @@ class Mongo extends Driver
|
||||
}
|
||||
$this->model = $options['model'];
|
||||
$this->queryTimes++;
|
||||
$query = $this->parseWhere($options['where']);
|
||||
$fields = $this->parseField($options['field']);
|
||||
if ($this->config['debug']) {
|
||||
$query = $this->parseWhere(!empty($options['where'])?$options['where']:'');
|
||||
$fields = $this->parseField(!empty($options['field'])?$options['field']:'');
|
||||
if (!empty($this->config['debug'])) {
|
||||
$this->queryStr = $this->_dbName . '.' . $this->_collectionName . '.findOne(';
|
||||
$this->queryStr .= $query ? json_encode($query) : '{}';
|
||||
$this->queryStr .= $fields ? ',' . json_encode($fields) : '';
|
||||
@@ -519,7 +519,7 @@ class Mongo extends Driver
|
||||
$this->model = $options['model'];
|
||||
$this->queryTimes++;
|
||||
$query = $this->parseWhere($options['where']);
|
||||
if ($this->config['debug']) {
|
||||
if (!empty($this->config['debug'])) {
|
||||
$this->queryStr = $this->_dbName . '.' . $this->_collectionName;
|
||||
$this->queryStr .= $query ? '.find(' . json_encode($query) . ')' : '';
|
||||
$this->queryStr .= '.count()';
|
||||
|
||||
@@ -101,7 +101,7 @@ class Mongo extends \Think\Model
|
||||
}
|
||||
|
||||
// 插入数据前的回调方法
|
||||
protected function _before_insert(&$data, $options)
|
||||
protected function _before_insert(&$data, $options = [])
|
||||
{
|
||||
// 写入数据到数据库
|
||||
if ($this->_autoInc && self::TYPE_INT == $this->_idType) {
|
||||
@@ -119,7 +119,7 @@ class Mongo extends \Think\Model
|
||||
}
|
||||
|
||||
// 查询成功后的回调方法
|
||||
protected function _after_select(&$resultSet, $options)
|
||||
protected function _after_select(&$resultSet, $options = [])
|
||||
{
|
||||
array_walk($resultSet, [$this, 'checkMongoId']);
|
||||
}
|
||||
@@ -185,7 +185,7 @@ class Mongo extends \Think\Model
|
||||
* @param integer $step 增长值
|
||||
* @return boolean
|
||||
*/
|
||||
public function setInc($field, $step = 1)
|
||||
public function setInc($field, $step = 1, $lazyTime = 0)
|
||||
{
|
||||
return $this->setField($field, ['inc', $step]);
|
||||
}
|
||||
@@ -197,7 +197,7 @@ class Mongo extends \Think\Model
|
||||
* @param integer $step 减少值
|
||||
* @return boolean
|
||||
*/
|
||||
public function setDec($field, $step = 1)
|
||||
public function setDec($field, $step = 1, $lazyTime = 0)
|
||||
{
|
||||
return $this->setField($field, ['inc', '-' . $step]);
|
||||
}
|
||||
|
||||
@@ -13,17 +13,27 @@ namespace think;
|
||||
|
||||
class Response
|
||||
{
|
||||
// 输出数据的转换方法
|
||||
protected static $tramsform = null;
|
||||
// 输出数据的类型
|
||||
protected static $type = 'html';
|
||||
// 输出数据
|
||||
protected static $data = '';
|
||||
// 是否exit
|
||||
protected static $isExit = false;
|
||||
|
||||
/**
|
||||
* 返回数据到客户端
|
||||
* 发送数据到客户端
|
||||
* @access protected
|
||||
* @param mixed $data 要返回的数据
|
||||
* @param String $type 返回数据格式
|
||||
* @param bool $exit 是否终止执行
|
||||
* @param bool $return 是否返回数据
|
||||
* @return void
|
||||
*/
|
||||
public static function returnData($data, $type = '', $exit = true)
|
||||
public static function send($data = '', $type = '', $return = false)
|
||||
{
|
||||
$type = strtolower($type ?: self::$type);
|
||||
|
||||
$headers = [
|
||||
'json' => 'application/json',
|
||||
'xml' => 'text/xml',
|
||||
@@ -32,32 +42,86 @@ class Response
|
||||
'script' => 'application/javascript',
|
||||
'text' => 'text/plain',
|
||||
];
|
||||
$type = strtolower($type);
|
||||
|
||||
if (!headers_sent() && isset($headers[$type])) {
|
||||
header('Content-Type:' . $headers[$type] . '; charset=utf-8');
|
||||
}
|
||||
|
||||
switch ($type) {
|
||||
case 'json':
|
||||
// 返回JSON数据格式到客户端 包含状态信息
|
||||
$data = json_encode($data, JSON_UNESCAPED_UNICODE);
|
||||
break;
|
||||
case 'xml':
|
||||
// 返回xml格式数据
|
||||
$data = \org\Transform::xmlEncode($data);
|
||||
break;
|
||||
case 'jsonp':
|
||||
// 返回JSON数据格式到客户端 包含状态信息
|
||||
$handler = !empty($_GET[Config::get('var_jsonp_handler')]) ? $_GET[Config::get('var_jsonp_handler')] : Config::get('default_jsonp_handler');
|
||||
$data = $handler . '(' . \org\Transform::jsonEncode($data) . ');';
|
||||
break;
|
||||
$data = $data ?: self::$data;
|
||||
if (is_callable(self::$tramsform)) {
|
||||
$data = call_user_func_array(self::$tramsform, [$data]);
|
||||
} else {
|
||||
switch ($type) {
|
||||
case 'json':
|
||||
// 返回JSON数据格式到客户端 包含状态信息
|
||||
$data = json_encode($data, JSON_UNESCAPED_UNICODE);
|
||||
break;
|
||||
case 'jsonp':
|
||||
// 返回JSON数据格式到客户端 包含状态信息
|
||||
$handler = !empty($_GET[Config::get('var_jsonp_handler')]) ? $_GET[Config::get('var_jsonp_handler')] : Config::get('default_jsonp_handler');
|
||||
$data = $handler . '(' . json_encode($data, JSON_UNESCAPED_UNICODE) . ');';
|
||||
break;
|
||||
case '':
|
||||
// 类型为空不做处理
|
||||
break;
|
||||
default:
|
||||
// 用于扩展其他返回格式数据
|
||||
Hook::listen('return_data', $data);
|
||||
}
|
||||
}
|
||||
//header('Content-Length:' . strlen($data));
|
||||
if ($exit) {
|
||||
exit($data);
|
||||
|
||||
if ($return) {
|
||||
return $data;
|
||||
} else {
|
||||
echo $data;
|
||||
}
|
||||
if (self::$isExit) {
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换控制器输出的数据
|
||||
* @access public
|
||||
* @param mixed $callback 调用的转换方法
|
||||
* @return void
|
||||
*/
|
||||
public static function tramsform($callback)
|
||||
{
|
||||
self::$tramsform = $callback;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出类型设置
|
||||
* @access public
|
||||
* @param string $type 输出内容的格式类型
|
||||
* @return void
|
||||
*/
|
||||
public static function type($type)
|
||||
{
|
||||
self::$type = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出数据设置
|
||||
* @access public
|
||||
* @param mixed $data 输出数据
|
||||
* @return void
|
||||
*/
|
||||
public static function data($data)
|
||||
{
|
||||
self::$data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出是否exit设置
|
||||
* @access public
|
||||
* @param bool $exit 是否退出
|
||||
* @return void
|
||||
*/
|
||||
public static function isExit($exit = false)
|
||||
{
|
||||
self::$isExit = $exit;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,7 +141,11 @@ class Response
|
||||
'time' => NOW_TIME,
|
||||
'data' => $data,
|
||||
];
|
||||
self::returnData($result, $type, true);
|
||||
|
||||
self::$data = $result;
|
||||
if ($type) {
|
||||
self::$type = $type;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -103,7 +171,10 @@ class Response
|
||||
$view = new \think\View();
|
||||
$result = $view->fetch(Config::get('dispatch_jump_tmpl'), $result);
|
||||
}
|
||||
self::returnData($result, $type, true);
|
||||
self::$data = $result;
|
||||
if ($type) {
|
||||
self::$type = $type;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,7 +200,10 @@ class Response
|
||||
$view = new \think\View();
|
||||
$result = $view->fetch(Config::get('dispatch_jump_tmpl'), $result);
|
||||
}
|
||||
self::returnData($result, $type, true);
|
||||
self::$data = $result;
|
||||
if ($type) {
|
||||
self::$type = $type;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -34,32 +34,34 @@ class Route
|
||||
// 添加URL映射规则
|
||||
public static function map($map, $route = '')
|
||||
{
|
||||
self::setting('map', $map, $route);
|
||||
return self::setting('map', $map, $route);
|
||||
}
|
||||
|
||||
// 添加变量规则
|
||||
public static function pattern($name, $rule = '')
|
||||
{
|
||||
self::setting('pattern', $name, $rule);
|
||||
return self::setting('pattern', $name, $rule);
|
||||
}
|
||||
|
||||
// 添加路由别名
|
||||
public static function alias($name, $rule = '')
|
||||
{
|
||||
self::setting('alias', $name, $rule);
|
||||
return self::setting('alias', $name, $rule);
|
||||
}
|
||||
|
||||
// 添加子域名部署规则
|
||||
public static function domain($domain, $rule = '')
|
||||
{
|
||||
self::setting('domain', $domain, $rule);
|
||||
return self::setting('domain', $domain, $rule);
|
||||
}
|
||||
|
||||
// 属性设置
|
||||
private static function setting($var, $name, $value = '')
|
||||
private static function setting($var, $name = '', $value = '')
|
||||
{
|
||||
if (is_array($name)) {
|
||||
self::${$var} = array_merge(self::${$var}, $name);
|
||||
} elseif (empty($name)) {
|
||||
return self::${$var};
|
||||
} else {
|
||||
self::${$var}[$name] = $value;
|
||||
}
|
||||
@@ -309,7 +311,7 @@ class Route
|
||||
$rule = array_shift($val);
|
||||
}
|
||||
// 单项路由
|
||||
$route = $val['route'];
|
||||
$route = !empty($val['route'])?$val['route']:'';
|
||||
if (0 === strpos($rule, '/') && preg_match($rule, $url, $matches)) {
|
||||
return self::checkRegex($route, $url, $matches);
|
||||
} else {
|
||||
|
||||
@@ -615,10 +615,14 @@ class Template
|
||||
$vars = explode('.', $var);
|
||||
$var = array_shift($vars);
|
||||
$name = '$' . $var;
|
||||
foreach ($vars as $key => $val) {
|
||||
$name .= '["' . $val . '"]';
|
||||
if (count($vars) > 1) {
|
||||
foreach ($vars as $key => $val) {
|
||||
$name .= '["' . $val . '"]';
|
||||
}
|
||||
} else {
|
||||
// 一维自动识别对象和数组
|
||||
$name = 'is_array($' . $var . ')?$' . $var . '["' . $vars[0] . '"]:$' . $var . '->' . $vars[0];
|
||||
}
|
||||
|
||||
} elseif (false !== strpos($var, '[')) {
|
||||
//支持 {$var['key']} 方式输出数组
|
||||
$name = "$" . $var;
|
||||
|
||||
@@ -134,14 +134,15 @@ class Url
|
||||
$domain = $host . (strpos($host, '.') ? '' : strstr($_SERVER['HTTP_HOST'], '.'));
|
||||
} elseif (true === $domain) {
|
||||
$domain = $_SERVER['HTTP_HOST'];
|
||||
if (Config::get('url_domain_deplay')) {
|
||||
if (Config::get('url_domain_deploy')) {
|
||||
// 开启子域名部署
|
||||
$domain = 'localhost' == $domain ? 'localhost' : 'www' . strstr($_SERVER['HTTP_HOST'], '.');
|
||||
// '子域名'=>array('项目[/分组]');
|
||||
foreach (Config::get('url_domain_rules') as $key => $rule) {
|
||||
if (false === strpos($key, '*') && 0 === strpos($url, $rule[0])) {
|
||||
// '子域名'=>['模块[/控制器/操作]'];
|
||||
foreach (Route::domain() as $key => $rule) {
|
||||
$rule = is_array($rule) ? $rule[0] : $rule;
|
||||
if (false === strpos($key, '*') && 0 === strpos($url, $rule)) {
|
||||
$domain = $key . strstr($domain, '.'); // 生成对应子域名
|
||||
$url = substr_replace($url, '', 0, strlen($rule[0]));
|
||||
$url = substr_replace($url, '', 0, strlen($rule));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,6 +40,14 @@ class View
|
||||
$this->config($config);
|
||||
$this->engine($this->config['engine_type']);
|
||||
}
|
||||
/**
|
||||
* 初始化视图
|
||||
* @access public
|
||||
* @param array $config 配置参数
|
||||
*/
|
||||
static public function getInstance(array $config = []){
|
||||
return new self($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 模板变量赋值
|
||||
@@ -55,6 +63,7 @@ class View
|
||||
} else {
|
||||
$this->data[$name] = $value;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -361,9 +361,9 @@ trait Auto
|
||||
}
|
||||
return NOW_TIME >= $start && NOW_TIME <= $end;
|
||||
case 'ip_allow': // IP 操作许可验证
|
||||
return in_array(get_client_ip(), explode(',', $rule));
|
||||
return in_array($_SERVER['REMOTE_ADDR'], explode(',', $rule));
|
||||
case 'ip_deny': // IP 操作禁止验证
|
||||
return !in_array(get_client_ip(), explode(',', $rule));
|
||||
return !in_array($_SERVER['REMOTE_ADDR'], explode(',', $rule));
|
||||
case 'filter': // 使用filter_var验证
|
||||
$result = filter_var($value, is_int($rule) ? $rule : filter_id($rule));
|
||||
return false === $result ? false : true;
|
||||
|
||||
@@ -20,11 +20,11 @@ return [
|
||||
// 别名定义
|
||||
'alias' => [
|
||||
'think\App' => CORE_PATH . 'app' . EXT,
|
||||
'think\Build' => CORE_PATH . 'build' . EXT,
|
||||
'think\Cache' => CORE_PATH . 'cache' . EXT,
|
||||
'think\Config' => CORE_PATH . 'config' . EXT,
|
||||
'think\Controller' => CORE_PATH . 'controller' . EXT,
|
||||
'think\Cookie' => CORE_PATH . 'cookie' . EXT,
|
||||
'think\Create' => CORE_PATH . 'create' . EXT,
|
||||
'think\Db' => CORE_PATH . 'db' . EXT,
|
||||
'think\Debug' => CORE_PATH . 'debug' . EXT,
|
||||
'think\Error' => CORE_PATH . 'error' . EXT,
|
||||
|
||||
@@ -43,9 +43,9 @@ if (APP_HOOK && isset($mode['tags'])) {
|
||||
}
|
||||
|
||||
// 自动生成
|
||||
if ('sae' != APP_MODE && is_file(APP_PATH . 'build.php')) {
|
||||
Create::build(include APP_PATH . 'build.php');
|
||||
if (APP_AUTO_BUILD && is_file(APP_PATH . 'build.php')) {
|
||||
Build::run(include APP_PATH . 'build.php');
|
||||
}
|
||||
|
||||
Loader::addNamespace('tests', TEST_PATH);
|
||||
// 执行应用
|
||||
!IN_UNIT_TEST && App::run(Config::get());
|
||||
!IN_UNIT_TEST && App::run();
|
||||
|
||||
Reference in New Issue
Block a user