修正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;
self::isExit() && exit();
}
@@ -94,7 +97,10 @@ class Response
*/
public static function type($type = null)
{
if(is_null($type)) return self::$type;
if (is_null($type)) {
return self::$type;
}
self::$type = $type;
}
@@ -117,7 +123,10 @@ class Response
*/
public static function isExit($exit = null)
{
if(is_null($exit)) return self::$isExit;
if (is_null($exit)) {
return self::$isExit;
}
self::$isExit = (boolean) $exit;
}
@@ -138,17 +147,20 @@ class Response
'time' => NOW_TIME,
'data' => $data,
];
if($type) self::type($type);
if ($type) {
self::type($type);
}
return $result;
}
/**
* 返回封装后的API数据到客户端
* 操作成功跳转的快捷方法
* @access public
* @param mixed $msg 要返回的数据
* @param mixed $data 返回的code
* @param mixed $url 提示信息
* @param mixed $wait 返回数据格式
* @param mixed $msg 提示信息
* @param mixed $data 返回的数据
* @param mixed $url 跳转的URL地址
* @param mixed $wait 跳转等待时间
* @return void
*/
public static function success($msg = '', $data = '', $url = '', $wait = 3)
@@ -160,9 +172,9 @@ class Response
'url' => $url ?: $_SERVER["HTTP_REFERER"],
'wait' => $wait,
];
$type = Config::get('default_return_type');
if(IS_AJAX){
$type = Config::get('default_ajax_return');
$type = Config::get('default_return_type');
if (IS_AJAX) {
$type = Config::get('default_ajax_return');
}
if ('html' == $type) {
$result = \think\View::getInstance()->fetch(Config::get('dispatch_jump_tmpl'), $result);
@@ -172,12 +184,12 @@ class Response
}
/**
* 返回封装后的API数据到客户端
* 操作错误跳转的快捷方法
* @access public
* @param mixed $msg 要返回的数据
* @param mixed $data 返回的code
* @param mixed $url 提示信息
* @param mixed $wait 返回数据格式
* @param mixed $msg 提示信息
* @param mixed $data 返回的数据
* @param mixed $url 跳转的URL地址
* @param mixed $wait 跳转等待时间
* @return void
*/
public static function error($msg = '', $data = '', $url = '', $wait = 3)
@@ -189,9 +201,9 @@ class Response
'url' => $url ?: 'javascript:history.back(-1);',
'wait' => $wait,
];
$type = Config::get('default_return_type');
if(IS_AJAX){
$type = Config::get('default_ajax_return');
$type = Config::get('default_return_type');
if (IS_AJAX) {
$type = Config::get('default_ajax_return');
}
if ('html' == $type) {
$result = \think\View::getInstance()->fetch(Config::get('dispatch_jump_tmpl'), $result);
@@ -209,12 +221,12 @@ class Response
*/
public static function redirect($url, $params = [])
{
$http_response_code = 301;
if(in_array($params, [301, 302])){
$http_response_code = 301;
if (in_array($params, [301, 302])) {
$http_response_code = $params;
$params = [];
}
$url = preg_match('/^(https?:|\/)/', $url) ? $url : Url::build($url, $params);
$url = preg_match('/^(https?:|\/)/', $url) ? $url : Url::build($url, $params);
header('Location: ' . $url, true, $http_response_code);
}

View File

@@ -118,6 +118,8 @@ class Template
{
if (is_array($config)) {
$this->config = array_merge($this->config, $config);
} elseif (isset($this->config[$config])) {
return $this->config[$config];
}
}
@@ -401,7 +403,7 @@ class Template
private function parseExtend(&$content)
{
$regex = $this->getRegex('extend');
$array = $blocks = $extBlocks = [];
$array = $blocks = $extBlocks = [];
$extend = '';
$fun = function ($template) use (&$fun, &$regex, &$array, &$extend, &$blocks, &$extBlocks) {
if (preg_match($regex, $template, $matches)) {
@@ -497,9 +499,9 @@ class Template
}
} else {
$right[] = [
'name' => $match[2][0],
'name' => $match[2][0],
'offset' => $match[0][1],
'tag' => $match[0][0],
'tag' => $match[0][0],
];
}
}
@@ -588,7 +590,7 @@ class Template
$str = stripslashes($match[1]);
$flag = substr($str, 0, 1);
switch ($flag) {
case '$': // 解析模板变量 格式 {$varName}
case '$': // 解析模板变量 格式 {$varName}
$this->parseVar($str);
// 是否带有?号
if (false !== $pos = strpos($str, '?')) {
@@ -601,8 +603,8 @@ class Template
if (isset($array[1])) {
// XXX: 加入这句原本是为解决变量末声明的问题,但$name中是多个条件时会解析错误故注释掉
/*if (strpos($name, '[')) {
$name = 'isset(' . $name . ') && ' . $name;
}*/
$name = 'isset(' . $name . ') && ' . $name;
}*/
$name .= $array[1] . trim($array[2]);
if ('=' == $first) {
// {$varname?='xxx'} $varname为真时才输出xxx
@@ -612,19 +614,19 @@ class Template
}
} else {
switch ($first) {
case '?':
case '?':
// {$varname??'xxx'} $varname有定义则输出$varname,否则输出xxx
$str = '<?php echo isset(' . $name . ') ? ' . $name . ' : ' . substr($str, 1) . '; ?>';
break;
case '=':
case '=':
// {$varname?='xxx'} $varname为真时才输出xxx
$str = '<?php if(!empty(' . $name . ')) echo ' . substr($str, 1) . '; ?>';
break;
case ':':
case ':':
// {$varname?:'xxx'} $varname为真时输出$varname,否则输出xxx
$str = '<?php echo !empty(' . $name . ') ? ' . $name . $str . '; ?>';
break;
default:
default:
if (strpos($str, ':')) {
// {$varname ? 'a' : 'b'} $varname为真时输出a,否则输出b
$str = '<?php echo !empty(' . $name . ') ? ' . $str . '; ?>';
@@ -638,22 +640,22 @@ class Template
$str = '<?php echo ' . $str . '; ?>';
}
break;
case ':': // 输出某个函数的结果
case ':': // 输出某个函数的结果
$str = substr($str, 1);
$this->parseVar($str);
$str = '<?php echo ' . $str . '; ?>';
break;
case '~': // 执行某个函数
case '~': // 执行某个函数
$str = substr($str, 1);
$str = '<?php ' . $str . '; ?>';
break;
case '-':
case '+': // 输出计算
case '+': // 输出计算
$str = '<?php echo ' . $str . '; ?>';
break;
case '/': // 注释标签
case '/': // 注释标签
$flag2 = substr($str, 1, 1);
if ($flag2 == '/' || ($flag2 == '*' && substr(rtrim($str), -2) == '*/')) {
if ('/' == $flag2 || ('*' == $flag2 && substr(rtrim($str), -2) == '*/')) {
$str = '';
}
break;
@@ -690,7 +692,7 @@ class Template
if (strpos($match[0], '.')) {
$vars = explode('.', $match[0]);
$first = array_shift($vars);
if ($first == '$Think') {
if ('$Think' == $first) {
// 所有以Think.打头的以特殊变量对待 无需模板赋值就可以输出
$parseStr = $this->parseThinkVar($vars);
} else {
@@ -738,10 +740,10 @@ class Template
// 模板函数过滤
$fun = trim($args[0]);
switch ($fun) {
case 'default': // 特殊模板函数
case 'default': // 特殊模板函数
$varStr = '(isset(' . $name . ') && (' . $name . ' !== \'\'))?(' . $name . '):' . $args[1];
break;
default: // 通用模板函数
default: // 通用模板函数
if (!in_array($fun, $template_deny_funs)) {
if (isset($args[1])) {
if (strstr($args[1], '###')) {

View File

@@ -108,7 +108,7 @@ class TagLib
if (preg_match_all($regex, $content, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE)) {
$right = [];
foreach ($matches as $match) {
if ($match[1][0] == '') {
if ('' == $match[1][0]) {
$name = $match[2][0];
// 如果有没闭合的标签头则取出最后一个
if (!empty($right[$name])) {
@@ -116,7 +116,7 @@ class TagLib
$nodes[$match[0][1]] = [
'name' => $name,
'begin' => array_pop($right[$name]), // 标签开始符
'end' => $match[0] // 标签结束符
'end' => $match[0], // 标签结束符
];
} else {
continue;
@@ -176,7 +176,7 @@ class TagLib
$content = preg_replace_callback($regex, function ($matches) use (&$tags, &$self) {
$name = $tags[0][$matches[1]];
// 解析标签属性
$attrs = $self->parseAttr($matches[0], $name);
$attrs = $self->parseAttr($matches[0], $name);
$method = '_' . $name;
return $self->$method($attrs, '', $matches[1]);
}, $content);
@@ -193,8 +193,8 @@ class TagLib
*/
private function getRegex($tags, $close)
{
$begin = $this->tpl->config['taglib_begin'];
$end = $this->tpl->config['taglib_end'];
$begin = $this->tpl->config('taglib_begin');
$end = $this->tpl->config('taglib_end');
$single = strlen(ltrim($begin, '\\')) == 1 && strlen(ltrim($end, '\\')) == 1 ? true : false;
if (is_array($tags)) {
$tagName = implode('|', $tags);
@@ -244,7 +244,7 @@ class TagLib
if (!$xml) {
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'])) {
$tag = strtolower($tag);
if (!isset($this->tags[$tag])) {
@@ -322,8 +322,8 @@ class TagLib
if (!empty($this->tags[$tag]['expression'])) {
static $_taglibs;
if (!isset($_taglibs[$tag])) {
$_taglibs[$tag][0] = strlen($this->tpl->config['taglib_begin'] . $tag);
$_taglibs[$tag][1] = strlen($this->tpl->config['taglib_end']);
$_taglibs[$tag][0] = strlen($this->tpl->config('taglib_begin') . $tag);
$_taglibs[$tag][1] = strlen($this->tpl->config('taglib_end'));
}
$str = substr($str, $_taglibs[$tag][0], -$_taglibs[$tag][1]);
$result['expression'] = trim($str);
@@ -360,7 +360,8 @@ class TagLib
if (':' == $flag) {
// 以:开头为函数调用,解析前去掉:
$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)) {
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);
}
}