1
|
|
|
<?php |
2
|
|
|
namespace suda\application\template; |
3
|
|
|
|
4
|
|
|
use suda\framework\Request; |
5
|
|
|
use suda\application\Application; |
6
|
|
|
use suda\framework\filesystem\FileSystem; |
7
|
|
|
use suda\application\template\compiler\Compiler; |
8
|
|
|
use suda\application\Resource as ApplicationResource; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* 模块模板 |
12
|
|
|
*/ |
13
|
|
|
class ModuleTemplateBase extends CompilableTemplate |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* 模板模块 |
17
|
|
|
* |
18
|
|
|
* @var string|null |
19
|
|
|
*/ |
20
|
|
|
protected $module = null; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* 路由组 |
24
|
|
|
* |
25
|
|
|
* @var string|null |
26
|
|
|
*/ |
27
|
|
|
protected $group = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* 安全URI路径 |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $uriName = 'application'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* 模板名 |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $name; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* 应用环境 |
45
|
|
|
* |
46
|
|
|
* @var Application |
47
|
|
|
*/ |
48
|
|
|
protected $application; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* 配置文件 |
52
|
|
|
* |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected $config; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* 请求信息 |
59
|
|
|
* |
60
|
|
|
* @var Request |
61
|
|
|
*/ |
62
|
|
|
protected $request; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* ModuleTemplateBase constructor. |
66
|
|
|
* @param string $name 模板描述符 |
67
|
|
|
* @param Application $application |
68
|
|
|
* @param Request $request |
69
|
|
|
* @param string|null $defaultModule |
70
|
|
|
*/ |
71
|
|
|
public function __construct(string $name, Application $application, Request $request, ?string $defaultModule = null) |
72
|
|
|
{ |
73
|
|
|
parent::__construct('', []); |
74
|
|
|
$this->application = $application; |
75
|
|
|
$this->request = $request; |
76
|
|
|
list($this->module, $this->group, $this->name) = $application->parseRouteName($name, $defaultModule, 'default'); |
77
|
|
|
$this->group = $request->getAttribute('group', $this->group); |
78
|
|
|
$this->config = $this->getModuleConfig($this->module); |
79
|
|
|
$this->uriName = TemplateUtil::getSafeUriName($this->application, $this->module); |
80
|
|
|
$this->value = []; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string|null $module |
85
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
|
|
protected function getModuleConfig(?string $module) |
88
|
|
|
{ |
89
|
|
|
return TemplateUtil::getConfig($this->application, $module); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return Compiler |
94
|
|
|
*/ |
95
|
|
|
protected function compiler() |
96
|
|
|
{ |
97
|
|
|
// 自定义编译参数 |
98
|
|
|
if (array_key_exists('compile', $this->config)) { |
99
|
|
|
return $this->createCompiler(); |
100
|
|
|
} |
101
|
|
|
return parent::compiler(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return Compiler |
106
|
|
|
*/ |
107
|
|
|
protected function createCompiler(): Compiler |
108
|
|
|
{ |
109
|
|
|
$compiler = new ModuleTemplateCompiler; |
110
|
|
|
if (array_key_exists('compile', $this->config)) { |
111
|
|
|
$compiler->setConfig($this->config['compile']); |
112
|
|
|
} |
113
|
|
|
$compiler->init(); |
114
|
|
|
$this->application->event()->exec( |
115
|
|
|
'application:template:compile::create', |
116
|
|
|
[$compiler, $this->config, $this->application] |
117
|
|
|
); |
118
|
|
|
return $compiler; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string|null $module |
124
|
|
|
* @param string|null $name |
125
|
|
|
* @return string|null |
126
|
|
|
*/ |
127
|
|
|
protected function getModuleStaticPath(?string $module, ?string $name = null) |
128
|
|
|
{ |
129
|
|
|
$name = $name ?? $this->getModuleConfig($module)['static']; |
130
|
|
|
return $this->getResource($module)->getResourcePath($this->getTemplatePath().'/'.$name) ?? ''; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string|null $module |
136
|
|
|
* @param string|null $name |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
protected function getModuleStaticOutputPath(?string $module, ?string $name = null) |
140
|
|
|
{ |
141
|
|
|
$config = $this->getModuleConfig($module); |
142
|
|
|
$name = $name ?? $this->getModuleConfig($module)['static']; |
143
|
|
|
$path = $config['assets-path'].'/'. $this->getModuleUriName($module) .'/'.$name; |
144
|
|
|
FileSystem::make($path); |
145
|
|
|
return $path; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string|null $module |
150
|
|
|
* @return mixed |
151
|
|
|
*/ |
152
|
|
|
protected function getModuleUriName(?string $module) |
153
|
|
|
{ |
154
|
|
|
$config = $this->getModuleConfig($module); |
155
|
|
|
return $config['uri-name']; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param string|null $module |
160
|
|
|
* @return ApplicationResource |
161
|
|
|
*/ |
162
|
|
|
protected function getResource(?string $module): ApplicationResource |
163
|
|
|
{ |
164
|
|
|
return TemplateUtil::getResource($this->application, $module ?? $this->module); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
|
|
protected function getTemplatePath() |
171
|
|
|
{ |
172
|
|
|
return TemplateUtil::getTemplatePath($this->application); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param string|null $module |
177
|
|
|
* @param string|null $name |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
protected function getStaticModulePrefix(?string $module = null, ?string $name = null) |
181
|
|
|
{ |
182
|
|
|
if ($module === null) { |
183
|
|
|
$module = $this->module; |
184
|
|
|
} |
185
|
|
|
$this->prepareStaticModuleSource($module, $name); |
186
|
|
|
$config = TemplateUtil::getConfig($this->application, $module); |
187
|
|
|
$name = $name ?? $config['static']; |
188
|
|
|
return $this->getModuleStaticAssetRoot($module) .'/'.$this->getModuleUriName($module). '/'.$name; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param string|null $module |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
|
|
protected function getModulePrefix(?string $module = null) |
196
|
|
|
{ |
197
|
|
|
if ($module === null) { |
198
|
|
|
$module = $this->module; |
199
|
|
|
} |
200
|
|
|
return $this->getModuleAssetRoot($module) .'/'.$this->getModuleUriName($module); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param string|null $module |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
|
protected function getModuleAssetRoot(?string $module) |
208
|
|
|
{ |
209
|
|
|
return TemplateUtil::getRequestAsset($this->application, $this->request, $module); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param string|null $module |
214
|
|
|
* @return string |
215
|
|
|
*/ |
216
|
|
|
protected function getModuleStaticAssetRoot(?string $module) |
217
|
|
|
{ |
218
|
|
|
return TemplateUtil::getStaticRequestAsset($this->application, $this->request, $module); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param string|null $module |
223
|
|
|
* @param string|null $name |
224
|
|
|
*/ |
225
|
|
|
protected function prepareStaticModuleSource(?string $module, ?string $name = null) |
226
|
|
|
{ |
227
|
|
|
$copySource = $this->application->conf('copy-static-source', SUDA_DEBUG); |
228
|
|
|
if ($copySource) { |
229
|
|
|
$static = $this->getModuleStaticPath($module, $name); |
230
|
|
|
$staticCopyed = is_dir($static) && in_array($static, static::$copyedStaticPaths); |
231
|
|
|
if ($staticCopyed === false) { |
232
|
|
|
$from = $static; |
233
|
|
|
$to = $this->getModuleStaticOutputPath($module, $name); |
234
|
|
|
$time = sprintf('copy template static source %s => %s ', $from, $to); |
235
|
|
|
$this->application->debug()->time($time); |
236
|
|
|
if (FileSystem::copyDir($from, $to)) { |
237
|
|
|
$this->application->debug()->timeEnd($time); |
238
|
|
|
static::$copyedStaticPaths[] = $static; |
239
|
|
|
} else { |
240
|
|
|
$this->application->debug()->warning('Failed: '.$time); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @return Application |
248
|
|
|
*/ |
249
|
|
|
public function getApplication(): Application |
250
|
|
|
{ |
251
|
|
|
return $this->application; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @return Request |
256
|
|
|
*/ |
257
|
|
|
public function getRequest(): Request |
258
|
|
|
{ |
259
|
|
|
return $this->request; |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|