YUICompressor.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Class Minify_YUICompressor
  4. * @package Minify
  5. */
  6. /**
  7. * Compress Javascript/CSS using the YUI Compressor
  8. *
  9. * You must set $jarFile and $tempDir before calling the minify functions.
  10. * Also, depending on your shell's environment, you may need to specify
  11. * the full path to java in $javaExecutable or use putenv() to setup the
  12. * Java environment.
  13. *
  14. * <code>
  15. * Minify_YUICompressor::$jarFile = '/path/to/yuicompressor-2.3.5.jar';
  16. * Minify_YUICompressor::$tempDir = '/tmp';
  17. * $code = Minify_YUICompressor::minifyJs(
  18. * $code
  19. * ,array('nomunge' => true, 'line-break' => 1000)
  20. * );
  21. * </code>
  22. *
  23. * @todo unit tests, $options docs
  24. *
  25. * @package Minify
  26. * @author Stephen Clay <steve@mrclay.org>
  27. */
  28. class Minify_YUICompressor {
  29. /**
  30. * Filepath of the YUI Compressor jar file. This must be set before
  31. * calling minifyJs() or minifyCss().
  32. *
  33. * @var string
  34. */
  35. public static $jarFile = null;
  36. /**
  37. * Writable temp directory. This must be set before calling minifyJs()
  38. * or minifyCss().
  39. *
  40. * @var string
  41. */
  42. public static $tempDir = null;
  43. /**
  44. * Filepath of "java" executable (may be needed if not in shell's PATH)
  45. *
  46. * @var string
  47. */
  48. public static $javaExecutable = 'java';
  49. /**
  50. * Minify a Javascript string
  51. *
  52. * @param string $js
  53. *
  54. * @param array $options (verbose is ignored)
  55. *
  56. * @see http://www.julienlecomte.net/yuicompressor/README
  57. *
  58. * @return string
  59. */
  60. public static function minifyJs($js, $options = array())
  61. {
  62. return self::_minify('js', $js, $options);
  63. }
  64. /**
  65. * Minify a CSS string
  66. *
  67. * @param string $css
  68. *
  69. * @param array $options (verbose is ignored)
  70. *
  71. * @see http://www.julienlecomte.net/yuicompressor/README
  72. *
  73. * @return string
  74. */
  75. public static function minifyCss($css, $options = array())
  76. {
  77. return self::_minify('css', $css, $options);
  78. }
  79. private static function _minify($type, $content, $options)
  80. {
  81. self::_prepare();
  82. if (! ($tmpFile = tempnam(self::$tempDir, 'yuic_'))) {
  83. throw new Exception('Minify_YUICompressor : could not create temp file.');
  84. }
  85. file_put_contents($tmpFile, $content);
  86. exec(self::_getCmd($options, $type, $tmpFile), $output, $result_code);
  87. unlink($tmpFile);
  88. if ($result_code != 0) {
  89. throw new Exception('Minify_YUICompressor : YUI compressor execution failed.');
  90. }
  91. return implode("\n", $output);
  92. }
  93. private static function _getCmd($userOptions, $type, $tmpFile)
  94. {
  95. $o = array_merge(
  96. array(
  97. 'charset' => ''
  98. ,'line-break' => 5000
  99. ,'type' => $type
  100. ,'nomunge' => false
  101. ,'preserve-semi' => false
  102. ,'disable-optimizations' => false
  103. )
  104. ,$userOptions
  105. );
  106. $cmd = self::$javaExecutable . ' -jar ' . escapeshellarg(self::$jarFile)
  107. . " --type {$type}"
  108. . (preg_match('/^[\\da-zA-Z0-9\\-]+$/', $o['charset'])
  109. ? " --charset {$o['charset']}"
  110. : '')
  111. . (is_numeric($o['line-break']) && $o['line-break'] >= 0
  112. ? ' --line-break ' . (int)$o['line-break']
  113. : '');
  114. if ($type === 'js') {
  115. foreach (array('nomunge', 'preserve-semi', 'disable-optimizations') as $opt) {
  116. $cmd .= $o[$opt]
  117. ? " --{$opt}"
  118. : '';
  119. }
  120. }
  121. return $cmd . ' ' . escapeshellarg($tmpFile);
  122. }
  123. private static function _prepare()
  124. {
  125. if (! is_file(self::$jarFile)) {
  126. throw new Exception('Minify_YUICompressor : $jarFile('.self::$jarFile.') is not a valid file.');
  127. }
  128. if (! is_executable(self::$jarFile)) {
  129. throw new Exception('Minify_YUICompressor : $jarFile('.self::$jarFile.') is not executable.');
  130. }
  131. if (! is_dir(self::$tempDir)) {
  132. throw new Exception('Minify_YUICompressor : $tempDir('.self::$tempDir.') is not a valid direcotry.');
  133. }
  134. if (! is_writable(self::$tempDir)) {
  135. throw new Exception('Minify_YUICompressor : $tempDir('.self::$tempDir.') is not writable.');
  136. }
  137. }
  138. }