Changeset 145444
- Timestamp:
- 08/13/2009 10:06:03 AM (17 years ago)
- Location:
- script-compressor/trunk
- Files:
-
- 2 edited
-
comp.class.php (modified) (7 diffs)
-
jscsscomp.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
script-compressor/trunk/comp.class.php
r84467 r145444 65 65 * Initialize Compressor class. 66 66 * 67 * @param array|string $files Target file(s). 68 * @param string $charset Charset in content-type. 69 * @param bool $gzip Whether compress by gzip 70 * @param bool $replacePath Whether replace paths. 71 * @param string $cache Cache directory. 67 * @param array $options Optionlist 68 * - files 69 * Target file(s). 70 * - charset 71 * Charset in content-type. 72 * - gzip 73 * Whether compress by gzip 74 * - replacePath 75 * Whether replace paths. 76 * - cache 77 * Cache directory. 72 78 * @return Compressor 73 79 */ 74 function Compressor($files, $charset = 'utf-8', $gzip = false, $replacePath = false, $cache = 'cache') { 75 $this->files = is_array($files) ? $files : array($files); 76 $this->charset = $charset; 77 $this->replacePath = $replacePath; 78 $this->cacheDir = $cache; 80 function Compressor($options) { 81 $this->options = $options + array( 82 'charset' => 'utf-8', 83 'gzip' => false, 84 'replacePath' => false, 85 'cache' => 'cache' 86 ); 87 88 $this->options['files'] = (array)$this->options['files']; 79 89 $this->headers = array(); 80 90 81 $this->setType();82 83 /* {{{ Check HTTP_IF_MODIFIED_SINCE */84 $this->lastModified = $this->getLastModified();85 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {86 if (!headers_sent() && $this->lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {87 $this->headers['HTTP'] = '304 Not Modified';88 }89 }90 /* }}} */91 92 /* {{{ Check gzip */93 $this->gzip = false;94 if ($gzip && function_exists('gzencode') && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strrpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false){95 $this->gzip = true;96 $enc = in_array('x-gzip', explode(',', strtolower(str_replace(' ', '', $_SERVER['HTTP_ACCEPT_ENCODING'])))) ? 'x-gzip' : 'gzip';97 $this->headers['Content-Encoding'] = $enc;98 }99 if ($this->gzip) {100 ini_set("zlib.output_compression", "Off");101 $this->headers['Vary'] = 'Accept-Encoding';102 }103 /* }}} */104 105 $this->headers['Cache-Control'] = 'must-revalidate';106 107 if (count($this->files) == 0) {108 $this->headers['HTTP'] = '404 Not Found';109 }110 }111 112 /**113 * Get target type.114 *115 */116 function setType() {117 91 $js_files = $css_files = 0; 118 foreach ($this->files as $id => $file) { 92 $this->lastModified = 0; 93 foreach ($this->options['files'] as $id => $file) { 119 94 if (!preg_match('/(.+\.)(js|css)(?:\?.*)?$/iD', $file, $matches)) { 120 unset($this-> files[$id]);95 unset($this->options['files'][$id]); 121 96 continue; 122 97 } 123 98 124 $this->files[$id] = $matches[1] . $matches[2]; 125 126 if(strtolower($matches[2]) == 'js'){ 99 $file = $matches[1] . $matches[2]; 100 101 if (file_exists($file)) { 102 $fileLmt = @filemtime($file); 103 if($fileLmt > $lastModified){ 104 $this->lastModified = $fileLmt; 105 } 106 } else { 107 unset($this->options['files'][$id]); 108 continue; 109 } 110 111 $this->options['files'][$id] = $file; 112 113 if(strtolower($matches[2]) === 'js'){ 127 114 ++$js_files; 128 } else if (strtolower($matches[2]) == 'css'){115 } else if (strtolower($matches[2]) === 'css'){ 129 116 ++$css_files; 130 117 } 131 118 } 132 133 119 if ($js_files > 0 && $css_files > 0) { 134 120 $this->type = 'plain'; … … 143 129 $this->type = 'none'; 144 130 } 145 } 146 147 /** 148 * Get time that target was modified. 149 * 150 * @return integer File time. 151 */ 152 function getLastModified() { 153 $lastModified = 0; 154 foreach($this->files as $id => $file){ 155 if (file_exists($file)) { 156 $fileLmt = @filemtime($file); 157 if($fileLmt > $lastModified){ 158 $lastModified = $fileLmt; 159 } 160 } else { 161 unset($this->files[$id]); 162 } 163 } 164 165 $this->headers['Last-Modified'] = gmdate('D, d M Y H:i:s', $lastModified) . ' GMT'; 166 167 return $lastModified; 131 132 $this->headers['Last-Modified'] = gmdate('D, d M Y H:i:s', $this->lastModified) . ' GMT'; 133 134 /* {{{ Check HTTP_IF_MODIFIED_SINCE */ 135 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { 136 if (!headers_sent() && $this->lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { 137 $this->headers['HTTP'] = '304 Not Modified'; 138 } 139 } 140 /* }}} */ 141 142 /* {{{ Check gzip */ 143 $this->gzip = false; 144 if ($this->options['gzip'] && function_exists('gzencode') && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strrpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false){ 145 $this->gzip = true; 146 $enc = in_array('x-gzip', explode(',', strtolower(str_replace(' ', '', $_SERVER['HTTP_ACCEPT_ENCODING'])))) ? 'x-gzip' : 'gzip'; 147 $this->headers['Content-Encoding'] = $enc; 148 } 149 if ($this->gzip) { 150 ini_set("zlib.output_compression", "Off"); 151 $this->headers['Vary'] = 'Accept-Encoding'; 152 } 153 /* }}} */ 154 155 $this->headers['Cache-Control'] = 'must-revalidate'; 156 157 if (count($this->options['files']) == 0) { 158 $this->headers['HTTP'] = '404 Not Found'; 159 } 168 160 } 169 161 … … 174 166 */ 175 167 function getHash() { 176 return md5(implode(',', $this-> files));168 return md5(implode(',', $this->options['files'])); 177 169 } 178 170 … … 183 175 */ 184 176 function getComposed(){ 185 if ($this->type == 'none') return '';177 if ($this->type === 'none') return ''; 186 178 187 179 $content = ''; 188 foreach($this-> filesas $file){180 foreach($this->options['files'] as $file){ 189 181 $file_content = file_get_contents($file) . "\n\n"; 190 if ($this->type == 'css' && $this->replacePath) {182 if ($this->type === 'css' && $this->options['replacePath']) { 191 183 if (preg_match_all('%url\((?:"|\')?(.+?)(?:"|\')?\)%i', $file_content, $matches, PREG_SET_ORDER)) { 192 184 $from = $to = array(); … … 218 210 function sendHeader() { 219 211 foreach ($this->headers as $header => $var) { 220 if ($header == 'HTTP') {212 if ($header === 'HTTP') { 221 213 header('HTTP/1.1 ' . $var); 222 214 } else { … … 232 224 */ 233 225 function getContent() { 234 if ($this->type == 'none' || (isset($this->headers['HTTP']) && $this->headers['HTTP']== '304 Not Modified')) return '';235 236 $cache_file = $this-> cacheDir. '/' . $this->getHash() . '-' . $this->lastModified . ($this->gzip ? '.gz' : '');226 if ($this->type === 'none' || (isset($this->headers['HTTP']) && $this->headers['HTTP'] === '304 Not Modified')) return ''; 227 228 $cache_file = $this->options['cache'] . '/' . $this->getHash() . '-' . $this->lastModified . ($this->gzip ? '.gz' : ''); 237 229 238 230 if (is_file($cache_file) && is_readable($cache_file)) { … … 245 237 require_once 'jsmin.php'; 246 238 $content = JSMin::minify($content); 247 if ($content[0] == "\n") $content = substr($content, 1);239 if ($content[0] === "\n") $content = substr($content, 1); 248 240 break; 249 241 case 'css': -
script-compressor/trunk/jscsscomp.php
r62216 r145444 8 8 require_once 'comp.class.php'; 9 9 10 $comp = new Compressor( 11 $scriptcomp->getScripts(),12 get_option('blog_charset'),13 $scriptcomp->options['gzip'],14 $scriptcomp->options['css_method'] == 'composed',15 $scriptcomp->options['cache']16 ) ;10 $comp = new Compressor(array( 11 'files' => $scriptcomp->getScripts(), 12 'charset' => get_option('blog_charset'), 13 'gzip' => $scriptcomp->options['gzip'], 14 'replacePath' => $scriptcomp->options['css_method'] == 'composed', 15 'cache' => $scriptcomp->options['cache'] 16 )); 17 17 $comp->sendHeader(); 18 18 echo $comp->getContent();
Note: See TracChangeset
for help on using the changeset viewer.