Minify_CSS::minify PHP Method

minify() public static method

Minify a CSS string
public static minify ( string $css, array $options = [] ) : string
$css string
$options array available options: 'preserveComments': (default true) multi-line comments that begin with "/*!" will be preserved with newlines before and after to enhance readability. 'removeCharsets': (default true) remove all @charset at-rules 'prependRelativePath': (default null) if given, this string will be prepended to all relative URIs in import/url declarations 'currentDir': (default null) if given, this is assumed to be the directory of the current CSS file. Using this, minify will rewrite all relative URIs in import/url declarations to correctly point to the desired files. For this to work, the files *must* exist and be visible by the PHP process. 'symlinks': (default = array()) If the CSS file is stored in a symlink-ed directory, provide an array of link paths to target paths, where the link paths are within the document root. Because paths need to be normalized for this to work, use "//" to substitute the doc root in the link paths (the array keys). E.g.: array('//symlink' => '/real/target/path') // unix array('//static' => 'D:\\staticStorage') // Windows 'docRoot': (default = $_SERVER['DOCUMENT_ROOT']) see Minify_CSS_UriRewriter::rewrite
return string
    public static function minify($css, $options = array())
    {
        $options = array_merge(array('compress' => true, 'removeCharsets' => true, 'preserveComments' => true, 'currentDir' => null, 'docRoot' => $_SERVER['DOCUMENT_ROOT'], 'prependRelativePath' => null, 'symlinks' => array()), $options);
        if ($options['removeCharsets']) {
            $css = preg_replace('/@charset[^;]+;\\s*/', '', $css);
        }
        if ($options['compress']) {
            if (!$options['preserveComments']) {
                $css = Minify_CSS_Compressor::process($css, $options);
            } else {
                $processor = array('Minify_CSS_Compressor', 'process');
                $css = Minify_CommentPreserver::process($css, $processor, array($options));
            }
        }
        if (!$options['currentDir'] && !$options['prependRelativePath']) {
            return $css;
        }
        if ($options['currentDir']) {
            $currentDir = $options['currentDir'];
            $docRoot = $options['docRoot'];
            $symlinks = $options['symlinks'];
            return Minify_CSS_UriRewriter::rewrite($css, $currentDir, $docRoot, $symlinks);
        } else {
            return Minify_CSS_UriRewriter::prepend($css, $options['prependRelativePath']);
        }
    }

Usage Example

Beispiel #1
0
 public function action_css()
 {
     $group = (string) $this->request->param('group');
     if (empty($group)) {
         $group = 'default';
     }
     if (!($content = Kohana::cache('minify::css::' . $group))) {
         $path = isset($this->_config['css'][$group]['path']) ? $this->_config['css'][$group]['path'] : '';
         $files = isset($this->_config['css'][$group]['files']) ? $this->_config['css'][$group]['files'] : array();
         if (!is_array($files)) {
             $files = array();
         }
         $content = '';
         foreach ($files as $file) {
             $content .= file_get_contents($path . DIRECTORY_SEPARATOR . $file);
         }
         if (!empty($content)) {
             $minifier = isset($this->_config['css'][$group]['minifier']) ? $this->_config['css'][$group]['minifier'] : '';
             if (class_exists($minifier)) {
                 $class = new ReflectionClass($minifier);
                 $css_min = $class->newInstance($content, array('current_dir' => $path));
                 $content = $class->getMethod('min')->invoke($css_min);
             } else {
                 $content = Minify_CSS::minify($content, array('current_dir' => $path));
             }
         }
         if ((bool) $this->_config['cache']) {
             Kohana::cache('minify::css::' . $group, $content, (int) $this->_config['cache_lifetime']);
         }
     }
     $this->response->body($content);
 }
All Usage Examples Of Minify_CSS::minify
Minify_CSS