Think.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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\Template;
  16. class Think
  17. {
  18. // 模板引擎实例
  19. private $template;
  20. private $app;
  21. // 模板引擎参数
  22. protected $config = [
  23. // 默认模板渲染规则 1 解析为小写+下划线 2 全部转换小写
  24. 'auto_rule' => 1,
  25. // 视图基础目录(集中式)
  26. 'view_base' => '',
  27. // 模板起始路径
  28. 'view_path' => '',
  29. // 模板文件后缀
  30. 'view_suffix' => 'html',
  31. // 模板文件名分隔符
  32. 'view_depr' => DIRECTORY_SEPARATOR,
  33. // 是否开启模板编译缓存,设为false则每次都会重新编译
  34. 'tpl_cache' => true,
  35. ];
  36. public function __construct(App $app, $config = [])
  37. {
  38. $this->app = $app;
  39. $this->config = array_merge($this->config, (array) $config);
  40. if (empty($this->config['view_path'])) {
  41. $this->config['view_path'] = $app->getModulePath() . 'view' . DIRECTORY_SEPARATOR;
  42. }
  43. $this->template = new Template($app, $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. $this->app
  79. ->log('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]');
  80. $this->template->fetch($template, $data, $config);
  81. }
  82. /**
  83. * 渲染模板内容
  84. * @access public
  85. * @param string $template 模板内容
  86. * @param array $data 模板变量
  87. * @param array $config 模板参数
  88. * @return void
  89. */
  90. public function display($template, $data = [], $config = [])
  91. {
  92. $this->template->display($template, $data, $config);
  93. }
  94. /**
  95. * 自动定位模板文件
  96. * @access private
  97. * @param string $template 模板文件规则
  98. * @return string
  99. */
  100. private function parseTemplate($template)
  101. {
  102. // 分析模板文件规则
  103. $request = $this->app['request'];
  104. // 获取视图根目录
  105. if (strpos($template, '@')) {
  106. // 跨模块调用
  107. list($module, $template) = explode('@', $template);
  108. }
  109. if ($this->config['view_base']) {
  110. // 基础视图目录
  111. $module = isset($module) ? $module : $request->module();
  112. $path = $this->config['view_base'] . ($module ? $module . DIRECTORY_SEPARATOR : '');
  113. } else {
  114. $path = isset($module) ? $this->app->getAppPath() . $module . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR : $this->config['view_path'];
  115. }
  116. $depr = $this->config['view_depr'];
  117. if (0 !== strpos($template, '/')) {
  118. $template = str_replace(['/', ':'], $depr, $template);
  119. $controller = Loader::parseName($request->controller());
  120. if ($controller) {
  121. if ('' == $template) {
  122. // 如果模板文件名为空 按照默认规则定位
  123. $template = str_replace('.', DIRECTORY_SEPARATOR, $controller) . $depr . $this->getActionTemplate($request);
  124. } elseif (false === strpos($template, $depr)) {
  125. $template = str_replace('.', DIRECTORY_SEPARATOR, $controller) . $depr . $template;
  126. }
  127. }
  128. } else {
  129. $template = str_replace(['/', ':'], $depr, substr($template, 1));
  130. }
  131. return $path . ltrim($template, '/') . '.' . ltrim($this->config['view_suffix'], '.');
  132. }
  133. protected function getActionTemplate($request)
  134. {
  135. $rule = [$request->action(true), Loader::parseName($request->action(true)), $request->action()];
  136. $type = $this->config['auto_rule'];
  137. return isset($rule[$type]) ? $rule[$type] : $rule[0];
  138. }
  139. /**
  140. * 配置或者获取模板引擎参数
  141. * @access private
  142. * @param string|array $name 参数名
  143. * @param mixed $value 参数值
  144. * @return mixed
  145. */
  146. public function config($name, $value = null)
  147. {
  148. if (is_array($name)) {
  149. $this->template->config($name);
  150. $this->config = array_merge($this->config, $name);
  151. } elseif (is_null($value)) {
  152. return $this->template->config($name);
  153. } else {
  154. $this->template->$name = $value;
  155. $this->config[$name] = $value;
  156. }
  157. }
  158. public function __call($method, $params)
  159. {
  160. return call_user_func_array([$this->template, $method], $params);
  161. }
  162. public function __debugInfo()
  163. {
  164. return ['config' => $this->config];
  165. }
  166. }