Plugin Directory

Changeset 145444


Ignore:
Timestamp:
08/13/2009 10:06:03 AM (17 years ago)
Author:
Regen
Message:

Refactor comp.class.php.

Location:
script-compressor/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • script-compressor/trunk/comp.class.php

    r84467 r145444  
    6565     * Initialize Compressor class.
    6666     *
    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.
    7278     * @return Compressor
    7379     */
    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'];
    7989        $this->headers = array();
    8090
    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() {
    11791        $js_files = $css_files = 0;
    118         foreach ($this->files as $id => $file) {
     92        $this->lastModified = 0;
     93        foreach ($this->options['files'] as $id => $file) {
    11994            if (!preg_match('/(.+\.)(js|css)(?:\?.*)?$/iD', $file, $matches)) {
    120                 unset($this->files[$id]);
     95                unset($this->options['files'][$id]);
    12196                continue;
    12297            }
    12398
    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'){
    127114                ++$js_files;
    128             } else if (strtolower($matches[2]) == 'css'){
     115            } else if (strtolower($matches[2]) === 'css'){
    129116                ++$css_files;
    130117            }
    131118        }
    132 
    133119        if ($js_files > 0 && $css_files > 0) {
    134120            $this->type = 'plain';
     
    143129            $this->type = 'none';
    144130        }
    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        }
    168160    }
    169161
     
    174166     */
    175167    function getHash() {
    176         return md5(implode(',', $this->files));
     168        return md5(implode(',', $this->options['files']));
    177169    }
    178170
     
    183175     */
    184176    function getComposed(){
    185         if ($this->type == 'none') return '';
     177        if ($this->type === 'none') return '';
    186178
    187179        $content = '';
    188         foreach($this->files as $file){
     180        foreach($this->options['files'] as $file){
    189181            $file_content = file_get_contents($file) . "\n\n";
    190             if ($this->type == 'css' && $this->replacePath) {
     182            if ($this->type === 'css' && $this->options['replacePath']) {
    191183                if (preg_match_all('%url\((?:"|\')?(.+?)(?:"|\')?\)%i', $file_content, $matches, PREG_SET_ORDER)) {
    192184                    $from = $to = array();
     
    218210    function sendHeader() {
    219211        foreach ($this->headers as $header => $var) {
    220             if ($header == 'HTTP') {
     212            if ($header === 'HTTP') {
    221213                header('HTTP/1.1 ' . $var);
    222214            } else {
     
    232224     */
    233225    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' : '');
    237229
    238230        if (is_file($cache_file) && is_readable($cache_file)) {
     
    245237                    require_once 'jsmin.php';
    246238                    $content = JSMin::minify($content);
    247                     if ($content[0] == "\n") $content = substr($content, 1);
     239                    if ($content[0] === "\n") $content = substr($content, 1);
    248240                    break;
    249241                case 'css':
  • script-compressor/trunk/jscsscomp.php

    r62216 r145444  
    88require_once 'comp.class.php';
    99
    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));
    1717$comp->sendHeader();
    1818echo $comp->getContent();
Note: See TracChangeset for help on using the changeset viewer.