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 <[email protected]> |
10
|
|
|
// +---------------------------------------------------------------------- |
11
|
|
|
declare (strict_types = 1); |
12
|
|
|
|
13
|
|
|
namespace think\view\driver; |
14
|
|
|
|
15
|
|
|
use think\App; |
16
|
|
|
use think\template\exception\TemplateNotFoundException; |
|
|
|
|
17
|
|
|
|
18
|
|
|
class Php |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
protected $template; |
21
|
|
|
protected $content; |
22
|
|
|
protected $app; |
23
|
|
|
|
24
|
|
|
// 模板引擎参数 |
25
|
|
|
protected $config = [ |
26
|
|
|
// 默认模板渲染规则 1 解析为小写+下划线 2 全部转换小写 |
27
|
|
|
'auto_rule' => 1, |
28
|
|
|
// 视图基础目录(集中式) |
29
|
|
|
'view_base' => '', |
30
|
|
|
// 模板起始路径 |
31
|
|
|
'view_path' => '', |
32
|
|
|
// 模板文件后缀 |
33
|
|
|
'view_suffix' => 'php', |
34
|
|
|
// 模板文件名分隔符 |
35
|
|
|
'view_depr' => DIRECTORY_SEPARATOR, |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
public function __construct(App $app, array $config = []) |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$this->app = $app; |
41
|
|
|
$this->config = array_merge($this->config, (array) $config); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* 检测是否存在模板文件 |
46
|
|
|
* @access public |
47
|
|
|
* @param string $template 模板文件或者模板规则 |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
|
|
public function exists(string $template): bool |
51
|
|
|
{ |
52
|
|
|
if ('' == pathinfo($template, PATHINFO_EXTENSION)) { |
53
|
|
|
// 获取模板文件名 |
54
|
|
|
$template = $this->parseTemplate($template); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return is_file($template); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* 渲染模板文件 |
62
|
|
|
* @access public |
63
|
|
|
* @param string $template 模板文件 |
64
|
|
|
* @param array $data 模板变量 |
|
|
|
|
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
public function fetch(string $template, array $data = []): void |
68
|
|
|
{ |
69
|
|
|
if ('' == pathinfo($template, PATHINFO_EXTENSION)) { |
70
|
|
|
// 获取模板文件名 |
71
|
|
|
$template = $this->parseTemplate($template); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// 模板不存在 抛出异常 |
75
|
|
|
if (!is_file($template)) { |
76
|
|
|
throw new TemplateNotFoundException('template not exists:' . $template, $template); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->template = $template; |
80
|
|
|
|
81
|
|
|
// 记录视图信息 |
82
|
|
|
$this->app['log'] |
83
|
|
|
->record('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]'); |
84
|
|
|
|
85
|
|
|
extract($data, EXTR_OVERWRITE); |
86
|
|
|
|
87
|
|
|
include $this->template; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* 渲染模板内容 |
92
|
|
|
* @access public |
93
|
|
|
* @param string $content 模板内容 |
94
|
|
|
* @param array $data 模板变量 |
|
|
|
|
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
|
|
public function display(string $content, array $data = []): void |
98
|
|
|
{ |
99
|
|
|
$this->content = $content; |
100
|
|
|
|
101
|
|
|
extract($data, EXTR_OVERWRITE); |
102
|
|
|
eval('?>' . $this->content); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* 自动定位模板文件 |
107
|
|
|
* @access private |
108
|
|
|
* @param string $template 模板文件规则 |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
private function parseTemplate(string $template): string |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
if (empty($this->config['view_path'])) { |
114
|
|
|
$this->config['view_path'] = $this->app->getAppPath() . 'view' . DIRECTORY_SEPARATOR; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$request = $this->app['request']; |
118
|
|
|
|
119
|
|
|
// 获取视图根目录 |
120
|
|
|
if (strpos($template, '@')) { |
121
|
|
|
// 跨模块调用 |
122
|
|
|
list($app, $template) = explode('@', $template); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if ($this->config['view_base']) { |
126
|
|
|
// 基础视图目录 |
127
|
|
|
$app = isset($app) ? $app : $request->app(); |
128
|
|
|
$path = $this->config['view_base'] . ($app ? $app . DIRECTORY_SEPARATOR : ''); |
129
|
|
|
} else { |
130
|
|
|
$path = isset($app) ? $this->app->getBasePath() . $app . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR : $this->config['view_path']; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$depr = $this->config['view_depr']; |
134
|
|
|
|
135
|
|
|
if (0 !== strpos($template, '/')) { |
136
|
|
|
$template = str_replace(['/', ':'], $depr, $template); |
137
|
|
|
$controller = App::parseName($request->controller()); |
138
|
|
|
|
139
|
|
|
if ($controller) { |
140
|
|
|
if ('' == $template) { |
141
|
|
|
// 如果模板文件名为空 按照默认规则定位 |
142
|
|
|
$template = str_replace('.', DIRECTORY_SEPARATOR, $controller) . $depr . (1 == $this->config['auto_rule'] ? App::parseName($request->action(true)) : $request->action()); |
143
|
|
|
} elseif (false === strpos($template, $depr)) { |
144
|
|
|
$template = str_replace('.', DIRECTORY_SEPARATOR, $controller) . $depr . $template; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} else { |
148
|
|
|
$template = str_replace(['/', ':'], $depr, substr($template, 1)); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $path . ltrim($template, '/') . '.' . ltrim($this->config['view_suffix'], '.'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* 配置模板引擎 |
156
|
|
|
* @access private |
157
|
|
|
* @param array $config 参数 |
158
|
|
|
* @return void |
159
|
|
|
*/ |
160
|
|
|
public function config(array $config): void |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
$this->config = array_merge($this->config, $name); |
|
|
|
|
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* 获取模板引擎配置 |
167
|
|
|
* @access public |
168
|
|
|
* @param string $name 参数名 |
169
|
|
|
* @return void |
170
|
|
|
*/ |
171
|
|
|
public function getConfig(string $name) |
172
|
|
|
{ |
173
|
|
|
return $this->config[$name] ?? null; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|