2
2
3
3
require_once ('BriskResourceCollector.php ' );
4
4
require_once ('BriskConfig.php ' );
5
+ require_once ('BriskUtils.php ' );
5
6
6
7
/**
7
8
* Checks a string for UTF-8 encoding.
@@ -65,7 +66,7 @@ function convertToUtf8($string) {
65
66
66
67
/**
67
68
* Class BriskPage
68
- * 构造pagelet的html以及所需要的静态资源json
69
+ * 构造 pagelet 的html以及所需要的静态资源json
69
70
*/
70
71
class BriskPage {
71
72
@@ -79,15 +80,13 @@ class BriskPage {
79
80
private static $ cdn ;
80
81
private static $ title = '' ;
81
82
82
- /* render mode */
83
+ // render mode
83
84
private static $ mode = null ;
84
85
85
- /* default render mode */
86
+ // default render mode
86
87
private static $ defaultMode = null ;
87
88
88
- /**
89
- * save filters
90
- */
89
+ // save filters
91
90
private static $ filter ;
92
91
93
92
private static $ context = array ();
@@ -146,9 +145,7 @@ public static function init($defaultMode) {
146
145
self ::$ defaultMode = self ::MODE_NOSCRIPT ;
147
146
}
148
147
149
- $ isAjax = isset ($ _SERVER ['HTTP_X_REQUESTED_WITH ' ])
150
- && (strtolower ($ _SERVER ['HTTP_X_REQUESTED_WITH ' ]) === 'xmlhttprequest ' );
151
-
148
+ $ isAjax = BriskUtils::isAjax ();
152
149
if ($ isAjax ) {
153
150
self ::setMode (self ::MODE_QUICKLING );
154
151
} else {
@@ -262,13 +259,53 @@ public static function getCDN() {
262
259
return self ::$ cdn ;
263
260
}
264
261
262
+ // 构建需要打印在页面中的资源表, 这里全部打印
263
+ protected static function buildResourceMap ($ map ) {
264
+ $ ret = array (
265
+ BriskConfig::TYPE_CSS => array (),
266
+ BriskConfig::TYPE_JS => array ()
267
+ );
268
+ foreach ($ map [BriskConfig::TYPE_JS ] as $ symbol ) {
269
+ if (preg_match ('/^p(\d)+$/ ' , $ symbol ) !== 1 ) {
270
+ $ js = BriskResourceCollector::getResource (BriskConfig::TYPE_JS , $ symbol );
271
+ $ ret [BriskConfig::TYPE_JS ][$ symbol ] = array (
272
+ BriskConfig::ATTR_URI => self ::getCDN () . $ js [BriskConfig::ATTR_URI ],
273
+ BriskConfig::ATTR_DEP => $ js [BriskConfig::ATTR_DEP ],
274
+ BriskConfig::ATTR_CSS => $ js [BriskConfig::ATTR_CSS ]
275
+ );
276
+ }
277
+ }
278
+
279
+ foreach ($ map [BriskConfig::TYPE_CSS ] as $ symbol ) {
280
+ if (preg_match ('/^p(\d)+$/ ' , $ symbol ) !== 1 ) {
281
+ $ css = BriskResourceCollector::getResource (BriskConfig::TYPE_CSS , $ symbol );
282
+ $ ret [BriskConfig::TYPE_CSS ][$ symbol ] = array (
283
+ BriskConfig::ATTR_URI => self ::getCDN () . $ css [BriskConfig::ATTR_URI ],
284
+ BriskConfig::ATTR_DEP => $ css [BriskConfig::ATTR_DEP ],
285
+ BriskConfig::ATTR_CSS => $ css [BriskConfig::ATTR_CSS ]
286
+ );
287
+ }
288
+ }
289
+
290
+ return array (
291
+ BriskConfig::TYPE_JS => array_merge (
292
+ $ ret [BriskConfig::TYPE_JS ],
293
+ $ map [BriskConfig::ATTR_ASYNCLOADED ][BriskConfig::TYPE_JS ]
294
+ ),
295
+ BriskConfig::TYPE_CSS => array_merge (
296
+ $ ret [BriskConfig::TYPE_CSS ],
297
+ $ map [BriskConfig::ATTR_ASYNCLOADED ][BriskConfig::TYPE_CSS ]
298
+ )
299
+ );
300
+ }
301
+
265
302
/**
266
303
* 根据资源表返回页面中的js片段
267
304
* @param {array} $res
268
305
* @return {string}
269
306
*/
270
- private static function genJsFragment ($ res ) {
271
- $ resourceMap = $ res [BriskConfig:: ATTR_ASYNCLOADED ] ;
307
+ protected static function genJsFragment ($ res ) {
308
+ $ resourceMap = self :: buildResourceMap ( $ res ) ;
272
309
$ loadModJs = BriskResourceCollector::getFramework ();
273
310
274
311
$ code = '<!-- brisk render js start --> ' ;
@@ -316,7 +353,7 @@ private static function genJsFragment($res) {
316
353
* @param {array} $res
317
354
* @return {string}
318
355
*/
319
- private static function genCssFragment ($ res ) {
356
+ protected static function genCssFragment ($ res ) {
320
357
$ code = '<!-- brisk render css start --> ' ;
321
358
322
359
if (!empty ($ res [BriskConfig::TYPE_CSS ])) {
@@ -348,10 +385,10 @@ private static function genCssFragment($res) {
348
385
* 将资源渲染到页面替换页面中的占位符
349
386
* @param {string} $html
350
387
* @param {array} $arr
351
- * @param {bool} $clean_hook 是否把页面中的占位符清除
388
+ * @param {bool} $cleanHook 是否把页面中的占位符清除
352
389
* @return mixed
353
390
*/
354
- private static function renderStatic ($ html , $ arr , $ clean_hook = false ) {
391
+ protected static function renderStatic ($ html , $ arr , $ cleanHook = false ) {
355
392
if (!empty ($ arr )) {
356
393
$ code = self ::genJsFragment ($ arr );
357
394
$ html = str_replace (self ::JS_HOOK , $ code . self ::JS_HOOK , $ html );
@@ -360,7 +397,7 @@ private static function renderStatic($html, $arr, $clean_hook = false) {
360
397
$ html = str_replace (self ::CSS_HOOK , $ code . self ::CSS_HOOK , $ html );
361
398
}
362
399
363
- if ($ clean_hook ) {
400
+ if ($ cleanHook ) {
364
401
$ html = str_replace (array (self ::CSS_HOOK , self ::JS_HOOK ), '' , $ html );
365
402
}
366
403
@@ -372,7 +409,7 @@ private static function renderStatic($html, $arr, $clean_hook = false) {
372
409
* @param {string} $html html页面内容
373
410
* @return mixed
374
411
*/
375
- private static function insertPageletGroup ($ html ) {
412
+ protected static function insertPageletGroup ($ html ) {
376
413
// 无widget直接返回
377
414
if (empty (self ::$ pageletGroup )) {
378
415
return $ html ;
@@ -458,20 +495,20 @@ public static function render($html) {
458
495
$ res = array ();
459
496
460
497
// 合并资源
461
- foreach (self ::$ inner_widget [$ mode ] as $ item ) {
462
- $ res = self ::mergeResource ($ res , $ item );
498
+ foreach (self ::$ inner_widget [$ mode ] as $ widgetResources ) {
499
+ $ res = self ::mergeResource ($ res , $ widgetResources );
463
500
}
464
501
465
502
// 为不是普通加载模式的资源添加cdn
466
- if ($ mode !== self ::MODE_NOSCRIPT ) {
467
- foreach ((array )$ res ['JS ' ] as $ symbol => $ js ) {
468
- $ res ['JS ' ][$ symbol ]['uri ' ] = self ::getCDN () . $ js ['uri ' ];
469
- }
470
-
471
- foreach ((array )$ res ['CSS ' ] as $ symbol => $ css ) {
472
- $ res ['CSS ' ][$ symbol ]['uri ' ] = self ::getCDN () . $ css ['uri ' ];
473
- }
474
- }
503
+ // if ($mode !== self::MODE_NOSCRIPT) {
504
+ // foreach ((array)$res['JS'] as $symbol => $js) {
505
+ // $res['JS'][$symbol]['uri'] = self::getCDN() . $js['uri'];
506
+ // }
507
+ //
508
+ // foreach ((array)$res['CSS'] as $symbol => $css) {
509
+ // $res['CSS'][$symbol]['uri'] = self::getCDN() . $css['uri'];
510
+ // }
511
+ // }
475
512
476
513
// tpl信息没有必要打到页面
477
514
switch ($ mode ) {
@@ -484,7 +521,32 @@ public static function render($html) {
484
521
case self ::MODE_QUICKLING :
485
522
// 返回json
486
523
header ('Content-Type: text/json; charset=utf-8 ' );
487
- $ res = self ::mergeResource ($ res , BriskResourceCollector::getPageStaticResource ());
524
+ $ all_static = BriskResourceCollector::getPageStaticResource ();
525
+ $ res = self ::mergeResource ($ all_static , $ res );
526
+
527
+ foreach ($ res [BriskConfig::TYPE_JS ] as $ index => $ symbol ) {
528
+ if (preg_match ('/^p(\d)+$/ ' , $ symbol ) === 1 ) {
529
+ $ js = BriskResourceCollector::getPackage ($ symbol );
530
+ } else {
531
+ $ js = BriskResourceCollector::getResource (BriskConfig::TYPE_JS , $ symbol );
532
+ }
533
+
534
+ $ js ['uri ' ] = self ::getCDN () . $ js ['uri ' ];
535
+ $ js ['id ' ] = $ symbol ;
536
+ $ res [BriskConfig::TYPE_JS ][$ index ] = $ js ;
537
+ }
538
+
539
+ foreach ($ res [BriskConfig::TYPE_CSS ] as $ index => $ symbol ) {
540
+ if (preg_match ('/^p(\d)+$/ ' , $ symbol ) === 1 ) {
541
+ $ css = BriskResourceCollector::getPackage ($ symbol );
542
+ } else {
543
+ $ css = BriskResourceCollector::getResource (BriskConfig::TYPE_CSS , $ symbol );
544
+ }
545
+
546
+ $ css ['uri ' ] = self ::getCDN () . $ css ['uri ' ];
547
+ $ css ['id ' ] = $ symbol ;
548
+ $ res [BriskConfig::TYPE_CSS ][$ index ] = $ css ;
549
+ }
488
550
489
551
if ($ res ['script ' ]) {
490
552
$ res ['script ' ] = convertToUtf8 (implode ("\n" , $ res ['script ' ]));
@@ -569,8 +631,8 @@ public static function render($html) {
569
631
570
632
/**
571
633
* WIDGET START
572
- * 解析参数, 收集widget所用到的静态资源
573
- * @param {string} $id pagelet id
634
+ * 解析参数, 收集widget所用到的静态资源
635
+ * @param {string} $id 分片id
574
636
* @param {?string} $mode
575
637
* @param {string} $group
576
638
* @return {bool}
@@ -584,25 +646,24 @@ public static function widgetStart($id, $mode = null, $group = null) {
584
646
$ widgetMode = self ::$ mode ;
585
647
}
586
648
587
- // 记录当前 pagelet id
649
+ // 记录当前分片id
588
650
self ::$ pageletId = $ id ;
589
651
590
- $ parent_id = $ hasParent ? self ::$ context ['id ' ] : '' ;
591
- $ qk_flag = (self ::$ mode == self ::MODE_QUICKLING ? '_qk_ ' : '' );
652
+ $ parentId = $ hasParent ? self ::$ context ['id ' ] : '' ;
653
+ $ qk_flag = (self ::$ mode === self ::MODE_QUICKLING ? '_qk_ ' : '' );
592
654
593
655
//
594
- $ id = empty ($ id ) ? '__elm_ ' . $ parent_id . '_ ' . $ qk_flag . self ::$ _session_id ++ : $ id ;
656
+ $ id = empty ($ id ) ? '__elm_ ' . $ parentId . '_ ' . $ qk_flag . self ::$ _session_id ++ : $ id ;
595
657
596
658
597
659
$ parent = self ::$ context ;
598
660
$ hasParent = !empty ($ parent );
599
661
600
662
$ hit = true ;
601
-
602
663
$ context = array (
603
- 'id ' => $ id , //widget id
604
- 'mode ' => $ widgetMode , // 当前widget的mode
605
- 'hit ' => $ hit // 是否命中
664
+ 'id ' => $ id , // widget id
665
+ 'mode ' => $ widgetMode , // 当前widget的mode
666
+ 'hit ' => $ hit // 是否命中
606
667
);
607
668
608
669
if ($ hasParent ) {
@@ -638,7 +699,7 @@ public static function widgetStart($id, $mode = null, $group = null) {
638
699
}
639
700
} else if ($ widgetMode === self ::MODE_QUICKLING ) {
640
701
// 渲染模式不是quickling时,可以认为是首次渲染
641
- if (self ::$ pageletId && self ::$ mode != self ::MODE_QUICKLING ) {
702
+ if (self ::$ pageletId && self ::$ mode !== self ::MODE_QUICKLING ) {
642
703
if (!$ group ) {
643
704
echo '<textarea class="g_bigrender" style="display:none;"> '
644
705
.'BigPipe.asyncLoad({id: " ' .$ id .'"}); '
@@ -676,7 +737,7 @@ public static function widgetStart($id, $mode = null, $group = null) {
676
737
677
738
/**
678
739
* WIDGET END
679
- * 收集html, 收集静态资源
740
+ * 收集html, 收集静态资源
680
741
*/
681
742
public static function widgetEnd ($ id = null ) {
682
743
$ ret = true ;
@@ -700,7 +761,7 @@ public static function widgetEnd($id = null) {
700
761
if (!$ hasParent ) {
701
762
$ widget = BriskResourceCollector::widgetEnd ();
702
763
// end
703
- if ($ widgetMode == self ::MODE_BIGRENDER ) {
764
+ if ($ widgetMode === self ::MODE_BIGRENDER ) {
704
765
$ widget_style = $ widget ['style ' ];
705
766
$ widget_script = $ widget ['script ' ];
706
767
// 内联css和script放到注释里面, 不需要收集
@@ -722,8 +783,6 @@ public static function widgetEnd($id = null) {
722
783
$ out
723
784
);
724
785
725
- $ html = '' ;
726
-
727
786
echo '--></code></div> ' ;
728
787
729
788
// 收集外链的js和css
@@ -754,7 +813,7 @@ public static function widgetEnd($id = null) {
754
813
}
755
814
756
815
if ($ widgetMode !== self ::MODE_BIGRENDER ) {
757
- echo '! MODE_BIGRENDER ' ;
816
+ // echo '! MODE_BIGRENDER';
758
817
echo '</div> ' ;
759
818
}
760
819
}
0 commit comments