View.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 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. declare (strict_types=1);
  12. namespace think;
  13. use think\helper\Arr;
  14. /**
  15. * 视图类
  16. * @package think
  17. */
  18. class View extends Manager
  19. {
  20. protected $namespace = '\\think\\view\\driver\\';
  21. /**
  22. * 模板变量
  23. * @var array
  24. */
  25. protected $data = [];
  26. /**
  27. * 内容过滤
  28. * @var mixed
  29. */
  30. protected $filter;
  31. /**
  32. * 全局模板变量
  33. * @var array
  34. */
  35. protected static $var = [];
  36. /**
  37. * 获取模板引擎
  38. * @access public
  39. * @param string $type 模板引擎类型
  40. * @return $this
  41. */
  42. public function engine(string $type = null)
  43. {
  44. return $this->driver($type);
  45. }
  46. /**
  47. * 模板变量静态赋值
  48. * @access public
  49. * @param mixed $name 变量名
  50. * @param mixed $value 变量值
  51. * @return $this
  52. */
  53. public function share($name, $value = '')
  54. {
  55. if (is_array($name)) {
  56. self::$var = array_merge(self::$var, $name);
  57. } else {
  58. self::$var[$name] = $value;
  59. }
  60. return $this;
  61. }
  62. /**
  63. * 清理模板变量
  64. * @access public
  65. * @return void
  66. */
  67. public function clear()
  68. {
  69. self::$var = [];
  70. $this->data = [];
  71. }
  72. /**
  73. * 模板变量赋值
  74. * @access public
  75. * @param string|array $name 模板变量
  76. * @param mixed $value 变量值
  77. * @return $this
  78. */
  79. public function assign($name, $value = null)
  80. {
  81. if (is_array($name)) {
  82. $this->data = array_merge($this->data, $name);
  83. } else {
  84. $this->data[$name] = $value;
  85. }
  86. return $this;
  87. }
  88. /**
  89. * 视图过滤
  90. * @access public
  91. * @param Callable $filter 过滤方法或闭包
  92. * @return $this
  93. */
  94. public function filter(callable $filter = null)
  95. {
  96. $this->filter = $filter;
  97. return $this;
  98. }
  99. /**
  100. * 解析和获取模板内容 用于输出
  101. * @access public
  102. * @param string $template 模板文件名或者内容
  103. * @param array $vars 模板变量
  104. * @return string
  105. * @throws \Exception
  106. */
  107. public function fetch(string $template = '', array $vars = []): string
  108. {
  109. return $this->getContent(function () use ($vars, $template) {
  110. $this->engine()->fetch($template, array_merge(self::$var, $this->data, $vars));
  111. });
  112. }
  113. /**
  114. * 渲染内容输出
  115. * @access public
  116. * @param string $content 内容
  117. * @param array $vars 模板变量
  118. * @return string
  119. */
  120. public function display(string $content, array $vars = []): string
  121. {
  122. return $this->getContent(function () use ($vars, $content) {
  123. $this->engine()->display($content, array_merge(self::$var, $this->data, $vars));
  124. });
  125. }
  126. /**
  127. * 获取模板引擎渲染内容
  128. * @param $callback
  129. * @return string
  130. * @throws \Exception
  131. */
  132. protected function getContent($callback): string
  133. {
  134. // 页面缓存
  135. ob_start();
  136. if (PHP_VERSION > 8.0) {
  137. ob_implicit_flush(false);
  138. } else {
  139. ob_implicit_flush(0);
  140. }
  141. // 渲染输出
  142. try {
  143. $callback();
  144. } catch (\Exception $e) {
  145. ob_end_clean();
  146. throw $e;
  147. }
  148. // 获取并清空缓存
  149. $content = ob_get_clean();
  150. if ($this->filter) {
  151. $content = call_user_func_array($this->filter, [$content]);
  152. }
  153. return $content;
  154. }
  155. /**
  156. * 模板变量赋值
  157. * @access public
  158. * @param string $name 变量名
  159. * @param mixed $value 变量值
  160. */
  161. public function __set($name, $value)
  162. {
  163. $this->data[$name] = $value;
  164. }
  165. /**
  166. * 取得模板显示变量的值
  167. * @access protected
  168. * @param string $name 模板变量
  169. * @return mixed
  170. */
  171. public function __get($name)
  172. {
  173. return $this->data[$name];
  174. }
  175. /**
  176. * 检测模板变量是否设置
  177. * @access public
  178. * @param string $name 模板变量名
  179. * @return bool
  180. */
  181. public function __isset($name)
  182. {
  183. return isset($this->data[$name]);
  184. }
  185. protected function resolveConfig(string $name)
  186. {
  187. $config = $this->app->config->get('view', []);
  188. Arr::forget($config, 'type');
  189. return $config;
  190. }
  191. /**
  192. * 默认驱动
  193. * @return string|null
  194. */
  195. public function getDefaultDriver()
  196. {
  197. return $this->app->config->get('view.type', 'php');
  198. }
  199. }