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