This commit is contained in:
thinkphp
2015-12-23 10:38:07 +08:00
21 changed files with 223 additions and 133 deletions

View File

@@ -39,6 +39,7 @@ defined('APP_HOOK') or define('APP_HOOK', false); // 是否开启HOOK
defined('ENV_PREFIX') or define('ENV_PREFIX', 'T_'); // 环境变量的配置前缀 defined('ENV_PREFIX') or define('ENV_PREFIX', 'T_'); // 环境变量的配置前缀
defined('IS_API') or define('IS_API', false); // 是否API接口 defined('IS_API') or define('IS_API', false); // 是否API接口
defined('IN_UNIT_TEST') or define('IN_UNIT_TEST', false); // 是否为单元测试 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'); 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 = '') 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 = '') function C($name = '', $value = null, $range = '')
{ {
if (is_null($value) && is_string($name)) { if (is_null($value) && is_string($name)) {
return think\Config::get($name, $range); return \think\Config::get($name, $range);
} else { } 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'; $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) function G($start, $end = '', $dec = 6)
{ {
if ('' == $end) { if ('' == $end) {
think\Debug::remark($start); \think\Debug::remark($start);
} else { } 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 = '') 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) 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) 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) 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) 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) 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) 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) 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) 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) 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 = []) 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) 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 = '') function session($name, $value = '')
{ {
if (is_array($name)) { if (is_array($name)) {
// 初始化 // 初始化
think\Session::init($name); \think\Session::init($name);
} elseif (is_null($name)) { } elseif (is_null($name)) {
// 清除 // 清除
think\Session::clear($value); \think\Session::clear($value);
} elseif ('' === $value) { } elseif ('' === $value) {
// 获取 // 获取
return think\Session::get($name); return \think\Session::get($name);
} elseif (is_null($value)) { } elseif (is_null($value)) {
// 删除session // 删除session
return think\Session::delete($name); return \think\Session::delete($name);
} else { } else {
// 设置session // 设置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)) { if (is_array($name)) {
// 初始化 // 初始化
think\Cookie::init($name); \think\Cookie::init($name);
} elseif (is_null($name)) { } elseif (is_null($name)) {
// 清除 // 清除
think\Cookie::clear($value); \think\Cookie::clear($value);
} elseif ('' === $value) { } elseif ('' === $value) {
// 获取 // 获取
return think\Cookie::get($name); return \think\Cookie::get($name);
} elseif (is_null($value)) { } elseif (is_null($value)) {
// 删除session // 删除session
return think\Cookie::delete($name); return \think\Cookie::delete($name);
} else { } else {
// 设置session // 设置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)) { if (is_array($options)) {
// 缓存操作的同时初始化 // 缓存操作的同时初始化
think\Cache::connect($options); \think\Cache::connect($options);
} elseif (is_array($name)) { } elseif (is_array($name)) {
// 缓存初始化 // 缓存初始化
return think\Cache::connect($name); return \think\Cache::connect($name);
} }
if ('' === $value) { if ('' === $value) {
// 获取缓存 // 获取缓存
return think\Cache::get($name); return \think\Cache::get($name);
} elseif (is_null($value)) { } elseif (is_null($value)) {
// 删除缓存 // 删除缓存
return think\Cache::rm($name); return \think\Cache::rm($name);
} else { } else {
// 缓存数据 // 缓存数据
if (is_array($options)) { if (is_array($options)) {
@@ -301,7 +302,7 @@ function S($name, $value = '', $options = null)
} else { } else {
$expire = is_numeric($options) ? $options : null; //默认快捷缓存设置过期时间 $expire = is_numeric($options) ? $options : null; //默认快捷缓存设置过期时间
} }
return think\Cache::set($name, $value, $expire); return \think\Cache::set($name, $value, $expire);
} }
} }

View File

@@ -13,8 +13,8 @@ return [
'default_return_type' => 'html', 'default_return_type' => 'html',
// 默认语言 // 默认语言
'default_lang' => 'zh-cn', 'default_lang' => 'zh-cn',
// response输出终止执行 // response是否返回方式
'response_exit' => true, 'response_return' => false,
// 默认AJAX 数据返回格式,可选JSON XML ... // 默认AJAX 数据返回格式,可选JSON XML ...
'default_ajax_return' => 'JSON', 'default_ajax_return' => 'JSON',
// 默认JSONP格式返回的处理方法 // 默认JSONP格式返回的处理方法

View File

@@ -47,7 +47,7 @@ class Tencent extends Driver
'oauth_consumer_key' => $this->AppKey, 'oauth_consumer_key' => $this->AppKey,
'access_token' => $this->token['access_token'], 'access_token' => $this->token['access_token'],
'openid' => $this->openid(), 'openid' => $this->openid(),
'clientip' => get_client_ip(), 'clientip' => $_SERVER['REMOTE_ADDR'],
'oauth_version' => '2.a', 'oauth_version' => '2.a',
'scope' => 'all', 'scope' => 'all',
'format' => 'json', 'format' => 'json',

View File

@@ -23,21 +23,21 @@ class App
* @access public * @access public
* @return void * @return void
*/ */
public static function run(array $config = []) public static function run()
{ {
// 初始化公共模块 // 初始化公共模块
self::initModule(COMMON_MODULE, $config); self::initModule(COMMON_MODULE, Config::get());
// 获取配置参数
$config = Config::get();
// 读取扩展配置文件 // 读取扩展配置文件
if ($config['extra_config_list']) { if (Config::get('extra_config_list')) {
foreach ($config['extra_config_list'] as $file) { foreach (Config::get('extra_config_list') as $file) {
Config::load($file, $file); Config::load($file, $file);
} }
} }
// 获取配置参数
$config = Config::get();
// 日志初始化 // 日志初始化
Log::init($config['log']); Log::init($config['log']);
// 缓存初始化 // 缓存初始化
@@ -110,12 +110,8 @@ class App
} }
// 操作方法执行完成监听 // 操作方法执行完成监听
APP_HOOK && Hook::listen('action_end', $data); APP_HOOK && Hook::listen('action_end', $data);
// 返回数据 // 输出数据
if (IN_UNIT_TEST) { return Response::send($data, '', Config::get('response_return'));
return $data;
} else {
Response::returnData($data, Config::get('default_return_type'), Config::get('response_exit'));
}
} else { } else {
// 操作方法不是Public 抛出异常 // 操作方法不是Public 抛出异常
throw new \ReflectionException(); throw new \ReflectionException();
@@ -127,12 +123,8 @@ class App
$data = $method->invokeArgs($instance, [$action, '']); $data = $method->invokeArgs($instance, [$action, '']);
// 操作方法执行完成监听 // 操作方法执行完成监听
APP_HOOK && Hook::listen('action_end', $data); APP_HOOK && Hook::listen('action_end', $data);
// 返回数据 // 输出数据
if (IN_UNIT_TEST) { return Response::send($data, '', Config::get('response_return'));
return $data;
} else {
Response::returnData($data, Config::get('default_return_type'), Config::get('response_exit'));
}
} else { } else {
throw new Exception('method [ ' . (new \ReflectionClass($instance))->getName() . '->' . $action . ' ] not exists ', 10002); 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 (!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_MODULE') && !empty($match[0])) && define('BIND_MODULE', $match[0]);
(!defined('BIND_CONTROLLER') && !empty($match[1])) && define('BIND_CONTROLLER', $match[1]); (!defined('BIND_CONTROLLER') && !empty($match[1])) && define('BIND_CONTROLLER', $match[1]);
(!defined('BIND_ACTION') && !empty($match[2])) && define('BIND_ACTION', $match[2]); (!defined('BIND_ACTION') && !empty($match[2])) && define('BIND_ACTION', $match[2]);
@@ -300,7 +295,7 @@ class App
// 路由检测 // 路由检测
if (!empty($config['url_route_on'])) { if (!empty($config['url_route_on'])) {
// 开启路由 则检测路由配置 // 开启路由 则检测路由配置
Route::register($config['route']); Route::register(!empty($config['route']) ? $config['route'] : null);
$result = Route::check($path_info, $depr); $result = Route::check($path_info, $depr);
if (false === $result) { if (false === $result) {
// 路由无效 // 路由无效

View File

@@ -8,15 +8,14 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com> // | Author: liu21st <liu21st@gmail.com>
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace think; 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)) { if (is_writable($lockfile)) {
return; return;
} else { } else {

View File

@@ -25,7 +25,9 @@ class Apc
'prefix' => '', 'prefix' => '',
'length' => 0, 'length' => 0,
]; ];
/*****************************
需要支持apc_cli模式
******************************/
/** /**
* 架构函数 * 架构函数
* *

View File

@@ -34,7 +34,7 @@ abstract class Hprose
} }
//导入类库 //导入类库
think\Loader::import('vendor.Hprose.HproseHttpServer'); \think\Loader::import('vendor.Hprose.HproseHttpServer');
//实例化HproseHttpServer //实例化HproseHttpServer
$server = new \HproseHttpServer(); $server = new \HproseHttpServer();
if ($this->allowMethodList) { if ($this->allowMethodList) {

View File

@@ -28,7 +28,7 @@ abstract class Jsonrpc
} }
//导入类库 //导入类库
think\Loader::import('vendor.jsonrpc.jsonRPCServer'); \think\Loader::import('vendor.jsonrpc.jsonRPCServer');
// 启动server // 启动server
\jsonRPCServer::handle($this); \jsonRPCServer::handle($this);
} }

View File

@@ -93,7 +93,10 @@ abstract class Rest
protected function response($data, $type = '', $code = 200) protected function response($data, $type = '', $code = 200)
{ {
Response::sendHttpStatus($code); Response::sendHttpStatus($code);
Response::returnData($data, strtolower($type)); Response::data($data);
if ($type) {
Response::type($type);
}
} }
/** /**

View File

@@ -31,7 +31,7 @@ abstract class Rpc
} }
//导入类库 //导入类库
think\Loader::import('vendor.phprpc.phprpc_server'); \think\Loader::import('vendor.phprpc.phprpc_server');
//实例化phprpc //实例化phprpc
$server = new \PHPRPC_Server(); $server = new \PHPRPC_Server();
if ($this->allowMethodList) { if ($this->allowMethodList) {

View File

@@ -1148,7 +1148,7 @@ abstract class Driver
*/ */
protected function debug($start) protected function debug($start)
{ {
if ($this->config['debug']) { if (!empty($this->config['debug'])) {
// 开启数据库调试模式 // 开启数据库调试模式
if ($start) { if ($start) {
Debug::remark('queryStartTime', 'time'); Debug::remark('queryStartTime', 'time');

View File

@@ -61,7 +61,7 @@ class Mongo extends Driver
* @return * @return
* @throws Exception * @throws Exception
*/ */
public function connect($config = '', $linkNum = 0) public function connect($config = '', $linkNum = 0, $autoConnection = false)
{ {
if (!isset($this->linkID[$linkNum])) { if (!isset($this->linkID[$linkNum])) {
if (empty($config)) { 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']}" : ''); $host = 'mongodb://' . ($config['username'] ? "{$config['username']}" : '') . ($config['password'] ? ":{$config['password']}@" : '') . $config['hostname'] . ($config['hostport'] ? ":{$config['hostport']}" : '') . '/' . ($config['database'] ? "{$config['database']}" : '');
try { 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) { } catch (\MongoConnectionException $e) {
throw new Exception($e->getmessage()); throw new Exception($e->getmessage());
} }
@@ -105,7 +105,7 @@ class Mongo extends Driver
$this->_mongo = $this->_linkID->selectDb($db); $this->_mongo = $this->_linkID->selectDb($db);
} }
// 当前MongoCollection对象 // 当前MongoCollection对象
if ($this->config['debug']) { if (!empty($this->config['debug'])) {
$this->queryStr = $this->_dbName . '.getCollection(' . $collection . ')'; $this->queryStr = $this->_dbName . '.getCollection(' . $collection . ')';
} }
if ($this->_collectionName != $collection) { if ($this->_collectionName != $collection) {
@@ -249,7 +249,7 @@ class Mongo extends Driver
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function insertAll($dataList, $options = []) public function insertAll($dataList, $options = [], $replace = false)
{ {
if (isset($options['table'])) { if (isset($options['table'])) {
$this->switchCollection($options['table']); $this->switchCollection($options['table']);
@@ -483,9 +483,9 @@ class Mongo extends Driver
} }
$this->model = $options['model']; $this->model = $options['model'];
$this->queryTimes++; $this->queryTimes++;
$query = $this->parseWhere($options['where']); $query = $this->parseWhere(!empty($options['where'])?$options['where']:'');
$fields = $this->parseField($options['field']); $fields = $this->parseField(!empty($options['field'])?$options['field']:'');
if ($this->config['debug']) { if (!empty($this->config['debug'])) {
$this->queryStr = $this->_dbName . '.' . $this->_collectionName . '.findOne('; $this->queryStr = $this->_dbName . '.' . $this->_collectionName . '.findOne(';
$this->queryStr .= $query ? json_encode($query) : '{}'; $this->queryStr .= $query ? json_encode($query) : '{}';
$this->queryStr .= $fields ? ',' . json_encode($fields) : ''; $this->queryStr .= $fields ? ',' . json_encode($fields) : '';
@@ -519,7 +519,7 @@ class Mongo extends Driver
$this->model = $options['model']; $this->model = $options['model'];
$this->queryTimes++; $this->queryTimes++;
$query = $this->parseWhere($options['where']); $query = $this->parseWhere($options['where']);
if ($this->config['debug']) { if (!empty($this->config['debug'])) {
$this->queryStr = $this->_dbName . '.' . $this->_collectionName; $this->queryStr = $this->_dbName . '.' . $this->_collectionName;
$this->queryStr .= $query ? '.find(' . json_encode($query) . ')' : ''; $this->queryStr .= $query ? '.find(' . json_encode($query) . ')' : '';
$this->queryStr .= '.count()'; $this->queryStr .= '.count()';

View File

@@ -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) { 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']); array_walk($resultSet, [$this, 'checkMongoId']);
} }
@@ -185,7 +185,7 @@ class Mongo extends \Think\Model
* @param integer $step 增长值 * @param integer $step 增长值
* @return boolean * @return boolean
*/ */
public function setInc($field, $step = 1) public function setInc($field, $step = 1, $lazyTime = 0)
{ {
return $this->setField($field, ['inc', $step]); return $this->setField($field, ['inc', $step]);
} }
@@ -197,7 +197,7 @@ class Mongo extends \Think\Model
* @param integer $step 减少值 * @param integer $step 减少值
* @return boolean * @return boolean
*/ */
public function setDec($field, $step = 1) public function setDec($field, $step = 1, $lazyTime = 0)
{ {
return $this->setField($field, ['inc', '-' . $step]); return $this->setField($field, ['inc', '-' . $step]);
} }

View File

@@ -13,17 +13,27 @@ namespace think;
class Response class Response
{ {
// 输出数据的转换方法
protected static $tramsform = null;
// 输出数据的类型
protected static $type = 'html';
// 输出数据
protected static $data = '';
// 是否exit
protected static $isExit = false;
/** /**
* 返回数据到客户端 * 发送数据到客户端
* @access protected * @access protected
* @param mixed $data 要返回的数据 * @param mixed $data 要返回的数据
* @param String $type 返回数据格式 * @param String $type 返回数据格式
* @param bool $exit 是否终止执行 * @param bool $return 是否返回数据
* @return void * @return void
*/ */
public static function returnData($data, $type = '', $exit = true) public static function send($data = '', $type = '', $return = false)
{ {
$type = strtolower($type ?: self::$type);
$headers = [ $headers = [
'json' => 'application/json', 'json' => 'application/json',
'xml' => 'text/xml', 'xml' => 'text/xml',
@@ -32,32 +42,86 @@ class Response
'script' => 'application/javascript', 'script' => 'application/javascript',
'text' => 'text/plain', 'text' => 'text/plain',
]; ];
$type = strtolower($type);
if (!headers_sent() && isset($headers[$type])) { if (!headers_sent() && isset($headers[$type])) {
header('Content-Type:' . $headers[$type] . '; charset=utf-8'); header('Content-Type:' . $headers[$type] . '; charset=utf-8');
} }
$data = $data ?: self::$data;
if (is_callable(self::$tramsform)) {
$data = call_user_func_array(self::$tramsform, [$data]);
} else {
switch ($type) { switch ($type) {
case 'json': case 'json':
// 返回JSON数据格式到客户端 包含状态信息 // 返回JSON数据格式到客户端 包含状态信息
$data = json_encode($data, JSON_UNESCAPED_UNICODE); $data = json_encode($data, JSON_UNESCAPED_UNICODE);
break; break;
case 'xml':
// 返回xml格式数据
$data = \org\Transform::xmlEncode($data);
break;
case 'jsonp': case 'jsonp':
// 返回JSON数据格式到客户端 包含状态信息 // 返回JSON数据格式到客户端 包含状态信息
$handler = !empty($_GET[Config::get('var_jsonp_handler')]) ? $_GET[Config::get('var_jsonp_handler')] : Config::get('default_jsonp_handler'); $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) . ');'; $data = $handler . '(' . json_encode($data, JSON_UNESCAPED_UNICODE) . ');';
break; break;
case '':
// 类型为空不做处理
break;
default:
// 用于扩展其他返回格式数据
Hook::listen('return_data', $data);
} }
//header('Content-Length:' . strlen($data)); }
if ($exit) {
exit($data); if ($return) {
return $data;
} else { } else {
echo $data; 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, 'time' => NOW_TIME,
'data' => $data, '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(); $view = new \think\View();
$result = $view->fetch(Config::get('dispatch_jump_tmpl'), $result); $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(); $view = new \think\View();
$result = $view->fetch(Config::get('dispatch_jump_tmpl'), $result); $result = $view->fetch(Config::get('dispatch_jump_tmpl'), $result);
} }
self::returnData($result, $type, true); self::$data = $result;
if ($type) {
self::$type = $type;
}
} }
/** /**

View File

@@ -34,32 +34,34 @@ class Route
// 添加URL映射规则 // 添加URL映射规则
public static function map($map, $route = '') public static function map($map, $route = '')
{ {
self::setting('map', $map, $route); return self::setting('map', $map, $route);
} }
// 添加变量规则 // 添加变量规则
public static function pattern($name, $rule = '') public static function pattern($name, $rule = '')
{ {
self::setting('pattern', $name, $rule); return self::setting('pattern', $name, $rule);
} }
// 添加路由别名 // 添加路由别名
public static function alias($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 = '') 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)) { if (is_array($name)) {
self::${$var} = array_merge(self::${$var}, $name); self::${$var} = array_merge(self::${$var}, $name);
} elseif (empty($name)) {
return self::${$var};
} else { } else {
self::${$var}[$name] = $value; self::${$var}[$name] = $value;
} }
@@ -309,7 +311,7 @@ class Route
$rule = array_shift($val); $rule = array_shift($val);
} }
// 单项路由 // 单项路由
$route = $val['route']; $route = !empty($val['route'])?$val['route']:'';
if (0 === strpos($rule, '/') && preg_match($rule, $url, $matches)) { if (0 === strpos($rule, '/') && preg_match($rule, $url, $matches)) {
return self::checkRegex($route, $url, $matches); return self::checkRegex($route, $url, $matches);
} else { } else {

View File

@@ -615,10 +615,14 @@ class Template
$vars = explode('.', $var); $vars = explode('.', $var);
$var = array_shift($vars); $var = array_shift($vars);
$name = '$' . $var; $name = '$' . $var;
if (count($vars) > 1) {
foreach ($vars as $key => $val) { foreach ($vars as $key => $val) {
$name .= '["' . $val . '"]'; $name .= '["' . $val . '"]';
} }
} else {
// 一维自动识别对象和数组
$name = 'is_array($' . $var . ')?$' . $var . '["' . $vars[0] . '"]:$' . $var . '->' . $vars[0];
}
} elseif (false !== strpos($var, '[')) { } elseif (false !== strpos($var, '[')) {
//支持 {$var['key']} 方式输出数组 //支持 {$var['key']} 方式输出数组
$name = "$" . $var; $name = "$" . $var;

View File

@@ -134,14 +134,15 @@ class Url
$domain = $host . (strpos($host, '.') ? '' : strstr($_SERVER['HTTP_HOST'], '.')); $domain = $host . (strpos($host, '.') ? '' : strstr($_SERVER['HTTP_HOST'], '.'));
} elseif (true === $domain) { } elseif (true === $domain) {
$domain = $_SERVER['HTTP_HOST']; $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'], '.'); $domain = 'localhost' == $domain ? 'localhost' : 'www' . strstr($_SERVER['HTTP_HOST'], '.');
// '子域名'=>array('项目[/分组]'); // '子域名'=>['模块[/控制器/操作]'];
foreach (Config::get('url_domain_rules') as $key => $rule) { foreach (Route::domain() as $key => $rule) {
if (false === strpos($key, '*') && 0 === strpos($url, $rule[0])) { $rule = is_array($rule) ? $rule[0] : $rule;
if (false === strpos($key, '*') && 0 === strpos($url, $rule)) {
$domain = $key . strstr($domain, '.'); // 生成对应子域名 $domain = $key . strstr($domain, '.'); // 生成对应子域名
$url = substr_replace($url, '', 0, strlen($rule[0])); $url = substr_replace($url, '', 0, strlen($rule));
break; break;
} }
} }

View File

@@ -40,6 +40,14 @@ class View
$this->config($config); $this->config($config);
$this->engine($this->config['engine_type']); $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 { } else {
$this->data[$name] = $value; $this->data[$name] = $value;
} }
return $this;
} }
/** /**

View File

@@ -361,9 +361,9 @@ trait Auto
} }
return NOW_TIME >= $start && NOW_TIME <= $end; return NOW_TIME >= $start && NOW_TIME <= $end;
case 'ip_allow': // IP 操作许可验证 case 'ip_allow': // IP 操作许可验证
return in_array(get_client_ip(), explode(',', $rule)); return in_array($_SERVER['REMOTE_ADDR'], explode(',', $rule));
case 'ip_deny': // IP 操作禁止验证 case 'ip_deny': // IP 操作禁止验证
return !in_array(get_client_ip(), explode(',', $rule)); return !in_array($_SERVER['REMOTE_ADDR'], explode(',', $rule));
case 'filter': // 使用filter_var验证 case 'filter': // 使用filter_var验证
$result = filter_var($value, is_int($rule) ? $rule : filter_id($rule)); $result = filter_var($value, is_int($rule) ? $rule : filter_id($rule));
return false === $result ? false : true; return false === $result ? false : true;

View File

@@ -20,11 +20,11 @@ return [
// 别名定义 // 别名定义
'alias' => [ 'alias' => [
'think\App' => CORE_PATH . 'app' . EXT, 'think\App' => CORE_PATH . 'app' . EXT,
'think\Build' => CORE_PATH . 'build' . EXT,
'think\Cache' => CORE_PATH . 'cache' . EXT, 'think\Cache' => CORE_PATH . 'cache' . EXT,
'think\Config' => CORE_PATH . 'config' . EXT, 'think\Config' => CORE_PATH . 'config' . EXT,
'think\Controller' => CORE_PATH . 'controller' . EXT, 'think\Controller' => CORE_PATH . 'controller' . EXT,
'think\Cookie' => CORE_PATH . 'cookie' . EXT, 'think\Cookie' => CORE_PATH . 'cookie' . EXT,
'think\Create' => CORE_PATH . 'create' . EXT,
'think\Db' => CORE_PATH . 'db' . EXT, 'think\Db' => CORE_PATH . 'db' . EXT,
'think\Debug' => CORE_PATH . 'debug' . EXT, 'think\Debug' => CORE_PATH . 'debug' . EXT,
'think\Error' => CORE_PATH . 'error' . EXT, 'think\Error' => CORE_PATH . 'error' . EXT,

View File

@@ -43,9 +43,9 @@ if (APP_HOOK && isset($mode['tags'])) {
} }
// 自动生成 // 自动生成
if ('sae' != APP_MODE && is_file(APP_PATH . 'build.php')) { if (APP_AUTO_BUILD && is_file(APP_PATH . 'build.php')) {
Create::build(include 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();