1
|
|
|
<?php |
2
|
|
|
namespace suda\application\template; |
3
|
|
|
|
4
|
|
|
use function array_key_exists; |
5
|
|
|
use function constant; |
6
|
|
|
use suda\framework\Config; |
7
|
|
|
use suda\framework\Request; |
8
|
|
|
use suda\application\Resource as ApplicationResource; |
9
|
|
|
use suda\application\Application; |
10
|
|
|
use suda\framework\filesystem\FileSystem; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* 模板 |
14
|
|
|
*/ |
15
|
|
|
class TemplateUtil |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* 获取配置 |
19
|
|
|
* |
20
|
|
|
* @param Application $application |
21
|
|
|
* @param string|null $module |
22
|
|
|
* @return mixed |
23
|
|
|
*/ |
24
|
|
|
public static function getConfig(Application $application, ?string $module) |
25
|
|
|
{ |
26
|
|
|
$configPath = static::getResource($application, $module) |
27
|
|
|
->getConfigResourcePath(static::getTemplatePath($application).'/config'); |
28
|
|
|
return static::getConfigFromPath($application, $module, $configPath); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* 获取配置 |
33
|
|
|
* @param Application $application |
34
|
|
|
* @param string|null $module |
35
|
|
|
* @param string $path |
36
|
|
|
* @return array|null |
37
|
|
|
*/ |
38
|
|
|
public static function getConfigFromPath(Application $application, ?string $module, ?string $path) { |
39
|
|
|
$config = []; |
40
|
|
|
if ($path !== null) { |
41
|
|
|
$config = Config::loadConfig($path) ?? []; |
42
|
|
|
} |
43
|
|
|
$config = static::fixConfig($application, $module, $config); |
44
|
|
|
return $config; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* 修正配置 |
49
|
|
|
* |
50
|
|
|
* @param Application $application |
51
|
|
|
* @param string|null $module |
52
|
|
|
* @param array $config |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
protected static function fixConfig(Application $application, ?string $module, array $config) |
56
|
|
|
{ |
57
|
|
|
if (!array_key_exists('assets-prefix', $config)) { |
58
|
|
|
$config['assets-prefix'] = defined('SUDA_ASSETS') ? constant('SUDA_ASSETS'): 'assets'; |
59
|
|
|
} |
60
|
|
|
if (!array_key_exists('static', $config)) { |
61
|
|
|
$config['static'] = 'static'; |
62
|
|
|
} |
63
|
|
|
if (!array_key_exists('assets-path', $config)) { |
64
|
|
|
$config['assets-path'] = SUDA_PUBLIC. '/'.$config['assets-prefix']; |
65
|
|
|
} |
66
|
|
|
if (!array_key_exists('static-name', $config)) { |
67
|
|
|
$config['uri-name'] = static::getSafeUriName($application, $module); |
68
|
|
|
} |
69
|
|
|
return $config; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* 获取安全路径名 |
74
|
|
|
* |
75
|
|
|
* @param Application $application |
76
|
|
|
* @param string|null $module |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public static function getSafeUriName(Application $application, ?string $module) |
80
|
|
|
{ |
81
|
|
|
if ($module !== null) { |
82
|
|
|
$moduleObj = $application->find($module); |
83
|
|
|
if ($moduleObj !== null) { |
84
|
|
|
return $moduleObj->getUriSafeName(); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
return 'application'; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* 获取资源静态前缀 |
92
|
|
|
* |
93
|
|
|
* @param Application $application |
94
|
|
|
* @param string|null $module |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public static function getAssetStaticRoot(Application $application, ?string $module):string |
98
|
|
|
{ |
99
|
|
|
$config = static::getConfig($application, $module); |
100
|
|
|
return '/'.$config['assets-prefix'].'/'.$config['static']; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* 获取资源前缀 |
105
|
|
|
* |
106
|
|
|
* @param Application $application |
107
|
|
|
* @param string|null $module |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public static function getAssetRoot(Application $application, ?string $module):string |
111
|
|
|
{ |
112
|
|
|
$config = static::getConfig($application, $module); |
113
|
|
|
return '/'.$config['assets-prefix']; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* 获取请求资源头 |
118
|
|
|
* |
119
|
|
|
* @param Application $application |
120
|
|
|
* @param Request $request |
121
|
|
|
* @param string|null $module |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
public static function getRequestAsset(Application $application, Request $request, ?string $module = null):string |
125
|
|
|
{ |
126
|
|
|
$assetRoot = static::getAssetRoot($application, $module); |
127
|
|
|
if (in_array($request->getIndex(), $application->conf('index', ['/index.php']))) { |
128
|
|
|
$name = static::isAssetsWritable($application, $module) |
129
|
|
|
? dirname($request->getIndex()):$request->getIndex(); |
130
|
|
|
return rtrim(str_replace('\\', '/', $name), '/').$assetRoot; |
131
|
|
|
} |
132
|
|
|
return rtrim(str_replace('\\', '/', $request->getIndex()), '/').$assetRoot; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* 是否写入资源文件 |
137
|
|
|
* |
138
|
|
|
* @param Application $application |
139
|
|
|
* @param string|null $module |
140
|
|
|
* @return boolean |
141
|
|
|
*/ |
142
|
|
|
public static function isAssetsWritable(Application $application, ?string $module):bool |
143
|
|
|
{ |
144
|
|
|
if ($application->conf('assets-auto-write', true) === false) { |
145
|
|
|
return false; |
146
|
|
|
} |
147
|
|
|
$config = static::getConfig($application, $module); |
148
|
|
|
return FileSystem::isWritable($config['assets-path']); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* 获取请求资源头 |
153
|
|
|
* |
154
|
|
|
* @param Application $application |
155
|
|
|
* @param Request $request |
156
|
|
|
* @param string|null $module |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
public static function getStaticRequestAsset( |
160
|
|
|
Application $application, |
161
|
|
|
Request $request, |
162
|
|
|
?string $module = null |
163
|
|
|
):string { |
164
|
|
|
$assetRoot = static::getAssetRoot($application, $module); |
165
|
|
|
$name = static::isAssetsWritable($application, $module) ? dirname($request->getIndex()):$request->getIndex(); |
166
|
|
|
return rtrim(str_replace('\\', '/', $name), '/').$assetRoot; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* 获取模板资源 |
171
|
|
|
* |
172
|
|
|
* @param Application $application |
173
|
|
|
* @param string|null $module |
174
|
|
|
* @return ApplicationResource |
175
|
|
|
*/ |
176
|
|
|
public static function getResource(Application $application, ?string $module): ApplicationResource |
177
|
|
|
{ |
178
|
|
|
if ($module !== null && ($moduleObj = $application->find($module))) { |
179
|
|
|
return $moduleObj->getResource(); |
180
|
|
|
} |
181
|
|
|
return $application->getResource(); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* 获取模板路径 |
186
|
|
|
* |
187
|
|
|
* @param Application $application |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
|
|
public static function getTemplatePath(Application $application) |
191
|
|
|
{ |
192
|
|
|
return 'template/'.$application->getStyle(); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|