/** * Optimize js base on list js * * @param array $js_urls List of js file * * @return array List of optimized js file */ public static function optimizejs($js_urls) { $content = ''; $optimize_js = T3Parameter::_getParam('optimize_js', 1); if (!$optimize_js) { return $js_urls; } //# Fix when optimized_folder is un-writeable $cachepath = T3Path::path(T3Parameter::_getParam('optimize_folder', 't3-assets')); $cachepath .= DS . 'js'; if (!T3Common::checkWriteable($cachepath)) { return $js_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(); jimport('joomla.filesystem.file'); foreach ($js_urls as $url) { if (!$url[0] || !preg_match('#\\.js$#', $url[0]) || $optimize_exclude_regex && preg_match($optimize_exclude_regex, $url[1])) { //ignore if not a local file or not a static js file //output to file if (count($files)) { $files_array[] = array('files' => $files, 'needupdate' => $needupdate); } //put this ignore file into ouput //$output[] = $url; // Ignore file however must follow by order $files_array[] = array('files' => $url, 'ignore' => true); //reset the flag for file update $needupdate = false; $files = array(); } else { //for static local file if ($optimize_js > 1) { // Check js file was minified $ignore_compress = false; foreach (self::$js_ignore_list as $js_ignore) { if (strpos($url[1], $js_ignore)) { $ignore_compress = true; break; } } if ($ignore_compress) { $files[] = array($url[0], ''); } else { // Need optimized // Check if this file is changed from the last optimized // This file is changed/modified after cached $cfile = $cachepath . DS . 'js_' . md5($url[0]) . '.' . basename($url[0]); if (!file_exists($cfile) || @filemtime($url[0]) > @filemtime($cfile)) { $required_optimize_list[] = array('cfile' => $cfile, 'url0' => $url[0], 'url1' => $url[1]); $needupdate = true; $need_optimize = true; } $files[] = array($cfile, ''); } } else { //just keep original $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::compressjs(@JFile::read($required_optimize['url0']), $required_optimize['url1']) . ';'; @JFile::write($required_optimize['cfile'], $data); } } if (!file_exists($cachepath)) { @JFolder::create($cachepath); } //$file = fopen($cachepath . DS . session_id() . "txt", "a"); 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'], 'js', $group_files['needupdate']); // Check result if (!$ourl) { return $js_urls; } // Put result into output $output[] = array('', $ourl); } else { //$ourl = $group_files['files']; $output[] = $group_files['files']; } } //# Release lock T3Head::optimizeReleaseLock($cachepath, $lock_file); return $output; }
function optimizejs($js_urls) { $content = ''; $optimize_js = T3Parameter::get('optimize_js', 1); if (!$optimize_js) { return $js_urls; } $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; jimport('joomla.filesystem.file'); foreach ($js_urls as $url) { if (!$url[0] || !preg_match('#\\.js$#', $url[0]) || $optimize_exclude_regex && preg_match($optimize_exclude_regex, $url[1])) { //ignore if not a local file or not a static js file //output to file if (count($files)) { $ourl = T3Head::store_file2($files, 'js', $needupdate); if (!$ourl) { return $js_urls; } //put result into output $output[] = array('', $ourl); } //put this ignore file into ouput $output[] = $url; //reset the flag for file update $needupdate = false; $files = array(); } else { //for static local file if ($optimize_js > 1) { //need optimized //Check if this file is changed from the last optimized $cfile = $cachepath . DS . md5($url[0]) . '.' . basename($url[0]); if (!is_file($cfile) || @filemtime($url[0]) > @filemtime($cfile)) { //this file is changed/modified after cached //get file content $data = T3Head::compressjs(@JFile::read($url[0]), $url[1]); //write to cache file @JFile::write($cfile, $data); //make a flag for updating cache file $needupdate = true; } $files[] = array($cfile, ''); } else { //just keep original $files[] = $url; } } } if (count($files)) { //output to file $ourl = T3Head::store_file2($files, 'js', $needupdate); if (!$ourl) { return $js_urls; } //put result into output $output[] = array('', $ourl); } return $output; }