Esempio n. 1
0
 public static function createFile(&$body, $src_files, $excluded_files, $replace_tags, $conds, $comments, $indexes)
 {
     $arr = plgSystemJBetolo::param('files');
     $abs_excl_minify = plgSystemJBetolo::param('minify_exclude');
     if ($abs_excl_minify) {
         $abs_excl_minify = explode(',', $abs_excl_minify);
     }
     $abs_excl_gzip = plgSystemJBetolo::param('gzip_exclude');
     if ($abs_excl_gzip) {
         $abs_excl_gzip = explode(',', $abs_excl_gzip);
     }
     $excl_js_imports = $excl_css_imports = '';
     $css_imports = $js_imports = array();
     $js_placement = plgSystemJBetolo::param('js_placement');
     $age = plgSystemJBetolo::param('cache_age');
     $app = JFactory::getApplication()->getName();
     $tmpl = JFactory::getApplication()->getTemplate();
     $paramHasChanged = false;
     $external_custom_orders = array();
     /**
      * app[administrator|site],type[css|js],attr[css=media,js='']
      */
     foreach ($src_files as $type => $_src_files) {
         if (empty($_src_files)) {
             continue;
         }
         $skipFiles = array_merge($conds[$type], $comments[$type]);
         $merge = plgSystemJBetolo::param($type . '_merge');
         $is_gz = JBETOLO_IS_GZ && plgSystemJBetolo::param($type . '_gzip') && jbetoloHelper::clientEncoding();
         $is_minify = JBETOLO_IS_MINIFY && plgSystemJBetolo::param($type . '_minify');
         $excl_files = $excluded_files[$type];
         if ($merge) {
             $mergeMode = plgSystemJBetolo::param($type . '_merge_mode', 'mono');
             $are_files_changed = $new_files_found = false;
             if (count($arr) == 0 || !isset($arr[$app]) || !isset($arr[$app][$tmpl]) || !isset($arr[$app][$tmpl][$type]) || !is_array($arr[$app][$tmpl][$type])) {
                 $arr[$app][$tmpl][$type] = array();
             }
             if ($mergeMode == 'mono') {
                 if (count($arr[$app][$tmpl][$type])) {
                     foreach ($arr[$app][$tmpl][$type] as $attr => $rec) {
                         $merged = array();
                         $merged_file = JBETOLO_CACHE_DIR . $rec['merged'];
                         $found_files = array();
                         $delete_merged_file = false;
                         foreach ($_src_files as $s => $src_file) {
                             if ($type == 'js' || isset($indexes['css'][$s]['attr']) && $attr == $indexes['css'][$s]['attr']) {
                                 if (!in_array($src_file, $rec['srcs'])) {
                                     $found_files[] = $src_file;
                                     $delete_merged_file = true;
                                 }
                             }
                         }
                         if (!empty($found_files)) {
                             $merged = array_merge($merged, $found_files);
                         }
                         $merged_file_exists = JFile::exists($merged_file);
                         if (empty($found_files) && $merged_file_exists) {
                             $are_files_changed = self::areFilesChanged($rec['parts']);
                             if ($are_files_changed) {
                                 $delete_merged_file = true;
                             }
                         }
                         if ($delete_merged_file && $merged_file_exists) {
                             JFile::delete($merged_file);
                         }
                         $merged_file_exists = JFile::exists($merged_file);
                         if (!$merged_file_exists) {
                             $merged = array_merge($merged, $rec['srcs']);
                         }
                         if (!empty($merged)) {
                             $merged = array_unique($merged);
                             $merged = jbetoloFileHelper::customOrder($merged, $type);
                             if ($type == 'js') {
                                 jbetoloJS::setJqueryFile($merged, jbetoloHelper::getArrayValues($excl_files, 'src'));
                                 $res = jbetoloJS::build($merged);
                             } else {
                                 $res = jbetoloCSS::build($merged, array_fill(0, count($merged), $attr));
                             }
                             $arr[$app][$tmpl][$type][$attr] = $res[$attr];
                             $paramHasChanged = true;
                         }
                     }
                 } else {
                     $_src_files = array_unique($_src_files);
                     if ($type == 'js') {
                         jbetoloJS::setJqueryFile($_src_files, jbetoloHelper::getArrayValues($excl_files, 'src'));
                     }
                     $arr[$app][$tmpl][$type] = $type == 'css' ? jbetoloCSS::build($_src_files, '', true, $indexes['css']) : jbetoloJS::build($_src_files);
                     $paramHasChanged = true;
                 }
                 $imports = $arr[$app][$tmpl][$type];
             } else {
                 if ($mergeMode == 'url') {
                     $files_key = jbetoloHelper::currentURL();
                 } else {
                     if ($mergeMode == 'resource') {
                         $_src_files = array_unique($_src_files);
                         $files_key = $_src_files;
                         sort($files_key);
                         $files_key = implode('', $files_key);
                     }
                 }
                 if (isset($arr[$app][$tmpl][$type][$files_key])) {
                     $rec = $arr[$app][$tmpl][$type][$files_key];
                     $add_key = key($rec);
                     $rec = $rec[$add_key];
                     if (self::areFilesChanged($rec['parts'])) {
                         if (JFile::exists(JBETOLO_CACHE_DIR . $rec['merged'])) {
                             JFile::delete(JBETOLO_CACHE_DIR . $rec['merged']);
                         }
                         $paramHasChanged = true;
                     }
                 } else {
                     $paramHasChanged = true;
                 }
                 if ($paramHasChanged) {
                     $arr[$app][$tmpl][$type][$files_key] = $type == 'css' ? jbetoloCSS::build($_src_files, '', true, $indexes['css']) : jbetoloJS::build($_src_files);
                 }
                 $imports = $arr[$app][$tmpl][$type][$files_key];
             }
             $external_custom_orders[$type] = jbetoloHelper::replaceTags($body, $replace_tags[$type], "", $indexes[$type], plgSystemJBetolo::param($type . '_external_custom_order_before'), plgSystemJBetolo::param($type . '_external_custom_order_after'));
             /**
              * @@todo: if cdn enabled just provide the file and no dynamic url
              */
             foreach ($imports as $attr => $rec) {
                 $url = jbetoloFileHelper::getServingURL($rec['merged'], $type, $is_gz, $age);
                 if ($type == 'js') {
                     $js_imports[] = "\n" . '<script type="text/javascript" src="' . $url . '"></script>';
                 } else {
                     $attrs = jbetoloHelper::returnAttributes($attr);
                     $css_imports[] = "\n" . '<link rel="stylesheet" type="text/css" href="' . $url . '" ' . $attrs . ' />';
                 }
             }
         }
         $gzip_excluded = plgSystemJBetolo::param($type . '_gzip_excluded');
         $minify_excluded = plgSystemJBetolo::param($type . '_minify_excluded');
         if (($is_gz && $gzip_excluded || $is_minify && $minify_excluded) && count($excl_files)) {
             $jqueryNoConflict = plgSystemJBetolo::param('js_jquery_no_conflict');
             foreach ($excl_files as $excl_file) {
                 $src = $excl_file['src'];
                 if (jbetoloFileHelper::fileInArray($src, $skipFiles)) {
                     continue;
                 }
                 $_minify = $is_minify && !jbetoloFileHelper::isFileExcluded($src, $abs_excl_minify);
                 $_gz = $is_gz && !jbetoloFileHelper::isFileExcluded($src, $abs_excl_gzip);
                 if (($_minify || $_gz) && !$excl_file['dynamic']) {
                     $src = str_replace(JBETOLO_URI_BASE, '', $src);
                     $attr = $type == 'css' ? jbetoloHelper::extractAttributes($excl_file['tag']) : 'main';
                     $file = $type == 'css' ? jbetoloCSS::build($src, array($attr)) : jbetoloJS::build($src);
                     $file = $file[$attr]['merged'];
                     $src = jbetoloFileHelper::getServingURL($file, $type, $is_gz, $age);
                     if ($type == 'js') {
                         $excl_js_imports .= "\n" . '<script type="text/javascript" src="' . $src . '"></script>';
                     } else {
                         if ($type == 'css') {
                             $attrs = jbetoloHelper::returnAttributes($attr);
                             $excl_css_imports .= "\n" . '<link rel="stylesheet" type="text/css" href="' . $src . '" ' . $attrs . ' />';
                         }
                     }
                     $body = str_ireplace($excl_file['tag'], '', $body);
                 }
             }
         }
     }
     jbetoloFileHelper::placeTags($body, $excl_css_imports, 'css');
     jbetoloFileHelper::placeTags($body, $js_imports, 'js', $js_placement, isset($external_custom_orders['js']) ? $external_custom_orders['js']['before'] : array(), isset($external_custom_orders['js']['after']) ? $external_custom_orders['js']['after'] : array(), true);
     jbetoloFileHelper::placeTags($body, $excl_js_imports, 'js', $js_placement, null, null, true);
     jbetoloFileHelper::placeTags($body, $css_imports, 'css');
     if ($paramHasChanged) {
         plgSystemJBetolo::param('files', $arr, 'set');
     }
 }