-
Notifications
You must be signed in to change notification settings - Fork 0
/
View.php
334 lines (292 loc) · 6.95 KB
/
View.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/**
*
* -+-----------------------------------
* |PHP5 Framework - 2011
* |Web Site: www.iblue.cc
* |E-mail: mejinke@gmail.com
* |Date: 2012-01-10
* -+-----------------------------------
*
* @desc View 视图类
* @author jingke
*/
class XF_View
{
/**
* 当前实例
* @var XF_View
*/
private static $_instance;
/**
* 模板目录开始目录
* @access private
* @var string
*/
private $_template_folder = '';
/**
* 视图资料
* @var array
*/
private $_view_data = null;
/**
* 使用的布局文件名称
* @access private
* @var string
*/
private $_layout = '';
/**
* 缓存驱动
* @access private
* @var XF_Cache_Interface
*/
private $_cache_instance;
/**
* Action缓存时间[分钟] 0为不缓存
* @access priviate
* @var int
*/
private $_cache_time = 0;
/**
* 页面资源(js、css)起始路径
* @var string
*/
private $_resource_path;
private function __construct(){}
private function __clone(){}
/**
* 获取当前实例
* @return XF_View
*/
public static function getInstance()
{
if (self::$_instance === null)
self::$_instance = new self();
return self::$_instance;
}
public function __get($name)
{
if (isset($this->_view_data[$name]))
return $this->_view_data[$name];
return NULL;
}
public function __set($name, $value)
{
if (strpos($name, '[') >0 && strpos($name, ']') > 0)
{
$name = str_replace(']', '', str_replace('[', '', $name));
$this->_view_data[$name][] = $value;
}
else
$this->_view_data[$name] = $value;
return $this;
}
/**
* 返回当前视图对象
* @return XF_View
*/
public function getView()
{
return $this;
}
/**
* 填充视图资料
* @access public
* @param string $key 名称
* @param mixed $value
* @return XF_View
*/
public function assign($key, $value)
{
$this->_view_data[$key] = $value;
return $this;
}
/**
* 设置Action的缓存驱动对象
* @access public
* @param XF_Cache_Interface $cache
* @return XF_View
*/
public function setCache(XF_Cache_Interface $cache)
{
$this->_cache_instance = $cache;
return $this;
}
/**
* 设置Action缓存时间
* @access public
* @param int $minutes
* @return XF_View
*/
public function setCacheTime($minutes = 0)
{
if (is_int(intval($minutes)))
{
$this->_cache_time = intval($minutes);
}
return $this;
}
/**
* 设置页面资源起始路径
* @param string $path
* @return XF_View
*/
public function setResourcePath($path)
{
$this->_resource_path = $path;
return $this;
}
/**
* 获取页面资源起始路径
* @return string
*/
public function getResourcePath()
{
return empty($this->_resource_path) ? '' : $this->_resource_path;
}
/**
* 渲染模板
* @access public
* @param string $template_file Action模板文件
* @param string $cache_sign 缓存标识
* @param XF_View_Layout_Abstract $layout
* @return string
*/
public function render($template_file = null, $cache_sign = '', XF_View_Layout_Abstract $layout = null)
{
$this->getTemplateStartLocation();
$request = XF_Controller_Request_Http::getInstance();
$appName = $request->getModule();
$controllerName =$request->getController();
$actionName = $request->getAction();
//如果没有设置模板,则自动获取当前Action名称相关的模板
if ($template_file == null)
$template_file = $this->_template_folder.'/'.$controllerName.'/'.$actionName.'.php';
if (!is_file($template_file))
throw new XF_View_Exception('Action template not found');
$content = $this->obGetContents($template_file);
XF_Controller_Plugin_Manage::getInstance()->postRender($content);
//是否需要缓存?
if ($this->_cache_time > 0)
{
$layout_tag = '';
if ($layout != null)
$layout_tag = '<!--Layout:'.get_class($layout).','.$layout->getCacheTime().','.(int)$layout->getCacheType().'-->';
$_content = $content.$layout_tag.$this->_makeCacheHeadTitleAndHeadMeta();
//写入缓存
if ($this->_cache_instance instanceof XF_Cache_SECache)
{
XF_File::mkdirs(TEMP_PATH.'/Cache');
$this->_cache_instance->setCacheSaveFile(TEMP_PATH.'/Cache/ActionViewCache');
}
$this->_cache_instance->setCacheTime($this->_cache_time);
$this->_cache_instance->add($cache_sign, $_content);
}
//是否启用布局
if ($layout != null)
{
$layout->assign('$layoutContent', $content);
$content = $layout->render();
}
return $content;
}
/**
* 生成缓存时需要保存的当前view的标题,meta等信息
* @access private
* @return string
*/
private function _makeCacheHeadTitleAndHeadMeta()
{
$title = $this->headTitle()->getTitle();
$metas = $this->headMeta()->getMetas();
$scripts = $this->headScript()->getScripts();
$stylesheets = $this->headStylesheet()->getStylesheets();
$links = $this->headLink()->getLinks();
$html = '<!--Title:'.$title.'-->';
$html .= '<!--Metas:'.serialize($metas).'-->';
$html .= '<!--Scripts:'.serialize($scripts).'-->';
$html .= '<!--Stylesheets:'.serialize($stylesheets).'-->';
$html .= '<!--Links:'.serialize($links).'-->';
return $html;
}
/**
* 添加模板文件获取内存资料
* @access public
* @param array $data
* @param string $file
* @return string
*/
public function obGetContents($file)
{
if (!is_file($file))
return '';
$content = '';
ob_start();
require $file;
$content = ob_get_contents();
ob_end_clean();
//清除BOM
$charset[1] = substr($content, 0, 1);
$charset[2] = substr($content, 1, 1);
$charset[3] = substr($content, 2, 1);
if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191)
$content = substr($content, 3);
return $content;
}
/**
* 获取模板起始位置目录
* @access public
* @return string
*/
public function getTemplateStartLocation()
{
$this->_template_folder = XF_Controller_Front::getInstance()->getModuleDir().'/views/'.XF_Config::getInstance()->getViewStyle();
return $this->_template_folder;
}
/**
* 标题Header
* @access public
* @param string $var
* @return XF_View_Helper_Header_Title
*/
public function headTitle($var = '')
{
return XF_View_Helper::getInstance()->headeTitle($var);
}
/**
* Meta
* @param string $var
* @return XF_View_Helper_Header_Meta
*/
public function headMeta($var = null)
{
return XF_View_Helper::getInstance()->headeMeta($var);
}
/**
* Link
* @param string $var
* @return XF_View_Helper_Header_Link
*/
public function headLink($var = null)
{
return XF_View_Helper::getInstance()->headeLink($var);
}
/**
* Script
* @param string $var
* @return XF_View_Helper_Header_Script
*/
public function headScript($var = null)
{
return XF_View_Helper::getInstance()->headeScript($var);
}
/**
* Stylesheet
* @param string $var
* @return XF_View_Helper_Header_Stylesheet
*/
public function headStylesheet($var = null)
{
return XF_View_Helper::getInstance()->headeStylesheet($var);
}
}