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

@@ -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));
}
} }