Think.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. ];
  35. public function __construct($config = [])
  36. {
  37. $this->config = array_merge($this->config, $config);
  38. if (empty($this->config['view_path'])) {
  39. $this->config['view_path'] = App::$modulePath . 'view' . DS;
  40. }
  41. $this->template = new Template($this->config);
  42. }
  43. /**
  44. * 检测是否存在模板文件
  45. * @access public
  46. * @param string $template 模板文件或者模板规则
  47. * @return bool
  48. */
  49. public function exists($template)
  50. {
  51. if ('' == pathinfo($template, PATHINFO_EXTENSION)) {
  52. // 获取模板文件名
  53. $template = $this->parseTemplate($template);
  54. }
  55. return is_file($template);
  56. }
  57. /**
  58. * 渲染模板文件
  59. * @access public
  60. * @param string $template 模板文件
  61. * @param array $data 模板变量
  62. * @param array $config 模板参数
  63. * @return void
  64. */
  65. public function fetch($template, $data = [], $config = [])
  66. {
  67. if ('' == pathinfo($template, PATHINFO_EXTENSION)) {
  68. // 获取模板文件名
  69. $template = $this->parseTemplate($template);
  70. }
  71. // 模板不存在 抛出异常
  72. if (!is_file($template)) {
  73. throw new TemplateNotFoundException('template not exists:' . $template, $template);
  74. }
  75. // 记录视图信息
  76. App::$debug && Log::record('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]', 'info');
  77. $this->template->fetch($template, $data, $config);
  78. }
  79. /**
  80. * 渲染模板内容
  81. * @access public
  82. * @param string $template 模板内容
  83. * @param array $data 模板变量
  84. * @param array $config 模板参数
  85. * @return void
  86. */
  87. public function display($template, $data = [], $config = [])
  88. {
  89. $this->template->display($template, $data, $config);
  90. }
  91. /**
  92. * 自动定位模板文件
  93. * @access private
  94. * @param string $template 模板文件规则
  95. * @return string
  96. */
  97. private function parseTemplate($template)
  98. {
  99. // 分析模板文件规则
  100. $request = Request::instance();
  101. // 获取视图根目录
  102. if (strpos($template, '@')) {
  103. // 跨模块调用
  104. list($module, $template) = explode('@', $template);
  105. }
  106. if ($this->config['view_base']) {
  107. // 基础视图目录
  108. $module = isset($module) ? $module : $request->module();
  109. $path = $this->config['view_base'] . ($module ? $module . DS : '');
  110. } else {
  111. $path = isset($module) ? APP_PATH . $module . DS . 'view' . DS : $this->config['view_path'];
  112. }
  113. $depr = $this->config['view_depr'];
  114. if (0 !== strpos($template, '/')) {
  115. $template = str_replace(['/', ':'], $depr, $template);
  116. $controller = Loader::parseName($request->controller());
  117. if ($controller) {
  118. if ('' == $template) {
  119. // 如果模板文件名为空 按照默认规则定位
  120. $template = str_replace('.', DS, $controller) . $depr . $request->action();
  121. } elseif (false === strpos($template, $depr)) {
  122. $template = str_replace('.', DS, $controller) . $depr . $template;
  123. }
  124. }
  125. } else {
  126. $template = str_replace(['/', ':'], $depr, substr($template, 1));
  127. }
  128. return $path . ltrim($template, '/') . '.' . ltrim($this->config['view_suffix'], '.');
  129. }
  130. /**
  131. * 配置或者获取模板引擎参数
  132. * @access private
  133. * @param string|array $name 参数名
  134. * @param mixed $value 参数值
  135. * @return mixed
  136. */
  137. public function config($name, $value = null)
  138. {
  139. if (is_array($name)) {
  140. $this->template->config($name);
  141. $this->config = array_merge($this->config, $name);
  142. } elseif (is_null($value)) {
  143. return $this->template->config($name);
  144. } else {
  145. $this->template->$name = $value;
  146. $this->config[$name] = $value;
  147. }
  148. }
  149. public function __call($method, $params)
  150. {
  151. return call_user_func_array([$this->template, $method], $params);
  152. }
  153. }