Skip to content

Commit c81f883

Browse files
author
AceMood
committed
widget support
1 parent 3186ad2 commit c81f883

6 files changed

+173
-49
lines changed

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
## What's brisky
22

3-
**brisky** is a static resource load framework evolute from fisp. We have some special structure in our resource map which normally named **resource.json**. And our module loader of browser side is [kernel.js](https://github.com/AceMood/kerneljs/), which is flexible enough to load js and css file as module dynamically.
3+
**brisky** is a static resource load framework evoluted from fisp. We have some special structure in our resource map which normally named **resource.json**. And our module loader of browser side is [kernel.js](https://github.com/AceMood/kerneljs/), which is flexible enough to load js and css file as module dynamically.
44

55
## Why brisky
66

7-
Fisp is suit for fip tool to load and manage front end resources. We have another static resource manage system named **soi**, [soi](https://github.com/Saber-Team/soi) has a very flexible plug-in archtecture, and its source code is simple enough for all developers to read. Used in production environment is also easy for not only front end developers. Further, we want to control the logic ourselves.
7+
Fisp is suit for fis tool to load and manage front end resources. We have another static resource manage system named **soi**, [soi](https://github.com/Saber-Team/soi) has a very flexible plug-in archtecture, and its source code is simple enough for all developers to read. Used in production environment is also easy for not only front end developers. Further, we want to control the logic ourselves.
88

99

1010
## Usage
@@ -21,5 +21,8 @@ Fisp is suit for fip tool to load and manage front end resources. We have anothe
2121

2222
## Tips
2323

24-
[1] **brisk** has a core class set in brisk directory, and other api is used as smarty plugins.
24+
[1] **brisk** has a core class set in brisk directory, and other api out of this directory are all smarty plugins.
2525

26+
[2] **brisk** does not depend on smarty object in its core code.
27+
28+
[3] **phpunit test code** will be added later.

src/brisk/BriskPage.class.php

+14-12
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,13 @@ class BriskPage {
124124

125125
/**
126126
* 某一个widget使用那种模式渲染
127-
* @var number
128127
*/
129128
static protected $widgetMode;
130129

131130
/* 保存当前inline script或style的id */
132131
static $cp;
133132

134-
/* */
133+
/* 保存当前inline script或style的id数组 */
135134
static $embeded = array();
136135

137136
/**
@@ -569,17 +568,20 @@ public static function render($html) {
569568
*/
570569
public static function widgetStart($id, $mode = null, $group = null) {
571570
$hasParent = !empty(self::$context);
571+
// widget渲染模式
572572
if ($mode) {
573573
$widgetMode = self::parseMode($mode);
574574
} else {
575575
$widgetMode = self::$mode;
576576
}
577577

578-
// record current pagelet id
578+
// 记录当前 pagelet id
579579
self::$pageletId = $id;
580580

581581
$parent_id = $hasParent ? self::$context['id'] : '';
582-
$qk_flag = self::$mode == self::MODE_QUICKLING ? '_qk_' : '';
582+
$qk_flag = (self::$mode == self::MODE_QUICKLING ? '_qk_' : '');
583+
584+
//
583585
$id = empty($id) ? '__elm_' . $parent_id . '_' . $qk_flag . self::$_session_id ++ : $id;
584586

585587

@@ -600,7 +602,7 @@ public static function widgetStart($id, $mode = null, $group = null) {
600602
}
601603

602604
if ($widgetMode === self::MODE_NOSCRIPT) {
603-
//只有指定pagelet_id的widget才嵌套一层div
605+
// 只有指定pagelet_id的widget才嵌套一层div
604606
if (self::$pageletId) {
605607
echo '<div id="' . $id . '">';
606608
}
@@ -617,7 +619,7 @@ public static function widgetStart($id, $mode = null, $group = null) {
617619

618620
if (self::$mode == self::MODE_QUICKLING) {
619621
$hit = self::$filter[$id];
620-
//如果父widget被命中,则子widget设置为命中
622+
// 如果父widget被命中,则子widget设置为命中
621623
if ($hasParent && $parent['hit']) {
622624
$hit = true;
623625
} else if ($hit) {
@@ -629,7 +631,7 @@ public static function widgetStart($id, $mode = null, $group = null) {
629631
// 渲染模式不是quickling时,可以认为是首次渲染
630632
if (self::$pageletId && self::$mode != self::MODE_QUICKLING) {
631633
if (!$group) {
632-
echo '<textarea class="g_fis_bigrender" style="display:none;">'
634+
echo '<textarea class="g_bigrender" style="display:none;">'
633635
.'BigPipe.asyncLoad({id: "'.$id.'"});'
634636
.'</textarea>';
635637
} else {
@@ -692,7 +694,7 @@ public static function widgetEnd($id = null) {
692694
if ($widgetMode == self::MODE_BIGRENDER) {
693695
$widget_style = $widget['style'];
694696
$widget_script = $widget['script'];
695-
//内联css和script放到注释里面, 不需要收集
697+
// 内联css和script放到注释里面, 不需要收集
696698
unset($widget['style']);
697699
unset($widget['script']);
698700

@@ -715,15 +717,15 @@ public static function widgetEnd($id = null) {
715717

716718
echo '--></code></div>';
717719

718-
//收集外链的js和css
720+
// 收集外链的js和css
719721
self::$inner_widget[self::$mode][] = $widget;
720722

721723
} else {
722724
$context['html'] = $html;
723-
//删除不需要的信息
725+
// 删除不需要的信息
724726
unset($context['mode']);
725727
unset($context['hit']);
726-
//not parent
728+
// not parent
727729
unset($context['parent_id']);
728730
self::$pageLets[] = $context;
729731
self::$inner_widget[$widgetMode][] = $widget;
@@ -734,7 +736,7 @@ public static function widgetEnd($id = null) {
734736
echo $html;
735737
} else {
736738
$context['html'] = $html;
737-
//删除不需要的信息
739+
// 删除不需要的信息
738740
unset($context['mode']);
739741
unset($context['hit']);
740742
self::$pageLets[] = $context;

src/brisk/BriskResource.class.php

+50-31
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class BriskResource {
1414

1515
const TYPE_CSS = 'CSS';
1616
const TYPE_JS = 'JS';
17+
const TYPE_TPL = 'TPL';
1718
const TYPE_PKG = 'pkgs';
1819

1920
const ATTR_DEP = 'deps';
@@ -116,7 +117,7 @@ public static function widgetEnd() {
116117
unset(self::$asyncDeleted[$type][$symbol]);
117118
}
118119
}
119-
$ret['asyncLoaded'] = self::getAsyncResourceMap(self::$widgetRequireAsync);
120+
$ret[self::ATTR_ASYNCLOADED] = self::getAsyncResourceMap(self::$widgetRequireAsync);
120121
}
121122

122123
foreach (self::$widgetStaticResource as $type => $val) {
@@ -254,9 +255,9 @@ public static function getPageStaticResource() {
254255
self::$pageStaticResource['style'] = self::$pageStylePool;
255256
}
256257

257-
//异步脚本
258+
// 异步脚本
258259
if (self::$pageRequireAsync) {
259-
self::$pageStaticResource['asyncLoaded'] = self::getAsyncResourceMap(self::$pageRequireAsync);
260+
self::$pageStaticResource[self::ATTR_ASYNCLOADED] = self::getAsyncResourceMap(self::$pageRequireAsync);
260261
}
261262

262263
return self::$pageStaticResource;
@@ -275,7 +276,7 @@ public static function getAsyncResourceMap($arrAsync, $cdn = '') {
275276

276277
// copy js info
277278
if (isset($arrAsync[self::TYPE_JS])) {
278-
foreach ($arrAsync[self::TYPE_JS] as $symbol => $res) {
279+
foreach ($arrAsync[self::TYPE_JS] as $id => $res) {
279280
// collect resource deps and css
280281
$deps = array();
281282
$css = array();
@@ -292,25 +293,19 @@ public static function getAsyncResourceMap($arrAsync, $cdn = '') {
292293
}
293294
}
294295

295-
$arrResourceMap[self::TYPE_JS][$symbol] = array(
296-
'uri' => $cdn . $res[self::ATTR_URI],
297-
);
298-
299296
if (!empty($res[self::ATTR_IN])) {
300-
$arrResourceMap[self::TYPE_JS][$symbol][self::ATTR_IN] = $res[self::ATTR_IN];
297+
$arrResourceMap[self::TYPE_JS][$id][self::ATTR_IN] = $res[self::ATTR_IN];
301298
//如果包含到了某一个包,则模块的url是多余的
302299
//if (!isset($_GET['__debug'])) {
303-
// unset($arrResourceMap[self::TYPE_JS][$symbol][self::ATTR_URI]);
300+
// unset($arrResourceMap[self::TYPE_JS][$id][self::ATTR_URI]);
304301
//}
305302
}
306303

307-
if (!empty($deps)) {
308-
$arrResourceMap[self::TYPE_JS][$symbol][self::ATTR_DEP] = $deps;
309-
}
310-
311-
if (!empty($css)) {
312-
$arrResourceMap[self::TYPE_JS][$symbol][self::ATTR_CSS] = $css;
313-
}
304+
$arrResourceMap[self::TYPE_JS][$id] = array(
305+
'uri' => $cdn . $res[self::ATTR_URI]
306+
);
307+
$arrResourceMap[self::TYPE_JS][$id][self::ATTR_DEP] = $deps;
308+
$arrResourceMap[self::TYPE_JS][$id][self::ATTR_CSS] = $css;
314309
}
315310
}
316311

@@ -324,17 +319,14 @@ public static function getAsyncResourceMap($arrAsync, $cdn = '') {
324319
}
325320
}
326321

327-
$arrResourceMap[self::TYPE_CSS][$symbol] = array(
328-
'uri' => $cdn . $res[self::ATTR_URI],
329-
);
330-
331322
if (!empty($res[self::ATTR_IN])) {
332323
$arrResourceMap[self::TYPE_CSS][$symbol][self::ATTR_IN] = $res[self::ATTR_IN];
333324
}
334325

335-
if (!empty($css)) {
336-
$arrResourceMap[self::TYPE_CSS][$symbol][self::ATTR_DEP] = $css;
337-
}
326+
$arrResourceMap[self::TYPE_CSS][$symbol] = array(
327+
'uri' => $cdn . $res[self::ATTR_URI],
328+
);
329+
$arrResourceMap[self::TYPE_CSS][$symbol][self::ATTR_DEP] = $css;
338330
}
339331
}
340332

@@ -404,6 +396,28 @@ public static function getUri($type, $symbol) {
404396
return '';
405397
}
406398

399+
/**
400+
* Specific type and symbol will confirm one resource
401+
* @param {string} $type
402+
* @param {string} $symbol
403+
* @return mixed
404+
*/
405+
public static function getResource($type, $symbol) {
406+
$pos = strpos($symbol, ':');
407+
if ($pos === false) {
408+
$namespace = '__global__';
409+
} else {
410+
$namespace = substr($symbol, 0, $pos);
411+
}
412+
413+
if (isset(self::$map[$namespace]) || self::register($namespace)) {
414+
$map = self::$map[$namespace];
415+
return $map->getResourceBySymbol($type, $symbol);
416+
}
417+
418+
return null;
419+
}
420+
407421
/**
408422
* Load module and all dependency
409423
* @param {string} $type resource type
@@ -412,16 +426,22 @@ public static function getUri($type, $symbol) {
412426
* @return mixed
413427
*/
414428
public static function load($type, $symbol, $async = false) {
415-
// already loaded
429+
echo 'load when async: <br/>';
430+
echo $symbol;
431+
echo '<br/>';
432+
echo $async;
433+
echo '<br/>';
434+
435+
436+
// 已加载
416437
if (isset(self::$loadedResources[$type][$symbol])) {
417-
//同步组件优先级比异步组件高
438+
// 同步组件优先级比异步组件高, 若记录在异步加载表中则删除
418439
if (!$async && self::getAsync($type, $symbol)) {
419440
self::delAsyncDependencies($type, $symbol);
420441
}
421442
return self::$loadedResources[$type][$symbol];
422-
}
423-
// have not loaded
424-
else {
443+
// 未加载
444+
} else {
425445
$pos = strpos($symbol, ':');
426446
if ($pos === false) {
427447
$namespace = '__global__';
@@ -487,8 +507,7 @@ public static function load($type, $symbol, $async = false) {
487507
} else {
488508
self::triggerError($symbol, 'undefined resource "' . $symbol . '"', E_USER_NOTICE);
489509
}
490-
}
491-
else {
510+
} else {
492511
self::triggerError($symbol, 'missing map file of "' . $namespace . '"', E_USER_NOTICE);
493512
}
494513
}

src/brisk/BriskResourceMap.class.php

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ final class BriskResourceMap {
2020
private $JsSymbolMap;
2121
// css resource info
2222
private $CssSymbolMap;
23+
// tpl resource info
24+
private $TplSymbolMap;
2325

2426
public function __construct($path) {
2527
$this->path = $path;
@@ -28,6 +30,7 @@ public function __construct($path) {
2830
$this->resMap = $this->map['resource'];
2931
$this->JsSymbolMap = $this->resMap['JS'];
3032
$this->CssSymbolMap = $this->resMap['CSS'];
33+
$this->TplSymbolMap = $this->resMap['TPL'];
3134
}
3235

3336
/**
@@ -77,4 +80,7 @@ public function getCssMap() {
7780
return $this->CssSymbolMap;
7881
}
7982

83+
public function getTplMap() {
84+
return $this->TplSymbolMap;
85+
}
8086
}

src/brisk/api.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ function brisk_require_css($symbol, $async = 'false') {
8989
}
9090

9191
/**
92-
* According to type and symbol get url
92+
* 根据资源类型和唯一名确定资源
9393
* @param {string} $type
9494
* @param {string} $symbol
9595
* @return mixed
9696
*/
97-
function brisk_get_resource_uri($type, $symbol) {
98-
return BriskResource::getUri($type, $symbol);
97+
function brisk_get_resource($type, $symbol) {
98+
return BriskResource::getResource($type, $symbol);
9999
}
100100

101101
/**
@@ -107,6 +107,13 @@ function brisk_render_response($content) {
107107
return BriskPage::render($content);
108108
}
109109

110+
/**
111+
*
112+
* @param {string} $pageletId
113+
* @param {?string} $mode
114+
* @param {?string} $group
115+
* @return {bool}
116+
*/
110117
function brisk_widget_start($pageletId, $mode, $group) {
111118
return BriskPage::widgetStart($pageletId, $mode, $group);
112119
}

0 commit comments

Comments
 (0)