Think.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\view\driver;
  12. use think\App;
  13. use think\exception\TemplateNotFoundException;
  14. use think\Loader;
  15. use think\Log;
  16. use think\Request;
  17. use think\Template;
  18. class Think
  19. {
  20. // 模板引擎实例
  21. private $template;
  22. // 模板引擎参数
  23. protected $config = [
  24. // 视图基础目录(集中式)
  25. 'view_base' => '',
  26. // 模板起始路径
  27. 'view_path' => '',
  28. // 模板文件后缀
  29. 'view_suffix' => 'html',
  30. // 模板文件名分隔符
  31. 'view_depr' => DS,
  32. // 是否开启模板编译缓存,设为false则每次都会重新编译
  33. 'tpl_cache' => true,
  34. // 默认模板渲染规则 1 解析为小写+下划线 2 全部转换小写
  35. 'auto_rule' => 1,
  36. ];
  37. public function __construct($config = [])
  38. {
  39. $this->config = array_merge($this->config, $config);
  40. if (empty($this->config['view_path'])) {
  41. $this->config['view_path'] = App::$modulePath . 'view' . DS;
  42. }
  43. $this->template = new Template($this->config);
  44. }
  45. /**
  46. * 检测是否存在模板文件
  47. * @access public
  48. * @param string $template 模板文件或者模板规则
  49. * @return bool
  50. */
  51. public function exists($template)
  52. {
  53. if ('' == pathinfo($template, PATHINFO_EXTENSION)) {
  54. // 获取模板文件名
  55. $template = $this->parseTemplate($template);
  56. }
  57. return is_file($template);
  58. }
  59. /**
  60. * 渲染模板文件
  61. * @access public
  62. * @param string $template 模板文件
  63. * @param array $data 模板变量
  64. * @param array $config 模板参数
  65. * @return void
  66. */
  67. public function fetch($template, $data = [], $config = [])
  68. {
  69. if ('' == pathinfo($template, PATHINFO_EXTENSION)) {
  70. // 获取模板文件名
  71. $template = $this->parseTemplate($template);
  72. }
  73. // 模板不存在 抛出异常
  74. if (!is_file($template)) {
  75. throw new TemplateNotFoundException('template not exists:' . $template, $template);
  76. }
  77. // 记录视图信息
  78. App::$debug && Log::record('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]', 'info');
  79. $this->template->fetch($template, $data, $config);
  80. }
  81. /**
  82. * 渲染模板内容
  83. * @access public
  84. * @param string $template 模板内容
  85. * @param array $data 模板变量
  86. * @param array $config 模板参数
  87. * @return void
  88. */
  89. public function display($template, $data = [], $config = [])
  90. {
  91. $this->template->display($template, $data, $config);
  92. }
  93. /**
  94. * 自动定位模板文件
  95. * @access private
  96. * @param string $template 模板文件规则
  97. * @return string
  98. */
  99. private function parseTemplate($template)
  100. {
  101. // 分析模板文件规则
  102. $request = Request::instance();
  103. // 获取视图根目录
  104. if (strpos($template, '@')) {
  105. // 跨模块调用
  106. list($module, $template) = explode('@', $template);
  107. }
  108. if ($this->config['view_base']) {
  109. // 基础视图目录
  110. $module = isset($module) ? $module : $request->module();
  111. $path = $this->config['view_base'] . ($module ? $module . DS : '');
  112. } else {
  113. $path = isset($module) ? APP_PATH . $module . DS . 'view' . DS : $this->config['view_path'];
  114. }
  115. $depr = $this->config['view_depr'];
  116. if (0 !== strpos($template, '/')) {
  117. $template = str_replace(['/', ':'], $depr, $template);
  118. $controller = Loader::parseName($request->controller());
  119. if ($controller) {
  120. if ('' == $template) {
  121. // 如果模板文件名为空 按照默认规则定位
  122. $template = str_replace('.', DS, $controller) . $depr . (1 == $this->config['auto_rule'] ? Loader::parseName($request->action(true)) : $request->action());
  123. } elseif (false === strpos($template, $depr)) {
  124. $template = str_replace('.', DS, $controller) . $depr . $template;
  125. }
  126. }
  127. } else {
  128. $template = str_replace(['/', ':'], $depr, substr($template, 1));
  129. }
  130. return $path . ltrim($template, '/') . '.' . ltrim($this->config['view_suffix'], '.');
  131. }
  132. /**
  133. * 配置或者获取模板引擎参数
  134. * @access private
  135. * @param string|array $name 参数名
  136. * @param mixed $value 参数值
  137. * @return mixed
  138. */
  139. public function config($name, $value = null)
  140. {
  141. if (is_array($name)) {
  142. $this->template->config($name);
  143. $this->config = array_merge($this->config, $name);
  144. } elseif (is_null($value)) {
  145. return $this->template->config($name);
  146. } else {
  147. $this->template->$name = $value;
  148. $this->config[$name] = $value;
  149. }
  150. }
  151. public function __call($method, $params)
  152. {
  153. return call_user_func_array([$this->template, $method], $params);
  154. }
  155. }