Exemple #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);
 }
 /**
  * Merges all background properties
  * @param array $input_css
  * @return array
  * @version 1.0
  * @see dissolve_short_bg()
  * @todo full CSS 3 compliance
  */
 public function merge_bg($input_css)
 {
     $background_prop_default =& $GLOBALS['csstidy']['background_prop_default'];
     // Max number of background images. CSS3 not yet fully implemented
     $number_of_values = @max(count(self::explode_ws(',', $input_css['background-image'])), count(self::explode_ws(',', $input_css['background-color'])), 1);
     // Array with background images to check if BG image exists
     $bg_img_array = @self::explode_ws(',', Diglin_Csstidy_Core::gvw_important($input_css['background-image']));
     $new_bg_value = '';
     $important = '';
     for ($i = 0; $i < $number_of_values; $i++) {
         foreach ($background_prop_default as $bg_property => $default_value) {
             // Skip if property does not exist
             if (!isset($input_css[$bg_property])) {
                 continue;
             }
             $cur_value = $input_css[$bg_property];
             // Skip some properties if there is no background image
             if ((!isset($bg_img_array[$i]) || $bg_img_array[$i] === 'none') && ($bg_property === 'background-size' || $bg_property === 'background-position' || $bg_property === 'background-attachment' || $bg_property === 'background-repeat')) {
                 continue;
             }
             // Remove !important
             if (Diglin_Csstidy_Core::is_important($cur_value)) {
                 $important = ' !important';
                 $cur_value = Diglin_Csstidy_Core::gvw_important($cur_value);
             }
             // Do not add default values
             if ($cur_value === $default_value) {
                 continue;
             }
             $temp = self::explode_ws(',', $cur_value);
             if (isset($temp[$i])) {
                 if ($bg_property == 'background-size') {
                     $new_bg_value .= '(' . $temp[$i] . ') ';
                 } else {
                     $new_bg_value .= $temp[$i] . ' ';
                 }
             }
         }
         $new_bg_value = trim($new_bg_value);
         if ($i != $number_of_values - 1) {
             $new_bg_value .= ',';
         }
     }
     // Delete all background-properties
     foreach ($background_prop_default as $bg_property => $default_value) {
         unset($input_css[$bg_property]);
     }
     // Add new background property
     if ($new_bg_value !== '') {
         $input_css['background'] = $new_bg_value . $important;
     }
     return $input_css;
 }