|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class View |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://www.icy2003.com/ |
|
6
|
|
|
* @author icy2003 <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2017, icy2003 |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace icy2003\php\ihelpers; |
|
10
|
|
|
|
|
11
|
|
|
use Exception; |
|
12
|
|
|
use icy2003\php\I; |
|
13
|
|
|
use icy2003\php\icomponents\file\LocalFile; |
|
14
|
|
|
use Throwable; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* 视图渲染类 |
|
18
|
|
|
*/ |
|
19
|
|
|
class View |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* 布局文件,支持别名 |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
public $layout = false; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* 布局文件的参数 |
|
31
|
|
|
* |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
public $layoutParams = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* 渲染视图 |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $view 视图文件名 |
|
40
|
|
|
* @param array $params 参数 |
|
41
|
|
|
* |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
|
|
public function render($view, $params = []) |
|
45
|
|
|
{ |
|
46
|
|
|
$content = $this->_renderContent($view, $params); |
|
47
|
|
|
$layoutFile = I::getAlias($this->layout); |
|
48
|
|
|
return $this->_renderContent($layoutFile, ['content' => $content, 'layoutParams' => $this->layoutParams]); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* 渲染视图,不使用布局 |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $view 视图文件名 |
|
55
|
|
|
* @param array $params 参数 |
|
56
|
|
|
* |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
public function renderPartial($view, $params = []) |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->_renderContent($view, $params); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* 渲染视图内容 |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $view 视图文件名 |
|
68
|
|
|
* @param array $params 参数 |
|
69
|
|
|
* |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function _renderContent($view, $params = []) |
|
73
|
|
|
{ |
|
74
|
|
|
$viewFile = I::getAlias($view); |
|
75
|
|
|
if (false === (new LocalFile())->isFile($viewFile)) { |
|
76
|
|
|
throw new Exception('找不到视图文件:' . $viewFile); |
|
77
|
|
|
} |
|
78
|
|
|
return $this->_renderPhpFile($viewFile, $params); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* 渲染 PHP 文件 |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $viewFile 视图文件名 |
|
85
|
|
|
* @param array $params 参数 |
|
86
|
|
|
* |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function _renderPhpFile($viewFile, $params) |
|
90
|
|
|
{ |
|
91
|
|
|
$level = ob_get_level(); |
|
92
|
|
|
ob_start(); |
|
93
|
|
|
ob_implicit_flush(0); |
|
94
|
|
|
extract($params, EXTR_OVERWRITE); |
|
95
|
|
|
try { |
|
96
|
|
|
require $viewFile; |
|
97
|
|
|
return ob_get_clean(); |
|
98
|
|
|
} catch (Exception $e) { |
|
99
|
|
|
while (ob_get_level() > $level) { |
|
100
|
|
|
if (!@ob_end_clean()) { |
|
101
|
|
|
ob_clean(); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
throw $e; |
|
105
|
|
|
} catch (Throwable $e) { |
|
106
|
|
|
while (ob_get_level() > $level) { |
|
107
|
|
|
if (!@ob_end_clean()) { |
|
108
|
|
|
ob_clean(); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
throw $e; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|