Esempio n. 1
0
 /**
  * 渲染静态资源
  * @param $html
  * @param $arr
  * @param bool $clean_hook
  * @return mixed
  */
 public static function renderStatic($html, $arr, $clean_hook = false)
 {
     if (!empty($arr)) {
         $code = '';
         $resource_map = $arr['async'];
         $loadModJs = FISResource::getFramework() && ($arr['js'] || $resource_map);
         if ($loadModJs) {
             foreach ($arr['js'] as $js) {
                 $code .= '<script type="text/javascript" src="' . $js . '"></script>';
                 if ($js == FISResource::getFramework()) {
                     if ($resource_map) {
                         $code .= '<script type="text/javascript">';
                         $code .= 'require.resourceMap(' . json_encode($resource_map) . ');';
                         $code .= '</script>';
                     }
                 }
             }
         }
         if (!empty($arr['script'])) {
             $code .= '<script type="text/javascript">' . PHP_EOL;
             foreach ($arr['script'] as $inner_script) {
                 $code .= '!function(){' . $inner_script . '}();' . PHP_EOL;
             }
             $code .= '</script>';
         }
         /**************autopack getCountUrl for sending log*********************/
         $jsCode = FISAutoPack::getCountUrl($arr);
         if ($jsCode != "") {
             $code .= '<script type="text/javascript">' . $jsCode . '</script>';
         }
         /**************autopack end*********************/
         $html = str_replace(self::JS_SCRIPT_HOOK, $code . self::JS_SCRIPT_HOOK, $html);
         //收集style和link
         $code = '';
         //通过编译插件可控制inline,link的样式位置
         $linkPosition = self::$global_link ? self::CSS_GLBAL_LINKS_HOOK : self::CSS_LINKS_HOOK;
         if (!empty($arr['style'])) {
             $code .= '<style type="text/css">';
             foreach ($arr['style'] as $inner_style) {
                 $code .= $inner_style;
             }
             $code .= '</style>';
         }
         //替换
         $html = str_replace($linkPosition, $code . $linkPosition, $html);
         //国际化定制
         if (!empty($arr['css'])) {
             $s_code = '';
             $s_code .= '<link rel="stylesheet" type="text/css" href="' . implode('" /><link rel="stylesheet" type="text/css" href="', $arr['css']) . '" />';
             if (self::$hide_link) {
                 $s_code = '<textarea style="display:none"  id="g_fis_css">' . $s_code . '</textarea>';
                 $html = str_replace(self::CSS_STYLE_HOOK, $s_code . self::CSS_STYLE_HOOK, $html);
             } else {
                 $html = str_replace($linkPosition, $s_code . $linkPosition, $html);
             }
         }
     }
     if ($clean_hook) {
         $html = str_replace(array($linkPosition, self::JS_SCRIPT_HOOK, self::CSS_STYLE_HOOK), '', $html);
     }
     return $html;
 }
Esempio n. 2
0
 /**
  * 渲染静态资源
  * @param $html
  * @param $arr
  * @param bool $clean_hook
  * @return mixed
  */
 public static function renderStatic($html, $arr, $clean_hook = false)
 {
     if (!empty($arr)) {
         $code = '';
         $resource_map = $arr['async'];
         $loadModJs = FISResource::getFramework() && ($arr['js'] || $resource_map);
         if ($loadModJs) {
             $code .= '<script type="text/javascript" src="' . self::getCdn() . FISResource::getFramework() . '"></script>';
             if ($resource_map) {
                 $code .= '<script type="text/javascript">';
                 $code .= 'require.resourceMap(' . json_encode($resource_map) . ');';
                 $code .= '</script>';
             }
             foreach ($arr['js'] as $js) {
                 if ($js == FISResource::getFramework()) {
                     continue;
                 }
                 $code .= '<script type="text/javascript" src="' . self::getCdn() . $js . '"></script>';
             }
         }
         if (!empty($arr['script'])) {
             $code .= '<script type="text/javascript">' . PHP_EOL;
             foreach ($arr['script'] as $inner_script) {
                 $code .= '!function(){' . $inner_script . '}();' . PHP_EOL;
             }
             $code .= '</script>';
         }
         /**************autopack getCountUrl for sending log*********************/
         $jsCode = FISAutoPack::getCountUrl();
         if ($jsCode != "") {
             $code .= '<script type="text/javascript">' . $jsCode . '</script>';
         }
         /**************autopack end*********************/
         $html = str_replace(self::JS_SCRIPT_HOOK, $code . self::JS_SCRIPT_HOOK, $html);
         $code = '';
         if (!empty($arr['css'])) {
             $code = '<link rel="stylesheet" type="text/css" href="' . self::getCdn() . implode('" /><link rel="stylesheet" type="text/css" href="' . self::getCdn(), $arr['css']) . '" />';
         }
         if (!empty($arr['style'])) {
             $code .= '<style type="text/css">';
             foreach ($arr['style'] as $inner_style) {
                 $code .= $inner_style;
             }
             $code .= '</style>';
         }
         //替换
         if (strpos($html, self::CSS_INLINE_LINKS_HOOK)) {
             $html = str_replace(self::CSS_INLINE_LINKS_HOOK, '<textarea id="g_fis_css" style="display:none;">' . $code . '</textarea>' . self::CSS_INLINE_LINKS_HOOK, $html);
         } else {
             $html = str_replace(self::CSS_LINKS_HOOK, $code . self::CSS_LINKS_HOOK, $html);
         }
     }
     if ($clean_hook) {
         $html = str_replace(array(self::CSS_LINKS_HOOK, self::JS_SCRIPT_HOOK, self::CSS_INLINE_LINKS_HOOK), '', $html);
     }
     return $html;
 }
 /**
  * 渲染静态资源
  * @param $html
  * @param $arr
  * @param bool $clean_hook
  * @return mixed
  */
 public static function renderStatic($html, $arr, $clean_hook = false)
 {
     if (!empty($arr)) {
         $code = '';
         $resource_map = $arr['async'];
         $loadModJs = FISResource::getFramework() && ($arr['js'] || $resource_map);
         if ($loadModJs) {
             $code .= '<script type="text/javascript" src="' . self::getCdn() . FISResource::getFramework() . '"></script>';
             if ($resource_map) {
                 $code .= '<script type="text/javascript">';
                 $code .= 'require.resourceMap(' . json_encode($resource_map) . ');';
                 $code .= '</script>';
             }
             foreach ($arr['js'] as $js) {
                 if ($js == FISResource::getFramework()) {
                     continue;
                 }
                 $code .= '<script type="text/javascript" src="' . self::getCdn() . $js . '"></script>';
             }
         }
         if (!empty($arr['script'])) {
             $code .= '<script type="text/javascript">' . PHP_EOL;
             foreach ($arr['script'] as $inner_script) {
                 $code .= '!function(){' . $inner_script . '}();' . PHP_EOL;
             }
             $code .= '</script>';
         }
         $html = str_replace(self::JS_SCRIPT_HOOK, $code . self::JS_SCRIPT_HOOK, $html);
         $code = '';
         if (!empty($arr['css'])) {
             $code = '<link rel="stylesheet" type="text/css" href="' . self::getCdn() . implode('" /><link rel="stylesheet" type="text/css" href="' . self::getCdn(), $arr['css']) . '" />';
             // $code .= '<style>';
             // foreach ($arr['css'] as $css) {
             //     $code .= file_get_contents($css);
             // }
             // $code .= '</style>';
         }
         if (!empty($arr['style'])) {
             $code .= '<style type="text/css">';
             foreach ($arr['style'] as $inner_style) {
                 $code .= $inner_style;
             }
             $code .= '</style>';
         }
         //替换
         $html = str_replace(self::CSS_LINKS_HOOK, $code . self::CSS_LINKS_HOOK, $html);
     }
     if ($clean_hook) {
         $html = str_replace(array(self::CSS_LINKS_HOOK, self::JS_SCRIPT_HOOK), '', $html);
     }
     return $html;
 }