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