rhymix/classes/optimizer/Optimizer.class.php
haneul 8103a9cc01 optimizer fixed
git-svn-id: http://xe-core.googlecode.com/svn/sandbox@7435 201d5d3c-b55e-5fd7-737f-ddc643e51545
2010-05-11 09:35:28 +00:00

93 lines
3.4 KiB
PHP

<?php
/**
* @class Optimizer
* @author zero (zero@nzeo.com)
* @brief class designed to be used to merge mutiple JS/CSS files into one file to shorten time taken for transmission.
*
**/
class Optimizer {
var $cache_path = "./files/cache/optimized/";
var $script_file = "./common/script.php?l=%s&t=.%s";
/**
* @brief Constructor which check if a directory, 'optimized' exists in designated path. If not create a new one
**/
function Optimizer() {
if(!is_dir($this->cache_path)) {
FileHandler::makeDir($this->cache_path);
}
}
/**
* @brief file that removes 'optimized' in a given array
* @param[in] $files an array to be modified
**/
function _getOptimizedRemoved($files) {
foreach($files as $key => $val) unset($files[$key]['optimized']);
return $files;
}
/**
* @brief method that optimizes a given file and returns a resultant file
* @param[in] source_files an array of source files to be optimized
* @param[in] type a type of source file, either js or css.
* @return Returns a optimized file
**/
function getOptimizedFiles($source_files, $type = "js") {
if(!is_array($source_files) || !count($source_files)) return;
// 관리자 설정시 설정이 되어 있지 않으면 패스
// 캐시 디렉토리가 없으면 실행하지 않음
$db_info = Context::getDBInfo();
if($db_info->use_optimizer == 'N' || !is_dir($this->cache_path)) return $this->_getOptimizedRemoved($source_files);
if(!count($source_files)) return;
$files = array();
foreach($source_files as $key => $file) {
if(!$file || !$file['file'] || !file_exists($file['file'])) continue;
$file['file'] = $source_files[$key]['file'] = str_replace("\\","/",$file['file']);
if(empty($file['optimized']) || preg_match('/^https?:\/\//i', $file['file']) ) $files[] = $file;
else{
$targets[] = $file;
}
}
if(!count($targets)) return $this->_getOptimizedRemoved($files);
$list_file_hash = md5(join(' ', $targets));
$list_file = FileHandler::getRealPath($this->cache_path . $list_file_hash);
if(!file_exists($list_file)){
$str = '<?php $f=array();';
foreach($targets as $file) $str .= '$f[]="'. $file['file'] . '";';
$str .= ' return $f; ?>';
FileHandler::writeFile($list_file, $str);
}
array_unshift($files, array('file' => sprintf($this->script_file, $list_file_hash, $type) , 'media' => 'all'));
$files = $this->_getOptimizedRemoved($files);
if(!count($files)) return $files;
$url_info = parse_url(Context::getRequestUri());
$abpath = $url_info['path'];
foreach($files as $key => $val) {
$file = $val['file'];
if($file{0} == '/' || strpos($file,'://')!==false) continue;
if(substr($file,0,2)=='./') $file = substr($file,2);
$file = $abpath.$file;
while(strpos($file,'/../')!==false) {
$file = preg_replace('/\/([^\/]+)\/\.\.\//','/',$file);
}
$files[$key]['file'] = $file;
}
return $files;
}
}
?>