YUICompressor.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.4.6.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. * Note: In case you run out stack (default is 512k), you may increase stack size in $options:
  24. * array('stack-size' => '2048k')
  25. *
  26. * @todo unit tests, $options docs
  27. *
  28. * @package Minify
  29. * @author Stephen Clay <steve@mrclay.org>
  30. */
  31. class Minify_YUICompressor {
  32. /**
  33. * Filepath of the YUI Compressor jar file. This must be set before
  34. * calling minifyJs() or minifyCss().
  35. *
  36. * @var string
  37. */
  38. public static $jarFile = null;
  39. /**
  40. * Writable temp directory. This must be set before calling minifyJs()
  41. * or minifyCss().
  42. *
  43. * @var string
  44. */
  45. public static $tempDir = null;
  46. /**
  47. * Filepath of "java" executable (may be needed if not in shell's PATH)
  48. *
  49. * @var string
  50. */
  51. public static $javaExecutable = 'java';
  52. /**
  53. * Minify a Javascript string
  54. *
  55. * @param string $js
  56. *
  57. * @param array $options (verbose is ignored)
  58. *
  59. * @see http://www.julienlecomte.net/yuicompressor/README
  60. *
  61. * @return string
  62. */
  63. public static function minifyJs($js, $options = array())
  64. {
  65. return self::_minify('js', $js, $options);
  66. }
  67. /**
  68. * Minify a CSS string
  69. *
  70. * @param string $css
  71. *
  72. * @param array $options (verbose is ignored)
  73. *
  74. * @see http://www.julienlecomte.net/yuicompressor/README
  75. *
  76. * @return string
  77. */
  78. public static function minifyCss($css, $options = array())
  79. {
  80. return self::_minify('css', $css, $options);
  81. }
  82. private static function _minify($type, $content, $options)
  83. {
  84. self::_prepare();
  85. if (! ($tmpFile = tempnam(self::$tempDir, 'yuic_'))) {
  86. throw new Exception('Minify_YUICompressor : could not create temp file in "'.self::$tempDir.'".');
  87. }
  88. file_put_contents($tmpFile, $content);
  89. exec(self::_getCmd($options, $type, $tmpFile), $output, $result_code);
  90. unlink($tmpFile);
  91. if ($result_code != 0) {
  92. throw new Exception('Minify_YUICompressor : YUI compressor execution failed.');
  93. }
  94. return implode("\n", $output);
  95. }
  96. private static function _getCmd($userOptions, $type, $tmpFile)
  97. {
  98. $o = array_merge(
  99. array(
  100. 'charset' => ''
  101. ,'line-break' => 5000
  102. ,'type' => $type
  103. ,'nomunge' => false
  104. ,'preserve-semi' => false
  105. ,'disable-optimizations' => false
  106. ,'stack-size' => ''
  107. )
  108. ,$userOptions
  109. );
  110. $cmd = self::$javaExecutable
  111. . (!empty($o['stack-size'])
  112. ? ' -Xss' . $o['stack-size']
  113. : '')
  114. . ' -jar ' . escapeshellarg(self::$jarFile)
  115. . " --type {$type}"
  116. . (preg_match('/^[\\da-zA-Z0-9\\-]+$/', $o['charset'])
  117. ? " --charset {$o['charset']}"
  118. : '')
  119. . (is_numeric($o['line-break']) && $o['line-break'] >= 0
  120. ? ' --line-break ' . (int)$o['line-break']
  121. : '');
  122. if ($type === 'js') {
  123. foreach (array('nomunge', 'preserve-semi', 'disable-optimizations') as $opt) {
  124. $cmd .= $o[$opt]
  125. ? " --{$opt}"
  126. : '';
  127. }
  128. }
  129. return $cmd . ' ' . escapeshellarg($tmpFile);
  130. }
  131. private static function _prepare()
  132. {
  133. if (! is_file(self::$jarFile)) {
  134. throw new Exception('Minify_YUICompressor : $jarFile('.self::$jarFile.') is not a valid file.');
  135. }
  136. if (! is_readable(self::$jarFile)) {
  137. throw new Exception('Minify_YUICompressor : $jarFile('.self::$jarFile.') is not readable.');
  138. }
  139. if (! is_dir(self::$tempDir)) {
  140. throw new Exception('Minify_YUICompressor : $tempDir('.self::$tempDir.') is not a valid direcotry.');
  141. }
  142. if (! is_writable(self::$tempDir)) {
  143. throw new Exception('Minify_YUICompressor : $tempDir('.self::$tempDir.') is not writable.');
  144. }
  145. }
  146. }