ClosureCompiler.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Class Minify_ClosureCompiler
  4. * @package Minify
  5. */
  6. /**
  7. * Compress Javascript using the Closure Compiler
  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_ClosureCompiler::$jarFile = '/path/to/closure-compiler-20120123.jar';
  16. * Minify_ClosureCompiler::$tempDir = '/tmp';
  17. * $code = Minify_ClosureCompiler::minify(
  18. * $code,
  19. * array('compilation_level' => 'SIMPLE_OPTIMIZATIONS')
  20. * );
  21. *
  22. * --compilation_level WHITESPACE_ONLY, SIMPLE_OPTIMIZATIONS, ADVANCED_OPTIMIZATIONS
  23. *
  24. * </code>
  25. *
  26. * @todo unit tests, $options docs
  27. * @todo more options support (or should just passthru them all?)
  28. *
  29. * @package Minify
  30. * @author Stephen Clay <steve@mrclay.org>
  31. * @author Elan Ruusamäe <glen@delfi.ee>
  32. */
  33. class Minify_ClosureCompiler {
  34. const OPTION_CHARSET = 'charset';
  35. const OPTION_COMPILATION_LEVEL = 'compilation_level';
  36. public static $isDebug = false;
  37. /**
  38. * Filepath of the Closure Compiler jar file. This must be set before
  39. * calling minifyJs().
  40. *
  41. * @var string
  42. */
  43. public static $jarFile = null;
  44. /**
  45. * Writable temp directory. This must be set before calling minifyJs().
  46. *
  47. * @var string
  48. */
  49. public static $tempDir = null;
  50. /**
  51. * Filepath of "java" executable (may be needed if not in shell's PATH)
  52. *
  53. * @var string
  54. */
  55. public static $javaExecutable = 'java';
  56. /**
  57. * Minify a Javascript string
  58. *
  59. * @param string $js
  60. *
  61. * @param array $options (verbose is ignored)
  62. *
  63. * @see https://code.google.com/p/closure-compiler/source/browse/trunk/README
  64. *
  65. * @return string
  66. *
  67. * @throws Minify_ClosureCompiler_Exception
  68. */
  69. public static function minify($js, $options = array())
  70. {
  71. self::_prepare();
  72. if (! ($tmpFile = tempnam(self::$tempDir, 'cc_'))) {
  73. throw new Minify_ClosureCompiler_Exception('Minify_ClosureCompiler : could not create temp file in "'.self::$tempDir.'".');
  74. }
  75. file_put_contents($tmpFile, $js);
  76. $cmd = self::_getCmd($options, $tmpFile);
  77. exec($cmd, $output, $result_code);
  78. unlink($tmpFile);
  79. if ($result_code != 0) {
  80. $message = 'Minify_ClosureCompiler : Closure Compiler execution failed.';
  81. if (self::$isDebug) {
  82. exec($cmd . ' 2>&1', $error);
  83. if ($error) {
  84. $message .= "\nReason:\n" . join("\n", $error);
  85. }
  86. }
  87. throw new Minify_ClosureCompiler_Exception($message);
  88. }
  89. return implode("\n", $output);
  90. }
  91. private static function _getCmd($userOptions, $tmpFile)
  92. {
  93. $o = array_merge(
  94. array(
  95. self::OPTION_CHARSET => 'utf-8',
  96. self::OPTION_COMPILATION_LEVEL => 'SIMPLE_OPTIMIZATIONS',
  97. ),
  98. $userOptions
  99. );
  100. $charsetOption = $o[self::OPTION_CHARSET];
  101. $cmd = self::$javaExecutable . ' -jar ' . escapeshellarg(self::$jarFile)
  102. . (preg_match('/^[\\da-zA-Z0-9\\-]+$/', $charsetOption)
  103. ? " --charset {$charsetOption}"
  104. : '');
  105. foreach (array(self::OPTION_COMPILATION_LEVEL) as $opt) {
  106. if ($o[$opt]) {
  107. $cmd .= " --{$opt} ". escapeshellarg($o[$opt]);
  108. }
  109. }
  110. return $cmd . ' ' . escapeshellarg($tmpFile);
  111. }
  112. private static function _prepare()
  113. {
  114. if (! is_file(self::$jarFile)) {
  115. throw new Minify_ClosureCompiler_Exception('Minify_ClosureCompiler : $jarFile('.self::$jarFile.') is not a valid file.');
  116. }
  117. if (! is_readable(self::$jarFile)) {
  118. throw new Minify_ClosureCompiler_Exception('Minify_ClosureCompiler : $jarFile('.self::$jarFile.') is not readable.');
  119. }
  120. if (! is_dir(self::$tempDir)) {
  121. throw new Minify_ClosureCompiler_Exception('Minify_ClosureCompiler : $tempDir('.self::$tempDir.') is not a valid direcotry.');
  122. }
  123. if (! is_writable(self::$tempDir)) {
  124. throw new Minify_ClosureCompiler_Exception('Minify_ClosureCompiler : $tempDir('.self::$tempDir.') is not writable.');
  125. }
  126. }
  127. }
  128. class Minify_ClosureCompiler_Exception extends Exception {}