00001 <?php
00002 class JCSSManagerCompressBaseCommand extends CommandDelegate {
00003 protected $in_files;
00004 protected $out_file;
00005
00006
00007
00008
00009
00010
00011
00012
00013 public function __construct($in_files, $out_file) {
00014 $this->in_files = $this->clean_in_files(Arr::force($in_files, false));
00015 $this->out_file = $out_file;
00016 parent::__construct(null, false);
00017 }
00018
00019
00020
00021
00022 protected function clean_in_files($in_files) {
00023 $ret = array();
00024 foreach($in_files as $key => $value) {
00025 if (is_numeric($key)) {
00026 $ret['default'][] = $value;
00027 }
00028 else {
00029 $ret[$key] = $value;
00030 }
00031 }
00032 return $ret;
00033 }
00034
00035
00036
00037
00038
00039
00040 public function execute() {
00041 Load::models('jcsscompressedfiles');
00042 $ret = new Status();
00043
00044 $files_to_unlink = array();
00045 foreach($this->in_files as $groupname => $in_files) {
00046 $versioned_file_name = false;
00047 $out_file = $this->out_file;
00048 if ($groupname != 'default') {
00049 $arr = explode('.', $out_file);
00050 $ext = array_pop($arr);
00051 $arr[] = $groupname;
00052 $arr[] = $ext;
00053 $out_file = implode('.', $arr);
00054 }
00055 if (count($in_files)) {
00056 $ret->merge($this->compress($in_files, $out_file, $files_to_unlink));
00057 }
00058 else {
00059 file_put_contents($out_file, '');
00060 }
00061 if ($ret->is_ok()) {
00062 $dao = JCSSCompressedFiles::update_db($this->get_db_type(), $out_file, $in_files, $ret);
00063 if ($ret->is_ok()) {
00064 $versioned_file_name = JCSSManager::make_absolute($dao->get_versioned_filename());
00065 rename($out_file, $versioned_file_name);
00066 }
00067 }
00068
00069 $gzip_file = false;
00070 if ($versioned_file_name && $ret->is_ok()) {
00071 $ret->merge($this->gzip($versioned_file_name, $gzip_file));
00072 }
00073 }
00074
00075 foreach($files_to_unlink as $src) {
00076 @unlink($src);
00077 }
00078
00079 return $ret;
00080 }
00081
00082
00083
00084
00085
00086
00087 protected function compress($in_files, $out_file, &$versioned_file_path) {
00088 $ret = new Status('JCSSManagerCompressBase::compress not implemented');
00089 return $ret;
00090 }
00091
00092
00093
00094
00095
00096
00097 protected function gzip($file, &$gzip_file) {
00098 $ret = new Status();
00099
00100 if (Config::has_feature(ConfigJCSSManager::ALSO_GZIP)) {
00101 $gzipped_file = $file . '.gz';
00102 $fp = gzopen($gzipped_file, 'w9');
00103 if ($fp) {
00104 gzwrite($fp, file_get_contents($file));
00105 gzclose($fp);
00106 }
00107 else {
00108 $ret->append('JCSSManager: Could not create gzip-file');
00109 }
00110 }
00111 return $ret;
00112 }
00113
00114
00115
00116
00117
00118
00119 protected function get_db_type() {
00120 throw new Exception('get_db_type() not implemented');
00121 }
00122
00123 }