Php.php 5.2 KB

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