Total Complexity | 16 |
Total Lines | 184 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
15 | class View |
||
1 ignored issue
–
show
|
|||
16 | { |
||
17 | /** |
||
18 | * 模板引擎实例 |
||
19 | * @var object |
||
20 | */ |
||
21 | public $engine; |
||
22 | |||
23 | /** |
||
24 | * 模板变量 |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $data = []; |
||
28 | |||
29 | /** |
||
30 | * 内容过滤 |
||
31 | * @var mixed |
||
32 | */ |
||
33 | protected $filter; |
||
34 | |||
35 | /** |
||
36 | * 初始化 |
||
37 | * @access public |
||
38 | * @param array $options 模板引擎参数 |
||
39 | * @return $this |
||
40 | */ |
||
41 | public function __construct(array $options = []) |
||
42 | { |
||
43 | // 初始化模板引擎 |
||
44 | $type = $options['type'] ?? 'think'; |
||
45 | unset($options['type']); |
||
46 | $this->engine($type, $options); |
||
47 | |||
48 | return $this; |
||
49 | } |
||
50 | |||
51 | public static function __make(Config $config) |
||
1 ignored issue
–
show
|
|||
52 | { |
||
53 | return new static($config->get('template')); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * 模板变量赋值 |
||
58 | * @access public |
||
59 | * @param string|array $name 模板变量 |
||
60 | * @param mixed $value 变量值 |
||
61 | * @return $this |
||
62 | */ |
||
63 | public function assign($name, $value = null) |
||
64 | { |
||
65 | if (is_array($name)) { |
||
66 | $this->data = array_merge($this->data, $name); |
||
67 | } else { |
||
68 | $this->data[$name] = $value; |
||
69 | } |
||
70 | |||
71 | return $this; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * 设置当前模板解析的引擎 |
||
76 | * @access public |
||
77 | * @param string $type 模板引擎类型 |
||
78 | * @param array|string $options 模板引擎参数 |
||
79 | * @return $this |
||
80 | */ |
||
81 | public function engine(string $type, array $options = []) |
||
82 | { |
||
83 | $this->engine = App::factory($type, '\\think\\view\\driver\\', $options); |
||
84 | |||
85 | return $this; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * 配置模板引擎 |
||
90 | * @access public |
||
91 | * @param array $name 模板参数 |
||
92 | * @return $this |
||
93 | */ |
||
94 | public function config(array $config) |
||
95 | { |
||
96 | $this->engine->config($config); |
||
97 | |||
98 | return $this; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * 检查模板是否存在 |
||
103 | * @access public |
||
104 | * @param string $name 参数名 |
||
105 | * @return bool |
||
106 | */ |
||
107 | public function exists(string $name): bool |
||
108 | { |
||
109 | return $this->engine->exists($name); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * 视图过滤 |
||
114 | * @access public |
||
115 | * @param Callable $filter 过滤方法或闭包 |
||
1 ignored issue
–
show
|
|||
116 | * @return $this |
||
117 | */ |
||
118 | public function filter(callable $filter = null) |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * 解析和获取模板内容 用于输出 |
||
126 | * @access public |
||
127 | * @param string $template 模板文件名或者内容 |
||
128 | * @param bool $renderContent 是否渲染内容 |
||
129 | * @return string |
||
130 | * @throws \Exception |
||
131 | */ |
||
132 | public function fetch(string $template = '', bool $renderContent = false): string |
||
133 | { |
||
134 | // 页面缓存 |
||
135 | ob_start(); |
||
136 | ob_implicit_flush(0); |
||
137 | |||
138 | // 渲染输出 |
||
139 | try { |
||
140 | $method = $renderContent ? 'display' : 'fetch'; |
||
141 | $this->engine->$method($template, $this->data); |
||
142 | } catch (\Exception $e) { |
||
143 | ob_end_clean(); |
||
144 | throw $e; |
||
145 | } |
||
146 | |||
147 | // 获取并清空缓存 |
||
148 | $content = ob_get_clean(); |
||
149 | |||
150 | if ($this->filter) { |
||
151 | $content = call_user_func_array($this->filter, [$content]); |
||
152 | } |
||
153 | |||
154 | return $content; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * 渲染内容输出 |
||
159 | * @access public |
||
160 | * @param string $content 内容 |
||
161 | * @return string |
||
162 | */ |
||
163 | public function display(string $content): string |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * 模板变量赋值 |
||
170 | * @access public |
||
171 | * @param string $name 变量名 |
||
172 | * @param mixed $value 变量值 |
||
173 | */ |
||
174 | public function __set($name, $value) |
||
175 | { |
||
176 | $this->data[$name] = $value; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * 取得模板显示变量的值 |
||
181 | * @access protected |
||
182 | * @param string $name 模板变量 |
||
183 | * @return mixed |
||
184 | */ |
||
185 | public function __get($name) |
||
186 | { |
||
187 | return $this->data[$name]; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * 检测模板变量是否设置 |
||
192 | * @access public |
||
193 | * @param string $name 模板变量名 |
||
194 | * @return bool |
||
195 | */ |
||
196 | public function __isset($name) |
||
199 | } |
||
200 | } |
||
201 |