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