View类优化

This commit is contained in:
thinkphp
2016-03-14 14:02:37 +08:00
parent 2a9fb524a8
commit 06ba891ca4
3 changed files with 18 additions and 53 deletions

View File

@@ -712,31 +712,31 @@ class Template
} elseif (')' == substr($name, -1, 1)) { } elseif (')' == substr($name, -1, 1)) {
// $name为对象或是自动识别或者含有函数 // $name为对象或是自动识别或者含有函数
switch ($first) { switch ($first) {
case '?': case '?':
$str = '<?php echo ' . $name . ' ? ' . $name . ' : ' . substr($str, 1) . '; ?>'; $str = '<?php echo ' . $name . ' ? ' . $name . ' : ' . substr($str, 1) . '; ?>';
break; break;
case '=': case '=':
$str = '<?php if(' . $name . ') echo ' . substr($str, 1) . '; ?>'; $str = '<?php if(' . $name . ') echo ' . substr($str, 1) . '; ?>';
break; break;
default: default:
$str = '<?php echo ' . $name . '?' . $str . '; ?>'; $str = '<?php echo ' . $name . '?' . $str . '; ?>';
} }
} else { } else {
// $name为数组 // $name为数组
switch ($first) { switch ($first) {
case '?': case '?':
// {$varname??'xxx'} $varname有定义则输出$varname,否则输出xxx // {$varname??'xxx'} $varname有定义则输出$varname,否则输出xxx
$str = '<?php echo isset(' . $name . ') ? ' . $name . ' : ' . substr($str, 1) . '; ?>'; $str = '<?php echo isset(' . $name . ') ? ' . $name . ' : ' . substr($str, 1) . '; ?>';
break; break;
case '=': case '=':
// {$varname?='xxx'} $varname为真时才输出xxx // {$varname?='xxx'} $varname为真时才输出xxx
$str = '<?php if(!empty(' . $name . ')) echo ' . substr($str, 1) . '; ?>'; $str = '<?php if(!empty(' . $name . ')) echo ' . substr($str, 1) . '; ?>';
break; break;
case ':': case ':':
// {$varname?:'xxx'} $varname为真时输出$varname,否则输出xxx // {$varname?:'xxx'} $varname为真时输出$varname,否则输出xxx
$str = '<?php echo !empty(' . $name . ')?' . $name . $str . '; ?>'; $str = '<?php echo !empty(' . $name . ')?' . $name . $str . '; ?>';
break; break;
default: default:
if (strpos($str, ':')) { if (strpos($str, ':')) {
// {$varname ? 'a' : 'b'} $varname为真时输出a,否则输出b // {$varname ? 'a' : 'b'} $varname为真时输出a,否则输出b
$str = '<?php echo !empty(' . $name . ')?' . $str . '; ?>'; $str = '<?php echo !empty(' . $name . ')?' . $str . '; ?>';
@@ -814,13 +814,13 @@ class Template
$parseStr = $this->parseThinkVar($vars); $parseStr = $this->parseThinkVar($vars);
} else { } else {
switch ($this->config['tpl_var_identify']) { switch ($this->config['tpl_var_identify']) {
case 'array': // 识别为数组 case 'array': // 识别为数组
$parseStr = $first . '[\'' . implode('\'][\'', $vars) . '\']'; $parseStr = $first . '[\'' . implode('\'][\'', $vars) . '\']';
break; break;
case 'obj': // 识别为对象 case 'obj': // 识别为对象
$parseStr = $first . '->' . implode('->', $vars); $parseStr = $first . '->' . implode('->', $vars);
break; break;
default: // 自动判断数组或对象 default: // 自动判断数组或对象
$parseStr = '(is_array(' . $first . ')?' . $first . '[\'' . implode('\'][\'', $vars) . '\']:' . $first . '->' . implode('->', $vars) . ')'; $parseStr = '(is_array(' . $first . ')?' . $first . '[\'' . implode('\'][\'', $vars) . '\']:' . $first . '->' . implode('->', $vars) . ')';
} }
} }
@@ -866,14 +866,14 @@ class Template
// 模板函数过滤 // 模板函数过滤
$fun = trim($args[0]); $fun = trim($args[0]);
switch ($fun) { switch ($fun) {
case 'default': // 特殊模板函数 case 'default': // 特殊模板函数
if (false === strpos($name, '(')) { if (false === strpos($name, '(')) {
$name = '(isset(' . $name . ') && (' . $name . ' !== \'\')?' . $name . ':' . $args[1] . ')'; $name = '(isset(' . $name . ') && (' . $name . ' !== \'\')?' . $name . ':' . $args[1] . ')';
} else { } else {
$name = '(' . $name . ' !== \'\'?' . $name . ':' . $args[1] . ')'; $name = '(' . $name . ' !== \'\'?' . $name . ':' . $args[1] . ')';
} }
break; break;
default: // 通用模板函数 default: // 通用模板函数
if (!in_array($fun, $template_deny_funs)) { if (!in_array($fun, $template_deny_funs)) {
if (isset($args[1])) { if (isset($args[1])) {
if (strstr($args[1], '###')) { if (strstr($args[1], '###')) {

View File

@@ -242,15 +242,12 @@ class View
} }
$depr = $this->config['view_depr']; $depr = $this->config['view_depr'];
$template = str_replace(['/', ':'], $depr, $template); $template = str_replace(['/', ':'], $depr, $template);
$theme = $this->getTemplateTheme();
// 获取当前模块 $path = $this->config['view_path'];
$module = defined('MODULE_NAME') ? MODULE_NAME : '';
if (strpos($template, '@')) { if (strpos($template, '@')) {
// 跨模块调用模版文件
list($module, $template) = explode('@', $template); list($module, $template) = explode('@', $template);
$path = APP_PATH . (APP_MULTI_MODULE ? $module . DS : '') . $this->config['view_layer'] . DS;
} }
// 获取当前主题的模版路径
$path = $this->getThemePath($module);
// 分析模板文件规则 // 分析模板文件规则
if (defined('CONTROLLER_NAME')) { if (defined('CONTROLLER_NAME')) {
@@ -261,16 +258,15 @@ class View
$template = str_replace('.', DS, CONTROLLER_NAME) . $depr . $template; $template = str_replace('.', DS, CONTROLLER_NAME) . $depr . $template;
} }
} }
return $path . $template . $this->config['view_suffix']; return realpath($path) . DS . $theme . $template . $this->config['view_suffix'];
} }
/** /**
* 获取当前的模板主题 * 获取当前的模板主题
* @access private * @access private
* @param string $module 模块名
* @return string * @return string
*/ */
private function getTemplateTheme($module) private function getTemplateTheme()
{ {
if ($this->config['theme_on']) { if ($this->config['theme_on']) {
if ($this->theme) { if ($this->theme) {
@@ -284,7 +280,7 @@ class View
} elseif (Cookie::get('think_theme')) { } elseif (Cookie::get('think_theme')) {
$theme = Cookie::get('think_theme'); $theme = Cookie::get('think_theme');
} }
if (!isset($theme) || !is_dir(APP_PATH . (APP_MULTI_MODULE ? $module . DS : '') . $this->config['view_layer'] . DS . $theme)) { if (!isset($theme) || !is_dir($this->config['view_path'] . DS . $theme)) {
$theme = $this->config['default_theme']; $theme = $this->config['default_theme'];
} }
Cookie::set('think_theme', $theme, 864000); Cookie::set('think_theme', $theme, 864000);
@@ -296,25 +292,6 @@ class View
return ''; return '';
} }
/**
* 获取当前的模板路径
* @access protected
* @param string $module 模块名
* @return string
*/
protected function getThemePath($module = '')
{
// 获取当前主题名称
$theme = $this->getTemplateTheme($module);
// 获取当前主题的模版路径
$tmplPath = $this->config['view_path']; // 模块设置独立的视图目录
if (!$tmplPath) {
// 定义TMPL_PATH 则改变全局的视图目录到模块之外
$tmplPath = defined('TMPL_PATH') ? TMPL_PATH . $module . DS : APP_PATH . (APP_MULTI_MODULE ? $module . DS : '') . $this->config['view_layer'] . DS;
}
return realpath($tmplPath) . DS . $theme;
}
/** /**
* 模板变量赋值 * 模板变量赋值
* @access public * @access public

View File

@@ -132,16 +132,4 @@ class viewTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expect_data, $method->invoke($view_instance, 'template_name')); $this->assertEquals($expect_data, $method->invoke($view_instance, 'template_name'));
} }
/**
* 测试引擎设置
* @return mixed
* @access public
*/
public function testGetThemePath()
{
$view_instance = \think\View::instance();
$method = new \ReflectionMethod('\think\View', 'getThemePath');
$method->setAccessible(true);
$this->assertEquals(DS . 'theme_name' . DS, $method->invoke($view_instance));
}
} }