Minify_YUICompressor

Compress Javascript/CSS using the YUI Compressor.

Defined (1)

The class is defined in the following location(s).

/lib/Minify/Minify/YUICompressor.php  
  1. class Minify_YUICompressor { 
  2.  
  3. /** 
  4. * Filepath of the YUI Compressor jar file. This must be set before 
  5. * calling minifyJs() or minifyCss(). 
  6. * @var string 
  7. */ 
  8. public static $jarFile = null; 
  9.  
  10. /** 
  11. * Writable temp directory. This must be set before calling minifyJs() 
  12. * or minifyCss(). 
  13. * @var string 
  14. */ 
  15. public static $tempDir = null; 
  16.  
  17. /** 
  18. * Filepath of "java" executable (may be needed if not in shell's PATH) 
  19. * @var string 
  20. */ 
  21. public static $javaExecutable = 'java'; 
  22.  
  23. /** 
  24. * Minify a Javascript string 
  25. *  
  26. * @param string $js 
  27. *  
  28. * @param array $options (verbose is ignored) 
  29. *  
  30. * @see http://www.julienlecomte.net/yuicompressor/README 
  31. *  
  32. * @return string  
  33. */ 
  34. public static function minifyJs($js, $options = array()) 
  35. return self::_minify('js', $js, $options); 
  36.  
  37. /** 
  38. * Minify a CSS string 
  39. *  
  40. * @param string $css 
  41. *  
  42. * @param array $options (verbose is ignored) 
  43. *  
  44. * @see http://www.julienlecomte.net/yuicompressor/README 
  45. *  
  46. * @return string  
  47. */ 
  48. public static function minifyCss($css, $options = array()) 
  49. $css = self::_minify('css', $css, $options); 
  50. $css = Minify_CSS_UriRewriter::rewrite($css, $options); 
  51.  
  52. return $css; 
  53.  
  54. private static function _minify($type, $content, $options) 
  55. self::_prepare(); 
  56. if (! ($tmpFile = tempnam(self::$tempDir, 'yuic_'))) { 
  57. throw new Exception('Minify_YUICompressor : could not create temp file.'); 
  58. file_put_contents($tmpFile, $content); 
  59. exec(self::_getCmd($options, $type, $tmpFile), $output, $result_code); 
  60. unlink($tmpFile); 
  61. if ($result_code != 0) { 
  62. throw new Exception('Minify_YUICompressor : YUI compressor execution failed.'); 
  63. return implode("\n", $output); 
  64.  
  65. private static function _getCmd($userOptions, $type, $tmpFile) 
  66. if (!is_file(self::$javaExecutable)) { 
  67. throw new Exception(sprintf('JAVA executable (%s) is not a valid file.', self::$javaExecutable)); 
  68.  
  69. if (!is_file(self::$jarFile)) { 
  70. throw new Exception(sprintf('JAR file (%s) is not a valid file.', self::$jarFile)); 
  71. $o = array_merge( 
  72. array( 
  73. 'charset' => '' 
  74. , 'line-break' => 5000 
  75. , 'type' => $type 
  76. , 'nomunge' => false 
  77. , 'preserve-semi' => false 
  78. , 'disable-optimizations' => false 
  79. , 'stack-size' => '' 
  80. , $userOptions 
  81. ); 
  82. $cmd = self::$javaExecutable 
  83. . (!empty($o['stack-size']) 
  84. ? ' -Xss' . $o['stack-size'] 
  85. : '') 
  86. . ' -jar ' . escapeshellarg(self::$jarFile) 
  87. . " --type {$type}" 
  88. . (preg_match('/^[\\da-zA-Z0-9\\-]+$/', $o['charset']) 
  89. ? " --charset {$o['charset']}"  
  90. : '') 
  91. . (is_numeric($o['line-break']) && $o['line-break'] >= 0 
  92. ? ' --line-break ' . (int)$o['line-break'] 
  93. : ''); 
  94. if ($type === 'js') { 
  95. foreach (array('nomunge', 'preserve-semi', 'disable-optimizations') as $opt) { 
  96. $cmd .= $o[$opt]  
  97. ? " --{$opt}" 
  98. : ''; 
  99. return $cmd . ' ' . escapeshellarg($tmpFile); 
  100.  
  101. private static function _prepare() 
  102. if (! is_file(self::$jarFile)) { 
  103. throw new Exception('Minify_YUICompressor : $jarFile('.self::$jarFile.') is not a valid file.'); 
  104. if (! is_readable(self::$jarFile)) { 
  105. throw new Exception('Minify_YUICompressor : $jarFile('.self::$jarFile.') is not readable.'); 
  106. if (! is_dir(self::$tempDir)) { 
  107. throw new Exception('Minify_YUICompressor : $tempDir('.self::$tempDir.') is not a valid direcotry.'); 
  108. if (! is_writable(self::$tempDir)) { 
  109. throw new Exception('Minify_YUICompressor : $tempDir('.self::$tempDir.') is not writable.'); 
  110.  
  111. public static function testJs(&$error) { 
  112. try { 
  113. Minify_YUICompressor::minifyJs('alert("ok");'); 
  114. $error = 'OK'; 
  115.  
  116. return true; 
  117. } catch (Exception $exception) { 
  118. $error = $exception->getMessage(); 
  119.  
  120. return false; 
  121.  
  122. public static function testCss(&$error) { 
  123. try { 
  124. Minify_YUICompressor::minifyCss('p{color:red}'); 
  125. $error = 'OK'; 
  126.  
  127. return true; 
  128. } catch (Exception $exception) { 
  129. $error = $exception->getMessage(); 
  130.  
  131. return false;