Esempio n. 1
0
 /**
  * Compress CSS files and write them to media/css folders
  * 
  * @param string $name
  * @param string|array $mergeCallback
  * @param bool $static
  * @return string
  */
 protected function _prepareCssCompression($name, $mergeCallback = null, $static = false)
 {
     $designPackage = Mage::getDesign();
     $config = Mage::getStoreConfig('uioptimization/csscompression');
     $uiHelper = Mage::helper('uioptimization');
     $options = array();
     if ($config['type'] == 'yuicompressor') {
         //YUI Compressor
         $minifier = 'Diglin_Minify_YUICompressor';
         $method = 'minifyCss';
         // init Minify class with YUI Compressor
         Diglin_Minify_YUICompressor::$jarFile = Mage::getBaseDir('lib') . DS . 'Diglin' . DS . 'yuicompressor' . DS . 'yuicompressor.jar';
         Diglin_Minify_YUICompressor::$tempDir = Mage::getBaseDir('tmp');
         if (strlen($config['java_path']) > 0) {
             Diglin_Minify_YUICompressor::$javaExecutable = $config['java_path'];
         }
     } elseif ($config['type'] == 'googleminify') {
         // Google Minify
         $minifier = 'Diglin_Minify_CSS';
         $method = 'minify';
         $options = array('minifierOptions' => array(Diglin_Minify::TYPE_CSS => array('preserveComments' => $config['preserve_comments'])));
     } else {
         // CSS Tidy
         $css = new Diglin_Csstidy_Core();
         switch ($config['template']) {
             case 'custom':
                 $css->load_template($config['custom_template']);
                 break;
             case 'low_compression':
             case 'default':
             case 'high_compression':
             case 'highest_compression':
             default:
                 $css->load_template($config['template']);
                 break;
         }
         $css->set_cfg('remove_last_;', $config['remove_last_semicolon']);
         $css->set_cfg('remove_bslash', $config['remove_bslash']);
         $css->set_cfg('compress_colors', $config['compress_colors']);
         $css->set_cfg('compress_font-weight', $config['compress_font']);
         $css->set_cfg('lowercase_s', $config['lowercase_s']);
         $css->set_cfg('optimise_shorthands', $config['optimise_shorthands']);
         //0 = none, 1=safe optimize, 2=all optimize
         $css->set_cfg('case_properties', $config['case_properties']);
         $css->set_cfg('sort_properties', $config['sort_properties']);
         $css->set_cfg('sort_selectors', $config['sort_selectors']);
         $css->set_cfg('merge_selectors', $config['merge_selectors']);
         $css->set_cfg('discard_invalid_properties', $config['discard_invalid_properties']);
         $css->set_cfg('css_level', $config['css_level']);
         //css2.0, css2.1, css1.0
         $css->set_cfg('preserve_css', $config['preserve_css']);
         $css->set_cfg('timestamp', $config['timestamp']);
     }
     $info = $uiHelper->getCompressedInfo($name, 'css', $static);
     if (!isset($info['result']) || $info['result'] != false) {
         $info['result'] = true;
     }
     if (!file_exists($info['targetPathFile']) && $info['result'] || !Mage::getStoreConfigFlag('uioptimization/general/cronupdate') && $info['result'] && filemtime($info['orgskin_path']) > filemtime($info['targetPathFile'])) {
         $ioFile = new Diglin_Io_File();
         if ($config['type'] == 'csstidy') {
             // CSS Tidy
             $css_code = $ioFile->read($info['orgskin_path']);
             $css->parse($css_code);
             $cssText = $css->print->plain();
         } else {
             if ($config['type'] == 'googleminify' || $config['type'] == 'yuicompressor') {
                 // Google Minify or YUI Compressor
                 $options += array('quiet' => true, 'files' => array($info['orgskin_path']), 'encodeMethod' => '', 'minifiers' => array(Diglin_Minify::TYPE_CSS => array($minifier, $method)));
                 $results = Diglin_Minify::serve('Files', $options);
                 $cssText = $results['content'];
             }
         }
         //clean or fix @import and url(...); by comparing the path with the original CSS file (not the compressed one otherwise the path is wrong)
         $cssText = $designPackage->beforeMergeCss($info['orgskin_path'], $cssText);
         $info['result'] = $ioFile->write($info['targetPathFile'], $cssText, 0644);
     }
     return $uiHelper->getResultPath($info, $mergeCallback);
 }