-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathFISResource.class.php
444 lines (398 loc) · 16.4 KB
/
FISResource.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<?php
class FISResource {
const CSS_LINKS_HOOK = '<!--[FIS_CSS_LINKS_HOOK]-->';
const JS_SCRIPT_HOOK = '<!--[FIS_JS_SCRIPT_HOOK]-->';
const FRAMEWORK_HOOK = '<!--[FIS_FRAMEWORK_HOOK]-->';
const CSS_STYLE_HOOK = '<!--[FIS_CSS_STYLE_HOOK]-->';
private static $arrMap = array();
private static $arrLoaded = array();
private static $arrAsyncDeleted = array();
private static $arrStaticCollection = array();
//收集require.async组件
private static $arrRequireAsyncCollection = array();
private static $arrScriptPool = array();
private static $arrStylePool = array();
public static $framework = null;
//记录{%script%}, {%style%}的id属性
public static $cp = null;
//{%script%} {%style%}去重
public static $arrEmbeded = array();
public static function reset(){
self::$arrMap = array();
self::$arrLoaded = array();
self::$arrAsyncDeleted = array();
self::$arrStaticCollection = array();
// 常驻进程时,此变量需要销毁
self::$arrRequireAsyncCollection = array();
self::$arrScriptPool = array();
self::$arrStylePool = array();
self::$framework = null;
}
public static function addStatic($src, $typ) {
if (!$typ) {
preg_match('/\.(\w+)(?:\?[\s\S]+)?$/', $src, $m);
if (!$m) {
return;
}
$typ = $m[1];
}
$typ = trim($typ);
// 变量写错
if (!in_array($typ, array('js', 'css'))) {
return;
}
if (!is_array(self::$arrStaticCollection[$typ])) {
self::$arrStaticCollection[$typ] = array();
}
if (!in_array($src, self::$arrStaticCollection[$typ])) {
self::$arrStaticCollection[$typ][] = $src;
}
}
public static function styleHook(){
return self::CSS_STYLE_HOOK;
}
public static function cssHook(){
return self::CSS_LINKS_HOOK;
}
public static function jsHook(){
return self::JS_SCRIPT_HOOK;
}
public static function placeHolder($mode){
$placeHolder = '';
switch ($mode) {
case 'modjs':
$placeHolder = self::FRAMEWORK_HOOK;
break;
default:
break;
}
return $placeHolder;
}
//输出模板的最后,替换css hook为css标签集合,替换js hook为js代码
public static function renderResponse($strContent){
$cssIntPos = strpos($strContent, self::CSS_LINKS_HOOK);
if($cssIntPos !== false){
$strContent = substr_replace($strContent, self::render('css'), $cssIntPos, strlen(self::CSS_LINKS_HOOK));
}
$styleIntPos = strpos($strContent, self::CSS_STYLE_HOOK);
if($styleIntPos !== false){
$strContent = substr_replace($strContent, self::renderStylePool(), $styleIntPos, strlen(self::CSS_STYLE_HOOK));
}
$frameworkIntPos = strpos($strContent, self::FRAMEWORK_HOOK);
if($frameworkIntPos !== false){
$strContent = substr_replace($strContent, self::render('framework'), $frameworkIntPos, strlen(self::FRAMEWORK_HOOK));
}
$jsIntPos = strpos($strContent, self::JS_SCRIPT_HOOK);
if($jsIntPos !== false){
$jsContent = ($frameworkIntPos !== false) ? '' : self::getModJsHtml();
$jsContent .= self::render('js') . self::renderScriptPool();
$strContent = substr_replace($strContent, $jsContent, $jsIntPos, strlen(self::JS_SCRIPT_HOOK));
}
self::reset();
return $strContent;
}
//设置framewok mod.js
public static function setFramework($strFramework) {
self::$framework = $strFramework;
}
//返回静态资源uri,有包的时候,返回包的uri
public static function getUri($strName, $smarty) {
$intPos = strpos($strName, ':');
if($intPos === false){
$strNamespace = '__global__';
} else {
$strNamespace = substr($strName, 0, $intPos);
}
if(isset(self::$arrMap[$strNamespace]) || self::register($strNamespace, $smarty)) {
$arrMap = &self::$arrMap[$strNamespace];
if (isset($arrMap['res'][$strName])) {
$arrRes = &$arrMap['res'][$strName];
if (!array_key_exists('fis_debug', $_GET) && isset($arrRes['pkg'])) {
$arrPkg = &$arrMap['pkg'][$arrRes['pkg']];
return $arrPkg['uri'];
} else {
return $arrRes['uri'];
}
}
}
}
public static function getTemplate($strName, $smarty) {
//绝对路径
return $smarty->joined_template_dir . str_replace('/template', '', self::getUri($strName, $smarty));
}
private static function getModJsHtml(){
$html = '';
$resourceMap = self::getResourceMap();
$loadModJs = (self::$framework && (isset(self::$arrStaticCollection['js']) || $resourceMap));
//require.resourceMap要在mod.js加载以后执行
if ($loadModJs) {
$html .= '<script type="text/javascript" src="' . self::$framework . '"></script>' . PHP_EOL;
}
if ($resourceMap) {
$html .= '<script type="text/javascript">';
$html .= 'require.resourceMap('.$resourceMap.');';
$html .= '</script>';
}
return $html;
}
//渲染资源,将收集到的js css,变为html标签,异步js资源变为resorce map。
public static function render($type){
$html = '';
if ($type === 'js') {
if (isset(self::$arrStaticCollection['js'])) {
$arrURIs = &self::$arrStaticCollection['js'];
foreach ($arrURIs as $uri) {
if ($uri === self::$framework) {
continue;
}
$html .= '<script type="text/javascript" src="' . $uri . '"></script>' . PHP_EOL;
}
}
} else if($type === 'css'){
if(isset(self::$arrStaticCollection['css'])){
$arrURIs = &self::$arrStaticCollection['css'];
$html = '<link rel="stylesheet" type="text/css" href="' . implode('"/><link rel="stylesheet" type="text/css" href="', $arrURIs) . '"/>';
}
} else if($type === 'framework'){
$html .= self::getModJsHtml();
}
return $html;
}
// fix issues https://github.com/fex-team/fis-plus-smarty-plugin/issues/6
public static function addStylePool($code) {
self::$arrStylePool[] = $code;
}
//输出css,将页面的css源代码集合到pool,一起输出
public static function renderStylePool(){
$style = '<style>';
$style .= implode('', self::$arrStylePool);
$style .= '</style>';
return $style;
}
public static function addScriptPool($str, $priority) {
$priority = intval($priority);
if (!isset(self::$arrScriptPool[$priority])) {
self::$arrScriptPool[$priority] = array();
}
self::$arrScriptPool[$priority][] = $str;
}
//输出js,将页面的js源代码集合到pool,一起输出
public static function renderScriptPool(){
$html = '';
if(!empty(self::$arrScriptPool)) {
$priorities = array_keys(self::$arrScriptPool);
rsort($priorities);
foreach ($priorities as $priority) {
$html .= '<script type="text/javascript">!function(){' . implode("}();\n!function(){", self::$arrScriptPool[$priority]) . '}();</script>';
}
}
return $html;
}
//获取异步js资源集合,变为json格式的resourcemap
public static function getResourceMap() {
$ret = '';
$arrResourceMap = array();
$needPkg = !array_key_exists('fis_debug', $_GET);
if (isset(self::$arrRequireAsyncCollection['res'])) {
foreach (self::$arrRequireAsyncCollection['res'] as $id => $arrRes) {
$deps = array();
if (!empty($arrRes['deps'])) {
foreach ($arrRes['deps'] as $strName) {
if (preg_match('/\.(?:js|jsx|es|es6|es7|ts|tsx|coffee)$/i', $strName)) {
$deps[] = $strName;
}
}
}
$arrResourceMap['res'][$id] = array(
'url' => $arrRes['uri'],
);
if (!empty($arrRes['pkg']) && $needPkg) {
$arrResourceMap['res'][$id]['pkg'] = $arrRes['pkg'];
}
if (!empty($deps)) {
$arrResourceMap['res'][$id]['deps'] = $deps;
}
}
}
if (isset(self::$arrRequireAsyncCollection['pkg']) && $needPkg) {
foreach (self::$arrRequireAsyncCollection['pkg'] as $id => $arrRes) {
$arrResourceMap['pkg'][$id] = array(
'url'=> $arrRes['uri']
);
}
}
if (!empty($arrResourceMap)) {
$ret = str_replace('\\/', '/', json_encode($arrResourceMap));
}
return $ret;
}
//获取命名空间的map.json
public static function register($strNamespace, $smarty){
if($strNamespace === '__global__'){
$strMapName = 'map.json';
} else {
$strMapName = $strNamespace . '-map.json';
}
$arrConfigDir = $smarty->getConfigDir();
foreach ($arrConfigDir as $strDir) {
$strPath = preg_replace('/[\\/\\\\]+/', '/', $strDir . '/' . $strMapName);
if(is_file($strPath)){
self::$arrMap[$strNamespace] = json_decode(file_get_contents($strPath), true);
return true;
}
}
return false;
}
/**
* 分析组件依赖
* @param array $arrRes 组件信息
* @param Object $smarty smarty对象
* @param bool $async 是否异步
*/
private static function loadDeps($arrRes, $smarty, $async) {
//require.async
if (isset($arrRes['extras']) && isset($arrRes['extras']['async'])) {
foreach ($arrRes['extras']['async'] as $uri) {
self::load($uri, $smarty, true);
}
}
if(isset($arrRes['deps'])){
foreach ($arrRes['deps'] as $strDep) {
self::load($strDep, $smarty, $async);
}
}
}
/**
* 已经分析到的组件在后续被同步使用时在异步组里删除。
* @param $strName
*/
private static function delAsyncDeps($strName, $onlyDeps = false) {
if (isset(self::$arrAsyncDeleted[$strName])) {
return true;
} else {
self::$arrAsyncDeleted[$strName] = true;
$arrRes = self::$arrRequireAsyncCollection['res'][$strName];
//first deps
if (isset($arrRes['deps'])) {
foreach ($arrRes['deps'] as $strDep) {
if (isset(self::$arrRequireAsyncCollection['res'][$strDep])) {
self::delAsyncDeps($strDep);
}
}
}
if ($onlyDeps) {
return true;
}
//second self
if (isset($arrRes['pkg'])) {
$arrPkg = self::$arrRequireAsyncCollection['pkg'][$arrRes['pkg']];
$syncJs = isset(self::$arrStaticCollection['js']) ? self::$arrStaticCollection['js'] : array();
if ($arrPkg && !in_array($arrPkg['uri'], $syncJs)) {
//@TODO
//unset(self::$arrRequireAsyncCollection['pkg'][$arrRes['pkg']]);
foreach ($arrPkg['has'] as $strHas) {
if (isset(self::$arrRequireAsyncCollection['res'][$strHas])) {
self::$arrLoaded[$strName] = $arrPkg['uri'];
self::delAsyncDeps($strHas, true);
}
}
self::$arrStaticCollection['js'][] = $arrPkg['uri'];
} else {
//@TODO
//unset(self::$arrRequireAsyncCollection['res'][$strName]);
}
} else {
//已经分析过的并且在其他文件里同步加载的组件,重新收集在同步输出组
self::$arrStaticCollection['js'][] = $arrRes['uri'];
self::$arrLoaded[$strName] = $arrRes['uri'];
//@TODO
//unset(self::$arrRequireAsyncCollection['res'][$strName]);
}
}
}
/**
* 加载组件以及组件依赖
* @param $strName id
* @param $smarty smarty对象
* @param bool $async 是否为异步组件(only JS)
* @return mixed
*/
public static function load($strName, $smarty, $async = false){
if(isset(self::$arrLoaded[$strName])) {
//同步组件优先级比异步组件高
if (!$async && isset(self::$arrRequireAsyncCollection['res'][$strName])) {
self::delAsyncDeps($strName);
}
return self::$arrLoaded[$strName];
} else {
$intPos = strpos($strName, ':');
if($intPos === false){
$strNamespace = '__global__';
} else {
$strNamespace = substr($strName, 0, $intPos);
}
if(isset(self::$arrMap[$strNamespace]) || self::register($strNamespace, $smarty)){
$arrMap = &self::$arrMap[$strNamespace];
$arrPkg = null;
$arrPkgHas = array();
if(isset($arrMap['res'][$strName])) {
$arrRes = &$arrMap['res'][$strName];
if (array_key_exists('fis_debug', $_GET)) {
echo '<!--"'.$strName.'" loaded-->'."\n";
}
if(!array_key_exists('fis_debug', $_GET) && isset($arrRes['pkg'])){
$arrPkg = &$arrMap['pkg'][$arrRes['pkg']];
$strURI = $arrPkg['uri'];
foreach ($arrPkg['has'] as $strResId) {
self::$arrLoaded[$strResId] = $strURI;
}
foreach ($arrPkg['has'] as $strResId) {
$arrHasRes = &$arrMap['res'][$strResId];
$arrPkgHas[$strResId] = $arrHasRes;
self::loadDeps($arrHasRes, $smarty, $async);
}
} else {
$strURI = $arrRes['uri'];
self::$arrLoaded[$strName] = $strURI;
self::loadDeps($arrRes, $smarty, $async);
}
if ($async && $arrRes['type'] === 'js') {
if ($arrPkg) {
self::$arrRequireAsyncCollection['pkg'][$arrRes['pkg']] = $arrPkg;
self::$arrRequireAsyncCollection['res'] = array_merge((array)self::$arrRequireAsyncCollection['res'], $arrPkgHas);
} else {
self::$arrRequireAsyncCollection['res'][$strName] = $arrRes;
}
} else {
self::$arrStaticCollection[$arrRes['type']][] = $strURI;
}
return $strURI;
} else {
self::triggerError($strName, 'undefined resource "' . $strName . '"', E_USER_NOTICE);
}
} else {
self::triggerError($strName, 'missing map file of "' . $strNamespace . '"', E_USER_NOTICE);
}
}
self::triggerError($strName, 'unknown resource "' . $strName . '" load error', E_USER_NOTICE);
}
/**
* 用户代码自定义js组件,其没有对应的文件
* 只有有后缀的组件找不到时进行报错
* @param $strName 组件ID
* @param $strMessage 错误信息
* @param $errorLevel 错误level
*/
private static function triggerError($strName, $strMessage, $errorLevel) {
$arrExt = array(
'js',
'css',
'tpl',
'html',
'xhtml',
);
if (preg_match('/\.('.implode('|', $arrExt).')$/', $strName)) {
trigger_error(date('Y-m-d H:i:s') . ' ' . $strName . ' ' . $strMessage, $errorLevel);
}
}
}