Minify_JS_ClosureCompiler

Minify Javascript using Google's Closure Compiler API.

Defined (1)

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

/lib/Minify/Minify/JS/ClosureCompiler.php  
  1. class Minify_JS_ClosureCompiler { 
  2. const URL = 'http://closure-compiler.appspot.com/compile'; 
  3.  
  4. /** 
  5. * Minify Javascript code via HTTP request to the Closure Compiler API 
  6. * @param string $js input code 
  7. * @param array $options unused at this point 
  8. * @return string 
  9. */ 
  10. public static function minify($js, array $options = array()) 
  11. $obj = new self($options); 
  12. return $obj->min($js); 
  13.  
  14. /** 
  15. * @param array $options 
  16. * fallbackFunc : default array($this, 'fallback'); 
  17. */ 
  18. public function __construct(array $options = array()) 
  19. $this->options = $options; 
  20. $this->_fallbackFunc = isset($options['fallbackMinifier']) 
  21. ? $options['fallbackMinifier'] 
  22. : array($this, '_fallback'); 
  23.  
  24. public function min($js) 
  25. if (trim($js) === '') 
  26. return $js; 
  27.  
  28. $postBody = $this->_buildPostBody($js); 
  29. $bytes = (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) 
  30. ? mb_strlen($postBody, '8bit') 
  31. : strlen($postBody); 
  32. if ($bytes > 200000) 
  33. return $this->fail($js,  
  34. 'File size is larger than Closure Compiler API limit (200000 bytes)'); 
  35.  
  36. $response = $this->_getResponse($postBody); 
  37. if (preg_match('/^Error\(\d\d?\):/', $response)) 
  38. return $this->fail($response,  
  39. "Received errors from Closure Compiler API:\n$response"); 
  40.  
  41. return $response; 
  42.  
  43. private function fail($js, $errorMessage) { 
  44. Minify0_Minify::$recoverableError = $errorMessage; 
  45. $response = "/* " . $errorMessage . "\n(Using fallback minifier)\n*/\n"; 
  46. if (is_callable($this->_fallbackFunc)) 
  47. $response .= call_user_func($this->_fallbackFunc, $js); 
  48. else 
  49. $response .= $js; 
  50.  
  51. return $response; 
  52.  
  53. protected $_fallbackFunc = null; 
  54. protected $_options = array(); 
  55.  
  56. protected function _getResponse($postBody) 
  57. $allowUrlFopen = preg_match('/1|yes|on|true/i', ini_get('allow_url_fopen')); 
  58. if ($allowUrlFopen) { 
  59. $contents = file_get_contents(self::URL, false, stream_context_create(array( 
  60. 'http' => array( 
  61. 'method' => 'POST',  
  62. 'header' => "Content-type: application/x-www-form-urlencoded\r\nConnection: close\r\n",  
  63. 'content' => $postBody,  
  64. 'max_redirects' => 0,  
  65. 'timeout' => 15,  
  66. ))); 
  67. } elseif (defined('CURLOPT_POST')) { 
  68. $ch = curl_init(self::URL); 
  69. curl_setopt($ch, CURLOPT_POST, true); 
  70. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
  71. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/x-www-form-urlencoded')); 
  72. curl_setopt($ch, CURLOPT_POSTFIELDS, $postBody); 
  73. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); 
  74. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); 
  75. $contents = curl_exec($ch); 
  76. curl_close($ch); 
  77. } else { 
  78. throw new Minify_JS_ClosureCompiler_Exception( 
  79. "Could not make HTTP request: allow_url_open is false and cURL not available" 
  80. ); 
  81. if (false === $contents) { 
  82. throw new Minify_JS_ClosureCompiler_Exception( 
  83. "No HTTP response from server" 
  84. ); 
  85. return trim($contents); 
  86.  
  87. protected function _buildPostBody($js, $returnErrors = false) 
  88. $a = array( 
  89. 'js_code' => $js,  
  90. 'output_info' => ($returnErrors ? 'errors' : 'compiled_code'),  
  91. 'output_format' => 'text',  
  92. 'compilation_level' =>  
  93. (isset($this->options['compilation_level']) ?  
  94. $this->options['compilation_level'] :  
  95. 'SIMPLE_OPTIMIZATIONS') 
  96. ); 
  97. if (isset($this->options['formatting']) && !empty($this->options['formatting'])) 
  98. $a['formatting'] = $this->options['formatting']; 
  99.  
  100. return http_build_query($a, null, '&'); 
  101.  
  102. /** 
  103. * Default fallback function if CC API fails 
  104. * @param string $js 
  105. * @return string 
  106. */ 
  107. protected function _fallback($js) 
  108. return Minify0_JSMin::minify($js); 
  109.  
  110. public static function test(&$error) { 
  111. try { 
  112. self::minify('alert("ok");'); 
  113. $error = 'OK'; 
  114.  
  115. return true; 
  116. } catch (Exception $exception) { 
  117. $error = $exception->getMessage(); 
  118.  
  119. return false;