Exemplo n.º 1
0
 /**
  * Optimize css base on list css
  *
  * @param array $css_urls  List of css file
  *
  * @return array  List of optimized css file
  */
 public static function optimizecss($css_urls)
 {
     $content = '';
     $optimize_css = T3Parameter::_getParam('optimize_css', 2);
     if (!$optimize_css) {
         return $css_urls;
         //no optimize css
     }
     // # Fix when optimized_folder is un-writeable
     $cachepath = T3Path::path(T3Parameter::_getParam('optimize_folder', 't3-assets'));
     $cachepath .= DS . 'css';
     //$parentpath = dirname($cachepath);
     if (!T3Common::checkWriteable($cachepath)) {
         return $css_urls;
     }
     $output = array();
     $optimize_exclude = trim(T3Parameter::_getParam('optimize_exclude', ''));
     $optimize_exclude_regex = null;
     if ($optimize_exclude) {
         $optimize_exclude_regex = '#' . preg_replace('#[\\r\\n]+#', '|', preg_quote($optimize_exclude)) . '#';
     }
     $files = array();
     // # Check lock file before start checking update
     $lock_file = "optimize.js.lock";
     $waiting = T3Head::optimizeCheckLock($cachepath, $lock_file);
     $lock_file_file = null;
     $needupdate = false;
     $need_optimize = false;
     $required_optimize_list = array();
     $files_array = array();
     // Limit files import into a css file (in IE7, only first 30 css files are loaded). other case, load unlimited
     $filelimit = $optimize_css == 1 ? 20 : 999;
     $filecount = 0;
     jimport('joomla.filesystem.file');
     foreach ($css_urls as $theme => $urls) {
         foreach ($urls as $url) {
             $ignore = false;
             $import = false;
             $importupdate = false;
             // check ignore to optimize
             // - not a local file
             // - not a css file
             // - in ignore list
             if (!$url[0]) {
                 $ignore = true;
             } elseif (!preg_match('#\\.css$#', $url[0])) {
                 $ignore = true;
                 //ignore dynamic css file
             } elseif ($optimize_exclude_regex && preg_match($optimize_exclude_regex, $url[1])) {
                 $ignore = true;
             }
             if (!$ignore && $optimize_css > 1) {
                 //check if need update. for css, the cache should be [filename] or [filename]-import
                 //[filename]-import - for the case there's @import inside
                 //in the ignore of @import, file still optimize but will be put into a sigle file
                 $cfile = $cachepath . DS . 'css_' . md5($url[0]) . '.' . basename($url[0]);
                 if (!(file_exists($cfile) && @filemtime($url[0]) < @filemtime($cfile)) && !(file_exists($cfile . '-import') && @filemtime($url[0]) < @filemtime($cfile . '-import'))) {
                     $required_optimize_list[] = array('cfile' => $cfile, 'url0' => $url[0], 'url1' => $url[1]);
                     //Need update
                     $data = @JFile::read($url[0]);
                     if (preg_match('#@import\\s+.+#', $data)) {
                         $import = true;
                         $importupdate = true;
                         $cfile = $cfile . '-import';
                     }
                     $needupdate = true;
                     $need_optimize = true;
                 } elseif (is_file($cfile . '-import')) {
                     $import = true;
                     $importupdate = false;
                     $cfile = $cfile . '-import';
                 }
             }
             //match ignore file, or import file, or reach the limit: flush previous files out
             if ($ignore || $import || count($files) == $filelimit) {
                 if (count($files)) {
                     $files_array[] = array('files' => $files, 'needupdate' => $needupdate);
                 }
                 //reset the flag for file update
                 $needupdate = false;
                 $files = array();
             }
             //write out the @import file
             if ($ignore) {
                 //$output[] = $url;
                 // Ignore file however must follow by order
                 $files_array[] = array('files' => $url, 'ignore' => true);
             } else {
                 if ($optimize_css > 1) {
                     $files[] = array($cfile, '', $url[2]);
                 } else {
                     $files[] = $url;
                 }
             }
         }
     }
     if ($need_optimize) {
         //# Only create lock if and only if require optimize
         if (!T3Head::optimizeCreateLock($cachepath, $lock_file)) {
             return false;
         }
         foreach ($required_optimize_list as $required_optimize) {
             $data = T3Head::compresscss(@JFile::read($required_optimize['url0']), $required_optimize['url1']);
             if (preg_match('#@import\\s+.+#', $data)) {
                 $import = true;
                 $importupdate = true;
                 $required_optimize['cfile'] = $required_optimize['cfile'] . '-import';
             }
             if (JFile::exists($required_optimize['cfile'])) {
                 @JFile::delete($required_optimize['cfile']);
             }
             if (JFile::exists($required_optimize['cfile'] . '-import')) {
                 @JFile::delete($required_optimize['cfile'] . '-import');
             }
             @JFile::write($required_optimize['cfile'], $data);
             $needupdate = true;
         }
     }
     if (count($files)) {
         $files_array[] = array('files' => $files, 'needupdate' => $needupdate);
     }
     foreach ($files_array as $group_files) {
         // Check ignore file
         if (!isset($group_files['ignore'])) {
             $ourl = T3Head::store_file2($group_files['files'], 'css', $group_files['needupdate']);
             if (!$ourl) {
                 return $css_urls;
             }
             $output[] = array('', $ourl);
         } else {
             $output[] = $group_files['files'];
         }
     }
     // # Release lock
     T3Head::optimizeReleaseLock($cachepath, $lock_file);
     return array($output);
 }
Exemplo n.º 2
0
 function optimizecss($css_urls)
 {
     $content = '';
     $optimize_css = T3Parameter::get('optimize_css', 2);
     if (!$optimize_css) {
         return $css_urls;
     }
     //no optimize css
     $output = array();
     $optimize_exclude = trim(T3Parameter::get('optimize_exclude', ''));
     $optimize_exclude_regex = null;
     if ($optimize_exclude) {
         $optimize_exclude_regex = '#' . preg_replace('#[\\r\\n]+#', '|', preg_quote($optimize_exclude)) . '#';
     }
     $files = array();
     $t3cache = T3Cache::getInstance();
     $cachepath = T3Path::path(T3Parameter::get('optimize_folder', 't3-assets'));
     $needupdate = false;
     $filelimit = $optimize_css == 1 ? 20 : 999;
     //limit files import into a css file (in IE7, only first 30 css files are loaded). other case, load unlimited
     $filecount = 0;
     jimport('joomla.filesystem.file');
     foreach ($css_urls as $theme => $urls) {
         foreach ($urls as $url) {
             $ignore = false;
             $import = false;
             $importupdate = false;
             //check ignore to optimize
             // - not a local file
             // - not a css file
             // - in ignore list
             if (!$url[0]) {
                 $ignore = true;
             } else {
                 if (!preg_match('#\\.css$#', $url[0])) {
                     $ignore = true;
                 } else {
                     if ($optimize_exclude_regex && preg_match($optimize_exclude_regex, $url[1])) {
                         $ignore = true;
                     }
                 }
             }
             if (!$ignore && $optimize_css > 1) {
                 //check if need update. for css, the cache should be [filename] or [filename]-import
                 //[filename]-import - for the case there's @import inside
                 //in the ignore of @import, file still optimize but will be put into a sigle file
                 $cfile = $cachepath . DS . md5($url[0]) . '.' . basename($url[0]);
                 if (!(is_file($cfile) && @filemtime($url[0]) < @filemtime($cfile)) && !(is_file($cfile . '-import') && @filemtime($url[0]) < @filemtime($cfile . '-import'))) {
                     //Need update
                     $data = T3Head::compresscss(@JFile::read($url[0]), $url[1]);
                     //check if contain @import
                     if (preg_match('#@import\\s+.+#', $data)) {
                         $import = true;
                         $importupdate = true;
                         $cfile = $cfile . '-import';
                     }
                     //delete old file if exists
                     @JFile::delete($cfile);
                     @JFile::delete($cfile . '-import');
                     //write to cache file
                     @JFile::write($cfile, $data);
                     //make a flag for updating cache file
                     $needupdate = true;
                 } else {
                     if (is_file($cfile . '-import')) {
                         $import = true;
                         $importupdate = false;
                         $cfile = $cfile . '-import';
                     }
                 }
             }
             //match ignore file, or import file, or reach the limit: flush previous files out
             if ($ignore || $import || count($files) == $filelimit) {
                 if (count($files)) {
                     $ourl = T3Head::store_file2($files, 'css', $needupdate);
                     if (!$ourl) {
                         return $css_urls;
                     }
                     //put result into output
                     $output[] = array('', $ourl);
                 }
                 //reset the flag for file update
                 $needupdate = false;
                 $files = array();
             }
             //write out the @import file
             if ($import) {
                 $ourl = T3Head::store_file2(array(array($cfile, '')), 'css', $importupdate);
                 if (!$ourl) {
                     return $css_urls;
                 }
                 //put result into output
                 $output[] = array('', $ourl);
             } else {
                 if ($ignore) {
                     $output[] = $url;
                 } else {
                     if ($optimize_css > 1) {
                         $files[] = array($cfile, '');
                     } else {
                         $files[] = $url;
                     }
                 }
             }
         }
     }
     if (count($files)) {
         $ourl = T3Head::store_file2($files, 'css', $needupdate);
         if (!$ourl) {
             return $css_urls;
         }
         //put result into output
         $output[] = array('', $ourl);
     }
     return array($output);
 }