Php.php 5.2 KB

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