修正taglib类的一处配置调用错误 增加 traits\controller\response

This commit is contained in:
thinkphp
2015-12-24 11:16:16 +08:00
parent 1bf0b2361c
commit 1b61dab806
4 changed files with 127 additions and 50 deletions

View File

@@ -70,7 +70,10 @@ class Response
} }
} }
if ($return) return $data; if ($return) {
return $data;
}
echo $data; echo $data;
self::isExit() && exit(); self::isExit() && exit();
} }
@@ -94,7 +97,10 @@ class Response
*/ */
public static function type($type = null) public static function type($type = null)
{ {
if(is_null($type)) return self::$type; if (is_null($type)) {
return self::$type;
}
self::$type = $type; self::$type = $type;
} }
@@ -117,7 +123,10 @@ class Response
*/ */
public static function isExit($exit = null) public static function isExit($exit = null)
{ {
if(is_null($exit)) return self::$isExit; if (is_null($exit)) {
return self::$isExit;
}
self::$isExit = (boolean) $exit; self::$isExit = (boolean) $exit;
} }
@@ -138,17 +147,20 @@ class Response
'time' => NOW_TIME, 'time' => NOW_TIME,
'data' => $data, 'data' => $data,
]; ];
if($type) self::type($type); if ($type) {
self::type($type);
}
return $result; return $result;
} }
/** /**
* 返回封装后的API数据到客户端 * 操作成功跳转的快捷方法
* @access public * @access public
* @param mixed $msg 要返回的数据 * @param mixed $msg 提示信息
* @param mixed $data 返回的code * @param mixed $data 返回的数据
* @param mixed $url 提示信息 * @param mixed $url 跳转的URL地址
* @param mixed $wait 返回数据格式 * @param mixed $wait 跳转等待时间
* @return void * @return void
*/ */
public static function success($msg = '', $data = '', $url = '', $wait = 3) public static function success($msg = '', $data = '', $url = '', $wait = 3)
@@ -161,7 +173,7 @@ class Response
'wait' => $wait, 'wait' => $wait,
]; ];
$type = Config::get('default_return_type'); $type = Config::get('default_return_type');
if(IS_AJAX){ if (IS_AJAX) {
$type = Config::get('default_ajax_return'); $type = Config::get('default_ajax_return');
} }
if ('html' == $type) { if ('html' == $type) {
@@ -172,12 +184,12 @@ class Response
} }
/** /**
* 返回封装后的API数据到客户端 * 操作错误跳转的快捷方法
* @access public * @access public
* @param mixed $msg 要返回的数据 * @param mixed $msg 提示信息
* @param mixed $data 返回的code * @param mixed $data 返回的数据
* @param mixed $url 提示信息 * @param mixed $url 跳转的URL地址
* @param mixed $wait 返回数据格式 * @param mixed $wait 跳转等待时间
* @return void * @return void
*/ */
public static function error($msg = '', $data = '', $url = '', $wait = 3) public static function error($msg = '', $data = '', $url = '', $wait = 3)
@@ -190,7 +202,7 @@ class Response
'wait' => $wait, 'wait' => $wait,
]; ];
$type = Config::get('default_return_type'); $type = Config::get('default_return_type');
if(IS_AJAX){ if (IS_AJAX) {
$type = Config::get('default_ajax_return'); $type = Config::get('default_ajax_return');
} }
if ('html' == $type) { if ('html' == $type) {
@@ -210,7 +222,7 @@ class Response
public static function redirect($url, $params = []) public static function redirect($url, $params = [])
{ {
$http_response_code = 301; $http_response_code = 301;
if(in_array($params, [301, 302])){ if (in_array($params, [301, 302])) {
$http_response_code = $params; $http_response_code = $params;
$params = []; $params = [];
} }

View File

@@ -118,6 +118,8 @@ class Template
{ {
if (is_array($config)) { if (is_array($config)) {
$this->config = array_merge($this->config, $config); $this->config = array_merge($this->config, $config);
} elseif (isset($this->config[$config])) {
return $this->config[$config];
} }
} }
@@ -653,7 +655,7 @@ class Template
break; break;
case '/': // 注释标签 case '/': // 注释标签
$flag2 = substr($str, 1, 1); $flag2 = substr($str, 1, 1);
if ($flag2 == '/' || ($flag2 == '*' && substr(rtrim($str), -2) == '*/')) { if ('/' == $flag2 || ('*' == $flag2 && substr(rtrim($str), -2) == '*/')) {
$str = ''; $str = '';
} }
break; break;
@@ -690,7 +692,7 @@ class Template
if (strpos($match[0], '.')) { if (strpos($match[0], '.')) {
$vars = explode('.', $match[0]); $vars = explode('.', $match[0]);
$first = array_shift($vars); $first = array_shift($vars);
if ($first == '$Think') { if ('$Think' == $first) {
// 所有以Think.打头的以特殊变量对待 无需模板赋值就可以输出 // 所有以Think.打头的以特殊变量对待 无需模板赋值就可以输出
$parseStr = $this->parseThinkVar($vars); $parseStr = $this->parseThinkVar($vars);
} else { } else {

View File

@@ -108,7 +108,7 @@ class TagLib
if (preg_match_all($regex, $content, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE)) { if (preg_match_all($regex, $content, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE)) {
$right = []; $right = [];
foreach ($matches as $match) { foreach ($matches as $match) {
if ($match[1][0] == '') { if ('' == $match[1][0]) {
$name = $match[2][0]; $name = $match[2][0];
// 如果有没闭合的标签头则取出最后一个 // 如果有没闭合的标签头则取出最后一个
if (!empty($right[$name])) { if (!empty($right[$name])) {
@@ -116,7 +116,7 @@ class TagLib
$nodes[$match[0][1]] = [ $nodes[$match[0][1]] = [
'name' => $name, 'name' => $name,
'begin' => array_pop($right[$name]), // 标签开始符 'begin' => array_pop($right[$name]), // 标签开始符
'end' => $match[0] // 标签结束符 'end' => $match[0], // 标签结束符
]; ];
} else { } else {
continue; continue;
@@ -193,8 +193,8 @@ class TagLib
*/ */
private function getRegex($tags, $close) private function getRegex($tags, $close)
{ {
$begin = $this->tpl->config['taglib_begin']; $begin = $this->tpl->config('taglib_begin');
$end = $this->tpl->config['taglib_end']; $end = $this->tpl->config('taglib_end');
$single = strlen(ltrim($begin, '\\')) == 1 && strlen(ltrim($end, '\\')) == 1 ? true : false; $single = strlen(ltrim($begin, '\\')) == 1 && strlen(ltrim($end, '\\')) == 1 ? true : false;
if (is_array($tags)) { if (is_array($tags)) {
$tagName = implode('|', $tags); $tagName = implode('|', $tags);
@@ -244,7 +244,7 @@ class TagLib
if (!$xml) { if (!$xml) {
throw new Exception('_XML_TAG_ERROR_ : ' . $attr); throw new Exception('_XML_TAG_ERROR_ : ' . $attr);
} }
$xml = (array)($xml->tag->attributes()); $xml = (array) ($xml->tag->attributes());
if (isset($xml['@attributes']) && $result = array_change_key_case($xml['@attributes'])) { if (isset($xml['@attributes']) && $result = array_change_key_case($xml['@attributes'])) {
$tag = strtolower($tag); $tag = strtolower($tag);
if (!isset($this->tags[$tag])) { if (!isset($this->tags[$tag])) {
@@ -322,8 +322,8 @@ class TagLib
if (!empty($this->tags[$tag]['expression'])) { if (!empty($this->tags[$tag]['expression'])) {
static $_taglibs; static $_taglibs;
if (!isset($_taglibs[$tag])) { if (!isset($_taglibs[$tag])) {
$_taglibs[$tag][0] = strlen($this->tpl->config['taglib_begin'] . $tag); $_taglibs[$tag][0] = strlen($this->tpl->config('taglib_begin') . $tag);
$_taglibs[$tag][1] = strlen($this->tpl->config['taglib_end']); $_taglibs[$tag][1] = strlen($this->tpl->config('taglib_end'));
} }
$str = substr($str, $_taglibs[$tag][0], -$_taglibs[$tag][1]); $str = substr($str, $_taglibs[$tag][0], -$_taglibs[$tag][1]);
$result['expression'] = trim($str); $result['expression'] = trim($str);
@@ -360,7 +360,8 @@ class TagLib
if (':' == $flag) { if (':' == $flag) {
// 以:开头为函数调用,解析前去掉: // 以:开头为函数调用,解析前去掉:
$name = substr($name, 1); $name = substr($name, 1);
} elseif ('$' != $flag && preg_match('/[a-zA-Z_]/', $flag)) { // XXX: 这句的写法可能还需要改进 } elseif ('$' != $flag && preg_match('/[a-zA-Z_]/', $flag)) {
// XXX: 这句的写法可能还需要改进
// 常量不需要解析 // 常量不需要解析
if (defined($name)) { if (defined($name)) {
return $name; return $name;

View File

@@ -0,0 +1,62 @@
<?php
/**
* 用法:
* T('controller/response');
* class index
* {
* use \traits\controller\response;
* public function index(){
* $this->error();
* $this->redirect();
* }
* }
*/
namespace traits\controller;
trait Response
{
// 视图类实例
protected $view = null;
/**
* 操作错误跳转的快捷方法
* @access public
* @param mixed $msg 提示信息
* @param mixed $data 返回的数据
* @param mixed $url 跳转的URL地址
* @param mixed $wait 跳转等待时间
* @return void
*/
public function error($msg = '', $data = '', $url = '', $wait = 3)
{
return Response::error($msg, $data, $url, $wait);
}
/**
* 操作成功跳转的快捷方法
* @access public
* @param mixed $msg 提示信息
* @param mixed $data 返回的数据
* @param mixed $url 跳转的URL地址
* @param mixed $wait 跳转等待时间
* @return void
*/
public function success($msg = '', $data = '', $url = '', $wait = 3)
{
return Response::success($msg, $data, $url, $wait);
}
/**
* URL重定向
* @access protected
* @param string $url 跳转的URL表达式
* @param array|int $params 其它URL参数或http code
* @return void
*/
public function redirect($url, $params = [])
{
return Response::redirect($url, $params);
}
}