| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Minify_CSS |
| 4 |
* @package Minify |
| 5 |
*/ |
| 6 |
|
| 7 |
/** |
| 8 |
* Minify CSS |
| 9 |
* |
| 10 |
* This class uses Minify_CSS_Compressor and Minify_CSS_UriRewriter to |
| 11 |
* minify CSS and rewrite relative URIs. |
| 12 |
* |
| 13 |
* @package Minify |
| 14 |
* @author Stephen Clay <steve@mrclay.org> |
| 15 |
* @author http://code.google.com/u/1stvamp/ (Issue 64 patch) |
| 16 |
* |
| 17 |
* @deprecated Use Minify_CSSmin |
| 18 |
*/ |
| 19 |
class Minify_CSS |
| 20 |
{ |
| 21 |
|
| 22 |
/** |
| 23 |
* Minify a CSS string |
| 24 |
* |
| 25 |
* @param string $css |
| 26 |
* |
| 27 |
* @param array $options available options: |
| 28 |
* |
| 29 |
* 'preserveComments': (default true) multi-line comments that begin |
| 30 |
* with "/*!" will be preserved with newlines before and after to |
| 31 |
* enhance readability. |
| 32 |
* |
| 33 |
* 'removeCharsets': (default true) remove all @charset at-rules |
| 34 |
* |
| 35 |
* 'prependRelativePath': (default null) if given, this string will be |
| 36 |
* prepended to all relative URIs in import/url declarations |
| 37 |
* |
| 38 |
* 'currentDir': (default null) if given, this is assumed to be the |
| 39 |
* directory of the current CSS file. Using this, minify will rewrite |
| 40 |
* all relative URIs in import/url declarations to correctly point to |
| 41 |
* the desired files. For this to work, the files *must* exist and be |
| 42 |
* visible by the PHP process. |
| 43 |
* |
| 44 |
* 'symlinks': (default = array()) If the CSS file is stored in |
| 45 |
* a symlink-ed directory, provide an array of link paths to |
| 46 |
* target paths, where the link paths are within the document root. Because |
| 47 |
* paths need to be normalized for this to work, use "//" to substitute |
| 48 |
* the doc root in the link paths (the array keys). E.g.: |
| 49 |
* <code> |
| 50 |
* array('//symlink' => '/real/target/path') // unix |
| 51 |
* array('//static' => 'D:\\staticStorage') // Windows |
| 52 |
* </code> |
| 53 |
* |
| 54 |
* 'docRoot': (default = $_SERVER['DOCUMENT_ROOT']) |
| 55 |
* see Minify_CSS_UriRewriter::rewrite |
| 56 |
* |
| 57 |
* @return string |
| 58 |
*/ |
| 59 |
public static function minify($css, $options = array()) |
| 60 |
{ |
| 61 |
$options = array_merge(array( |
| 62 |
'compress' => true, |
| 63 |
'removeCharsets' => true, |
| 64 |
'preserveComments' => true, |
| 65 |
'currentDir' => null, |
| 66 |
'docRoot' => $_SERVER['DOCUMENT_ROOT'], |
| 67 |
'prependRelativePath' => null, |
| 68 |
'symlinks' => array(), |
| 69 |
), $options); |
| 70 |
|
| 71 |
if ($options['removeCharsets']) { |
| 72 |
$css = preg_replace('/@charset[^;]+;\\s*/', '', $css); |
| 73 |
} |
| 74 |
|
| 75 |
if ($options['compress']) { |
| 76 |
if (! $options['preserveComments']) { |
| 77 |
$css = Minify_CSS_Compressor::process($css, $options); |
| 78 |
} else { |
| 79 |
$processor = array('Minify_CSS_Compressor', 'process'); |
| 80 |
$css = Minify_CommentPreserver::process($css, $processor, array($options)); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
if (! $options['currentDir'] && ! $options['prependRelativePath']) { |
| 85 |
return $css; |
| 86 |
} |
| 87 |
|
| 88 |
if ($options['currentDir']) { |
| 89 |
$currentDir = $options['currentDir']; |
| 90 |
$docRoot = $options['docRoot']; |
| 91 |
$symlinks = $options['symlinks']; |
| 92 |
|
| 93 |
return Minify_CSS_UriRewriter::rewrite($css, $currentDir, $docRoot, $symlinks); |
| 94 |
} |
| 95 |
|
| 96 |
return Minify_CSS_UriRewriter::prepend($css, $options['prependRelativePath']); |
| 97 |
} |
| 98 |
} |
| 99 |
|