Think.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 <[email protected]>
  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. // 新版后台自动复制模板
  76. if (strpos($template, '/view_new') !== false) {
  77. $sourcePath = str_replace('/view_new', '/view', $template);
  78. if (file_exists($sourcePath)) {
  79. if (!copy($sourcePath, $template)) {
  80. throw new TemplateNotFoundException('Please copy file:' . $sourcePath . ' => ' . $template);
  81. }
  82. }
  83. }else{
  84. throw new TemplateNotFoundException('template not exists:' . $template, $template);
  85. }
  86. }
  87. // 记录视图信息
  88. App::$debug && Log::record('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]', 'info');
  89. $this->template->fetch($template, $data, $config);
  90. }
  91. /**
  92. * 渲染模板内容
  93. * @access public
  94. * @param string $template 模板内容
  95. * @param array $data 模板变量
  96. * @param array $config 模板参数
  97. * @return void
  98. */
  99. public function display($template, $data = [], $config = [])
  100. {
  101. $this->template->display($template, $data, $config);
  102. }
  103. /**
  104. * 自动定位模板文件
  105. * @access private
  106. * @param string $template 模板文件规则
  107. * @return string
  108. */
  109. private function parseTemplate($template)
  110. {
  111. // 分析模板文件规则
  112. $request = Request::instance();
  113. // 获取视图根目录
  114. if (strpos($template, '@')) {
  115. // 跨模块调用
  116. list($module, $template) = explode('@', $template);
  117. }
  118. if ($this->config['view_base']) {
  119. // 基础视图目录
  120. $module = isset($module) ? $module : $request->module();
  121. $path = $this->config['view_base'] . ($module ? $module . DS : '');
  122. } else {
  123. $path = isset($module) ? APP_PATH . $module . DS . 'view' . DS : $this->config['view_path'];
  124. }
  125. $depr = $this->config['view_depr'];
  126. if (0 !== strpos($template, '/')) {
  127. $template = str_replace(['/', ':'], $depr, $template);
  128. $controller = Loader::parseName($request->controller());
  129. if ($controller) {
  130. if ('' == $template) {
  131. // 如果模板文件名为空 按照默认规则定位
  132. $template = str_replace('.', DS, $controller) . $depr . (1 == $this->config['auto_rule'] ? Loader::parseName($request->action(true)) : $request->action());
  133. } elseif (false === strpos($template, $depr)) {
  134. $template = str_replace('.', DS, $controller) . $depr . $template;
  135. }
  136. }
  137. } else {
  138. $template = str_replace(['/', ':'], $depr, substr($template, 1));
  139. }
  140. return $path . ltrim($template, '/') . '.' . ltrim($this->config['view_suffix'], '.');
  141. }
  142. /**
  143. * 配置或者获取模板引擎参数
  144. * @access private
  145. * @param string|array $name 参数名
  146. * @param mixed $value 参数值
  147. * @return mixed
  148. */
  149. public function config($name, $value = null)
  150. {
  151. if (is_array($name)) {
  152. $this->template->config($name);
  153. $this->config = array_merge($this->config, $name);
  154. } elseif (is_null($value)) {
  155. return $this->template->config($name);
  156. } else {
  157. $this->template->$name = $value;
  158. $this->config[$name] = $value;
  159. }
  160. }
  161. public function __call($method, $params)
  162. {
  163. return call_user_func_array([$this->template, $method], $params);
  164. }
  165. }