[ Index ] |
PHP Cross Reference of phpBB-3.3.14-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @package s9e\TextFormatter 5 * @copyright Copyright (c) 2010-2022 The s9e authors 6 * @license http://www.opensource.org/licenses/mit-license.php The MIT License 7 */ 8 namespace s9e\TextFormatter\Configurator\JavaScript\Minifiers; 9 10 use RuntimeException; 11 use s9e\TextFormatter\Configurator\JavaScript\OnlineMinifier; 12 13 class ClosureCompilerService extends OnlineMinifier 14 { 15 /** 16 * @var string Closure Compiler's compilation level 17 */ 18 public $compilationLevel = 'ADVANCED_OPTIMIZATIONS'; 19 20 /** 21 * @var bool Whether to exclude Closure Compiler's default externs 22 */ 23 public $excludeDefaultExterns = true; 24 25 /** 26 * @var string Externs used for compilation 27 */ 28 public $externs; 29 30 /** 31 * @var string Closure Compiler Service's URL 32 */ 33 public $url = 'https://closure-compiler.appspot.com/compile'; 34 35 /** 36 * Constructor 37 */ 38 public function __construct() 39 { 40 parent::__construct(); 41 $this->externs = file_get_contents(__DIR__ . '/../externs.service.js'); 42 } 43 44 /** 45 * {@inheritdoc} 46 */ 47 public function getCacheDifferentiator() 48 { 49 $key = [$this->compilationLevel, $this->excludeDefaultExterns]; 50 51 if ($this->excludeDefaultExterns) 52 { 53 $key[] = $this->externs; 54 } 55 56 return $key; 57 } 58 59 /** 60 * Compile given JavaScript source via the Closure Compiler Service 61 * 62 * @param string $src JavaScript source 63 * @return string Compiled source 64 */ 65 public function minify($src) 66 { 67 $body = $this->generateRequestBody($src); 68 $response = $this->query($body); 69 if ($response === false) 70 { 71 throw new RuntimeException('Could not contact the Closure Compiler service'); 72 } 73 74 return $this->decodeResponse($response); 75 } 76 77 /** 78 * Decode the response returned by the Closure Compiler service 79 * 80 * @param string $response Response body 81 * @return string Minified code 82 */ 83 protected function decodeResponse($response) 84 { 85 $response = json_decode($response, true); 86 if (is_null($response)) 87 { 88 throw new RuntimeException('Closure Compiler service returned invalid JSON: ' . json_last_error_msg()); 89 } 90 91 if (isset($response['serverErrors'][0])) 92 { 93 $error = $response['serverErrors'][0]; 94 95 throw new RuntimeException('Server error ' . $error['code'] . ': ' . $error['error']); 96 } 97 98 if (isset($response['errors'][0])) 99 { 100 $error = $response['errors'][0]; 101 102 throw new RuntimeException('Compilation error: ' . $error['error']); 103 } 104 105 return $response['compiledCode']; 106 } 107 108 /** 109 * Generate the request body for given code 110 * 111 * @param string $src JavaScript source 112 * @return string Compiled source 113 */ 114 protected function generateRequestBody($src) 115 { 116 $params = [ 117 'compilation_level' => $this->compilationLevel, 118 'js_code' => $src, 119 'output_format' => 'json', 120 'output_info' => 'compiled_code' 121 ]; 122 123 // Add our custom externs if default externs are disabled 124 if ($this->excludeDefaultExterns && $this->compilationLevel === 'ADVANCED_OPTIMIZATIONS') 125 { 126 $params['exclude_default_externs'] = 'true'; 127 $params['js_externs'] = $this->externs; 128 } 129 130 // Add dupe variables by hand 131 $body = http_build_query($params) . '&output_info=errors'; 132 133 return $body; 134 } 135 136 /** 137 * Query the Closure Compiler service 138 * 139 * @param string $body Request body 140 * @return string|false Response body, or FALSE 141 */ 142 protected function query($body) 143 { 144 return $this->httpClient->post( 145 $this->url, 146 ['headers' => ['Content-Type: application/x-www-form-urlencoded']], 147 $body 148 ); 149 } 150 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Mon Nov 25 19:05:08 2024 | Cross-referenced by PHPXref 0.7.1 |