Php.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. class Php
  16. {
  17. // 模板引擎参数
  18. protected $config = [
  19. // 默认模板渲染规则 1 解析为小写+下划线 2 全部转换小写
  20. 'auto_rule' => 1,
  21. // 视图基础目录(集中式)
  22. 'view_base' => '',
  23. // 模板起始路径
  24. 'view_path' => '',
  25. // 模板文件后缀
  26. 'view_suffix' => 'php',
  27. // 模板文件名分隔符
  28. 'view_depr' => DIRECTORY_SEPARATOR,
  29. ];
  30. protected $template;
  31. protected $app;
  32. protected $content;
  33. public function __construct(App $app, $config = [])
  34. {
  35. $this->app = $app;
  36. $this->config = array_merge($this->config, (array) $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. $this->app
  72. ->log('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]');
  73. extract($data, EXTR_OVERWRITE);
  74. include $this->template;
  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. $this->content = $content;
  86. extract($data, EXTR_OVERWRITE);
  87. eval('?>' . $this->content);
  88. }
  89. /**
  90. * 自动定位模板文件
  91. * @access private
  92. * @param string $template 模板文件规则
  93. * @return string
  94. */
  95. private function parseTemplate($template)
  96. {
  97. if (empty($this->config['view_path'])) {
  98. $this->config['view_path'] = $this->app->getModulePath() . 'view' . DIRECTORY_SEPARATOR;
  99. }
  100. $request = $this->app['request'];
  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 . DIRECTORY_SEPARATOR : '');
  110. } else {
  111. $path = isset($module) ? $this->app->getAppPath() . $module . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR : $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('.', DIRECTORY_SEPARATOR, $controller) . $depr . $this->getActionTemplate($request);
  121. } elseif (false === strpos($template, $depr)) {
  122. $template = str_replace('.', DIRECTORY_SEPARATOR, $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. protected function getActionTemplate($request)
  131. {
  132. $rule = [$request->action(true), Loader::parseName($request->action(true)), $request->action()];
  133. $type = $this->config['auto_rule'];
  134. return isset($rule[$type]) ? $rule[$type] : $rule[0];
  135. }
  136. /**
  137. * 配置模板引擎
  138. * @access private
  139. * @param string|array $name 参数名
  140. * @param mixed $value 参数值
  141. * @return void
  142. */
  143. public function config($name, $value = null)
  144. {
  145. if (is_array($name)) {
  146. $this->config = array_merge($this->config, $name);
  147. } elseif (is_null($value)) {
  148. return isset($this->config[$name]) ? $this->config[$name] : null;
  149. } else {
  150. $this->config[$name] = $value;
  151. }
  152. }
  153. public function __debugInfo()
  154. {
  155. return ['config' => $this->config];
  156. }
  157. }