CSS.php 3.3 KB

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