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
|
|
|
public function __construct(string $name, Application $application, Request $request, ?string $defaultModule = null) |
68
|
|
|
{ |
69
|
|
|
parent::__construct('', []); |
70
|
|
|
$this->application = $application; |
71
|
|
|
$this->request = $request; |
72
|
|
|
list($this->module, $this->group, $this->name) = $application->parseRouteName($name, $defaultModule, 'default'); |
73
|
|
|
$this->group = $request->getAttribute('group', $this->group); |
74
|
|
|
$this->config = $this->getModuleConfig($this->module); |
75
|
|
|
$this->uriName = TemplateUtil::getSafeUriName($this->application, $this->module); |
76
|
|
|
$this->value = []; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function getModuleConfig(?string $module) |
80
|
|
|
{ |
81
|
|
|
return TemplateUtil::getConfig($this->application, $module); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function createCompiler():Compiler |
85
|
|
|
{ |
86
|
|
|
$compiler = new ModuleTemplateCompiler; |
87
|
|
|
$compiler->init(); |
88
|
|
|
return $compiler; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function getModuleStaticPath(?string $module, ?string $name = null) |
92
|
|
|
{ |
93
|
|
|
$name = $name ?? $this->getModuleConfig($module)['static']; |
94
|
|
|
return $this->getResource($module)->getResourcePath($this->getTemplatePath().'/'.$name) ?? ''; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
protected function getModuleStaticOutputPath(?string $module, ?string $name = null) |
99
|
|
|
{ |
100
|
|
|
$config = $this->getModuleConfig($module); |
101
|
|
|
$name = $name ?? $this->getModuleConfig($module)['static']; |
102
|
|
|
$path = $config['assets-path'].'/'. $this->getModuleUriName($module) .'/'.$name; |
103
|
|
|
FileSystem::make($path); |
104
|
|
|
return $path; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function getModuleUriName(?string $module) |
108
|
|
|
{ |
109
|
|
|
$config = $this->getModuleConfig($module); |
110
|
|
|
return $config['uri-name']; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected function getResource(?string $module): ApplicationResource |
114
|
|
|
{ |
115
|
|
|
return TemplateUtil::getResource($this->application, $module ?? $this->module); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected function getTemplatePath() |
119
|
|
|
{ |
120
|
|
|
return TemplateUtil::getTemplatePath($this->application); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
protected function getStaticModulePrefix(?string $module = null, ?string $name = null) |
124
|
|
|
{ |
125
|
|
|
if ($module === null) { |
126
|
|
|
$module = $this->module; |
127
|
|
|
} |
128
|
|
|
$this->prepareStaticModuleSource($module, $name); |
129
|
|
|
$config = TemplateUtil::getConfig($this->application, $module); |
130
|
|
|
$name = $name ?? $config['static']; |
131
|
|
|
return $this->getModuleStaticAssetRoot($module) .'/'.$this->getModuleUriName($module). '/'.$name; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
protected function getModulePrefix(?string $module = null) |
135
|
|
|
{ |
136
|
|
|
if ($module === null) { |
137
|
|
|
$module = $this->module; |
138
|
|
|
} |
139
|
|
|
return $this->getModuleAssetRoot($module) .'/'.$this->getModuleUriName($module); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
protected function getModuleAssetRoot(?string $module) |
143
|
|
|
{ |
144
|
|
|
return TemplateUtil::getRequestAsset($this->application, $this->request, $module); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
protected function getModuleStaticAssetRoot(?string $module) |
148
|
|
|
{ |
149
|
|
|
return TemplateUtil::getStaticRequestAsset($this->application, $this->request, $module); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
protected function prepareStaticModuleSource(?string $module, ?string $name = null) |
153
|
|
|
{ |
154
|
|
|
$static = $this->getModuleStaticPath($module, $name); |
155
|
|
|
if (SUDA_DEBUG && is_dir($static) && !in_array($static, static::$copyedStaticPaths)) { |
156
|
|
|
$from = $static; |
157
|
|
|
$to = $this->getModuleStaticOutputPath($module, $name); |
158
|
|
|
$time = sprintf('copy template static source %s => %s ', $from, $to); |
159
|
|
|
$this->application->debug()->time($time); |
160
|
|
|
if (FileSystem::copyDir($from, $to)) { |
161
|
|
|
$this->application->debug()->timeEnd($time); |
162
|
|
|
static::$copyedStaticPaths[] = $static; |
163
|
|
|
} else { |
164
|
|
|
$this->application->debug()->warning('Failed: '.$time); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get 应用环境 |
171
|
|
|
* |
172
|
|
|
* @return Application |
173
|
|
|
*/ |
174
|
|
|
public function getApplication() |
175
|
|
|
{ |
176
|
|
|
return $this->application; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get 请求信息 |
181
|
|
|
* |
182
|
|
|
* @return Request |
183
|
|
|
*/ |
184
|
|
|
public function getRequest() |
185
|
|
|
{ |
186
|
|
|
return $this->request; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|