时间:2021-07-01 10:21:17 帮助过:23人阅读
class mergeCompress
{
var $merge_arr;
var $commpress_arr;
var $type;
var $file_dir; //压缩合并后的目录
var $file_name; //压缩合并后的文件名
var $path_jar; // yui jar的路径
var $is_del = true; //压缩后的零散文件是否删除掉
var $errs = array();
public function __construct($commpress_arr=array(),$merge_arr=array(),$type="js",$file_dir="",$file_name="",$path_jar=""){
$this->type = $type;
$this->commpress_arr = $commpress_arr;
$this->merge_arr = $merge_arr;
$this->file_dir = $file_dir;
$this->file_name = $file_name;
$this->path_jar = $path_jar;
}
public function exec(){
$new_compress_arr = $this->compress();
$this->merge_arr = array_merge($this->merge_arr,$new_compress_arr);
$this->merge();
if($this->is_del){
foreach($new_compress_arr as $i=>$url){
unlink($url);
}
}
}
public function compress(){
$new_compress_arr = array();
$temp = "java -jar {jar_path} --type {type} --charset utf-8 {source} > {compressor_source}";
$temp = str_replace("{jar_path}",$this->path_jar,$temp);
$temp = str_replace("{type}",$this->type,$temp);
$type = $this->type;
$file_dir = $this->file_dir;
foreach($this->commpress_arr as $i => $val){
if(!file_exists($val)){
array_push($this->errs,$val." Does not exist!!!! ");
continue;
}
$cmd = str_replace("{source}",$val,$temp);
$new_name = basename($val);
$new_name = substr($new_name,0,strrpos($new_name,".")).".min.".$type;
$file_new_name = $file_dir."/".$new_name;
$cmd = str_replace("{compressor_source}",$file_new_name,$cmd);
$arr = array();
exec($cmd,$arr,$is);
if($is != 0){
array_push($errs,$val." compress fail!!!! ");
}
array_push($new_compress_arr,$file_new_name);
}
return $new_compress_arr;
}
public function merge(){
$content="";
$file_name = $this->file_dir."/".$this->file_name;
foreach($this->merge_arr as $i=>$url){
if(!file_exists($url)){
array_push($this->errs,$url." Does not exist!!!! ");