Пример #1
0
 /**
  * Execute css/js optimizing base on joomla document object
  *
  * @return void
  */
 public static function proccess()
 {
     $document = JFactory::getDocument();
     $urlPath = new T3Path();
     //proccess stylesheets
     $themes = T3Common::get_active_themes();
     //$themes[] = array('core', 'default'); //default now move to template root folder
     //$themes[] = array('engine', 'default');
     $themes = array_reverse($themes);
     $scripts = array();
     $css_urls = array();
     $css_urls['site'] = array();
     foreach ($themes as $theme) {
         $css_urls[$theme[0] . '.' . $theme[1]] = array();
     }
     foreach ($themes as $theme) {
         $css_urls[$theme[0] . '.' . $theme[1] . '-browser'] = array();
     }
     if (T3Common::isRTL()) {
         foreach ($themes as $theme) {
             $css_urls[$theme[0] . '.' . $theme[1] . '-rtl'] = array();
         }
     }
     //$bname = T3Common::getBrowserSortName();
     //$bver = T3Common::getBrowserMajorVersion();
     $optimize_css = T3Parameter::_getParam('optimize_css', 2);
     $optimize_js = T3Parameter::_getParam('optimize_js', 2);
     foreach ($document->_styleSheets as $strSrc => $strAttr) {
         $path = T3Head::cleanUrl($strSrc);
         if (!$path || !preg_match('#\\.css$#', $path)) {
             //External url
             $css_urls['site'][] = array('', $strSrc);
             continue;
         }
         $intemplate = false;
         if (preg_match('/^templates\\/' . T3_ACTIVE_TEMPLATE . '\\//', $path)) {
             $path = preg_replace('/^templates\\/' . T3_ACTIVE_TEMPLATE . '\\//', '', $path);
             $intemplate = true;
         }
         $paths = array();
         //$paths[] = array ('', $path, $strSrc); //The third element is the original url
         $paths[] = array('', $path);
         // Fix when source code in subfolder
         //if ($intemplate) {
         //only load other css files if in T3v2 template
         $ext = '';
         if (preg_match('#\\.[^.]+$#', $path, $matches)) {
             $ext = $matches[0];
         }
         //if ($ext) {
         //$paths[] = array('-browser', str_replace($ext, "-$bname$ext", $path));
         //$paths[] = array('-browser', str_replace($ext, "-$bname$bver$ext", $path));
         //if (T3Common::isRTL()) {
         //$paths[] = array('-rtl', str_replace($ext, "-rtl$ext", $path));
         //$paths[] = array('-rtl', str_replace($ext, "-$bname-rtl$ext", $path));
         //$paths[] = array('-rtl', str_replace($ext, "-$bname$bver-rtl$ext", $path));
         //}
         //}
         if ($ext && T3Common::isRTL()) {
             $paths[] = array('-rtl', str_replace($ext, "-rtl{$ext}", $path));
         }
         //}
         foreach ($paths as $path) {
             //
             if ($intemplate) {
                 $urls = $urlPath->get($path[1], true);
                 if ($urls) {
                     foreach ($urls as $theme => $url) {
                         $url[] = $strAttr;
                         $css_urls[$theme . $path[0]][$url[0]] = $url;
                     }
                 }
             } else {
                 if (is_file(T3Path::path($path[1]))) {
                     $css_urls['site'][T3Path::path($path[1])] = array(T3Path::path($path[1]), count($path) > 2 ? $path[2] : T3Path::url($path[1]), $strAttr);
                     //use original url ($path[2]) if exists
                 }
             }
         }
     }
     // Remove current stylesheets
     $document->_styleSheets = array();
     foreach ($document->_scripts as $strSrc => $strType) {
         $srcurl = T3Head::cleanUrl($strSrc);
         if (!$srcurl || !preg_match('#\\.js$#', $srcurl)) {
             $scrips[] = array('', $strSrc);
             continue;
         }
         if (preg_match('/^templates\\/' . T3_ACTIVE_TEMPLATE . '\\//', $srcurl)) {
             //in template
             $srcurl = preg_replace('/^templates\\/' . T3_ACTIVE_TEMPLATE . '\\//', '', $srcurl);
             $path = str_replace('/', DS, $srcurl);
             $url = $urlPath->get($path);
             if ($url) {
                 $scrips[] = $url;
             }
         } else {
             // Don't read file content => keep original link
             if ($optimize_js < 1) {
                 $scrips[] = array('', $strSrc);
                 continue;
             }
             $path = str_replace('/', DS, $srcurl);
             $scrips[] = array(JPATH_SITE . DS . $path, JURI::base(true) . '/' . $srcurl);
         }
     }
     //remove current scripts
     $document->_scripts = array();
     $tmp_css_urls = false;
     do {
         $tmp_css_urls = T3Head::optimizecss($css_urls);
     } while ($tmp_css_urls === false);
     $css_urls = $tmp_css_urls;
     //if ($url) $css_urls = array(array(array('', $url)));
     //re-add stylesheet to head
     foreach ($css_urls as $urls) {
         foreach ($urls as $url) {
             if (count($url) > 2) {
                 $attrs = $url[2];
                 $document->addStylesheet($url[1], $attrs['mime'], $attrs['media'], $attrs['attribs']);
             } else {
                 $document->addStylesheet($url[1]);
             }
         }
     }
     $tmp_scrips = false;
     do {
         $tmp_scrips = T3Head::optimizejs($scrips);
     } while ($tmp_scrips === false);
     $scrips = $tmp_scrips;
     //re-add stylesheet to head
     foreach ($scrips as $url) {
         $document->addScript($url[1]);
     }
 }
Пример #2
0
 function proccess()
 {
     $document =& JFactory::getDocument();
     //proccess stylesheets
     $themes = T3Common::get_active_themes();
     //$themes[] = array('core', 'default'); //default now move to template root folder
     //$themes[] = array('engine', 'default');
     $themes = array_reverse($themes);
     $scripts = array();
     $css_urls = array();
     $css_urls['site'] = array();
     foreach ($themes as $theme) {
         $css_urls[$theme[0] . '.' . $theme[1]] = array();
     }
     foreach ($themes as $theme) {
         $css_urls[$theme[0] . '.' . $theme[1] . '-browser'] = array();
     }
     if (T3Common::isRTL()) {
         foreach ($themes as $theme) {
             $css_urls[$theme[0] . '.' . $theme[1] . '-rtl'] = array();
         }
     }
     $bname = T3Common::getBrowserSortName();
     $bver = T3Common::getBrowserMajorVersion();
     $optimize_css = T3Parameter::get('optimize_css', 2);
     $optimize_js = T3Parameter::get('optimize_js', 2);
     foreach ($document->_styleSheets as $strSrc => $strAttr) {
         $path = T3Head::cleanUrl($strSrc);
         if (!$path) {
             //External url
             $css_urls['site'][] = array('', $strSrc);
             continue;
         }
         $intemplate = false;
         if (preg_match('/^templates\\/' . T3_ACTIVE_TEMPLATE . '\\//', $path)) {
             $path = preg_replace('/^templates\\/' . T3_ACTIVE_TEMPLATE . '\\//', '', $path);
             $intemplate = true;
         }
         /*
         if (!$intemplate && $optimize_css < 2) // don't read file content => keep original link
         {
         	$css_urls['site'][] = array ('', $strSrc);
         	continue;
         }
         */
         $paths = array();
         $paths[] = array('', $path, $strSrc);
         //The third element is the original url
         //if ($intemplate) {
         //only load other css files if in T3v2 template
         $ext = '';
         if (preg_match('#\\.[^.]+$#', $path, $matches)) {
             $ext = $matches[0];
         }
         //$file_info = pathinfo($path);
         //$ext = $file_info['extension'];
         if ($ext) {
             $paths[] = array('-browser', str_replace($ext, "-{$bname}{$ext}", $path));
             $paths[] = array('-browser', str_replace($ext, "-{$bname}{$bver}{$ext}", $path));
             if (T3Common::isRTL()) {
                 $paths[] = array('-rtl', str_replace($ext, "-rtl{$ext}", $path));
                 $paths[] = array('-rtl', str_replace($ext, "-{$bname}-rtl{$ext}", $path));
                 $paths[] = array('-rtl', str_replace($ext, "-{$bname}{$bver}-rtl{$ext}", $path));
             }
         }
         //}
         foreach ($paths as $path) {
             //
             if ($intemplate) {
                 if ($urls = T3Path::get($path[1], true)) {
                     foreach ($urls as $theme => $url) {
                         $css_urls[$theme . $path[0]][$url[0]] = $url;
                     }
                 }
             } else {
                 if (is_file(T3Path::path($path[1]))) {
                     $css_urls['site'][T3Path::path($path[1])] = array(T3Path::path($path[1]), count($path) > 2 ? $path[2] : T3Path::url($path[1]));
                 }
                 //use original url ($path[2]) if exists
             }
         }
     }
     //remove current stylesheets
     $document->_styleSheets = array();
     foreach ($document->_scripts as $strSrc => $strType) {
         $srcurl = T3Head::cleanUrl($strSrc);
         if (!$srcurl) {
             $scrips[] = array('', $strSrc);
             continue;
         }
         if (preg_match('/^templates\\/' . T3_ACTIVE_TEMPLATE . '\\//', $srcurl)) {
             //in template
             $srcurl = preg_replace('/^templates\\/' . T3_ACTIVE_TEMPLATE . '\\//', '', $srcurl);
             $path = str_replace('/', DS, $srcurl);
             if ($url = T3Path::get($path)) {
                 $scrips[] = $url;
             }
         } else {
             if ($optimize_js < 1) {
                 $scrips[] = array('', $strSrc);
                 continue;
             }
             $path = str_replace('/', DS, $srcurl);
             $scrips[] = array(JPATH_SITE . DS . $path, JURI::base(true) . '/' . $srcurl);
         }
     }
     //remove current scripts
     $document->_scripts = array();
     $css_urls = T3Head::optimizecss($css_urls);
     //if ($url) $css_urls = array(array(array('', $url)));
     //re-add stylesheet to head
     foreach ($css_urls as $urls) {
         foreach ($urls as $url) {
             $document->addStylesheet($url[1]);
         }
     }
     $scrips = T3Head::optimizejs($scrips);
     //re-add stylesheet to head
     foreach ($scrips as $url) {
         $document->addScript($url[1]);
     }
 }