/** * WIDGET START * 解析参数,收集widget所用到的静态资源 * @param $id * @param $mode * @param $group * @return bool */ public static function start($id, $mode = null, $group = null) { $has_parent = !empty(self::$_context); if ($mode) { $widget_mode = self::_parseMode($mode); } else { $widget_mode = self::$mode; } self::$_pagelet_id = $id; $parent_id = $has_parent ? self::$_context['id'] : ''; $qk_flag = self::$mode == self::MODE_QUICKLING ? '_qk_' : ''; $id = empty($id) ? '__elm_' . $parent_id . '_' . $qk_flag . self::$_session_id++ : $id; $parent = self::$_context; $has_parent = !empty($parent); //$hit $hit = true; $context = array('id' => $id, 'mode' => $widget_mode, 'hit' => $hit); if ($has_parent) { $context['parent_id'] = $parent['id']; self::$_contextMap[$parent['id']] = $parent; } if ($widget_mode == self::MODE_NOSCRIPT) { //只有指定paglet_id的widget才嵌套一层div if (self::$_pagelet_id) { echo '<div id="' . $id . '">'; } } else { if ($widget_mode == self::MODE_BIGRENDER) { //widget 为bigrender时,将内容渲染到html注释里面 if (!$has_parent) { echo '<div id="' . $id . '">'; echo '<code class="g_bigrender"><!--'; } } else { echo '<div id="' . $id . '">'; } if (self::$mode == self::MODE_QUICKLING) { $hit = self::$filter[$id]; //如果父widget被命中,则子widget设置为命中 if ($has_parent && $parent['hit']) { $hit = true; } else { if ($hit) { //指定获取一个子widget时,需要单独处理这个widget $context['parent_id'] = null; $has_parent = false; } } } else { if ($widget_mode == self::MODE_QUICKLING) { //渲染模式不是quickling时,可以认为是首次渲染 if (self::$_pagelet_id && self::$mode != self::MODE_QUICKLING) { if (!$group) { echo '<textarea class="g_fis_bigrender" style="display:none;">' . 'BigPipe.asyncLoad({id: "' . $id . '"});' . '</textarea>'; } else { if (isset(self::$_pagelet_group[$group])) { self::$_pagelet_group[$group][] = $id; } else { self::$_pagelet_group[$group] = array($id); echo "<!--" . $group . "-->"; } } } // 不需要渲染这个widget $hit = false; } } $context['hit'] = $hit; if ($hit) { if (!$has_parent) { //获取widget内部的静态资源 FISResource::widgetStart(); } //start a buffer ob_start(); } } //设置当前处理context self::$_context = $context; return $hit; }
/** * WIDGET END * 收集html,收集静态资源 */ public static function end($id = null) { $ret = true; $context = self::$_context; $widget_mode = $context['mode']; $has_parent = $context['parent_id']; if ($id) { self::$_pagelet_id = $id; } if ($widget_mode == self::MODE_NOSCRIPT) { if (self::$_pagelet_id) { echo '</div>'; } } else { if ($context['hit']) { //close buffer $html = ob_get_clean(); if (!$has_parent) { $widget = FISResource::widgetEnd(); // end if ($widget_mode == self::MODE_BIGRENDER) { $widget_style = $widget['style']; $widget_script = $widget['script']; //内联css和script放到注释里面, 不需要收集 unset($widget['style']); unset($widget['script']); $out = ''; if ($widget_style) { $out .= '<style type="text/css">' . implode('', $widget_style) . '</style>'; } $out .= $html; if ($widget_script) { $out .= '<script type="text/javascript">' . implode('', $widget_script) . '</script>'; } echo str_replace(array('\\', '-->'), array('\\\\', '--\\>'), $out); $html = ''; echo '--></code></div>'; //收集外链的js和css self::$inner_widget[self::$mode][] = $widget; } else { $context['html'] = $html; //删除不需要的信息 unset($context['mode']); unset($context['hit']); //not parent unset($context['parent_id']); self::$_pagelets[] = $context; self::$inner_widget[$widget_mode][] = $widget; } } else { // end if ($widget_mode == self::MODE_BIGRENDER) { echo $html; } else { $context['html'] = $html; //删除不需要的信息 unset($context['mode']); unset($context['hit']); self::$_pagelets[] = $context; } } } if ($widget_mode != self::MODE_BIGRENDER) { echo '! MODE_BIGRENDER'; echo '</div>'; } } //切换context self::$_context = self::$_contextMap[$context['parent_id']]; unset(self::$_contextMap[$context['parent_id']]); if (!$has_parent) { self::$_context = null; } return $ret; }