[ Index ] |
PHP Cross Reference of phpBB-3.2.11-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* 4 * @package s9e\TextFormatter 5 * @copyright Copyright (c) 2010-2019 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 use RuntimeException; 10 use s9e\TextFormatter\Configurator\JavaScript\OnlineMinifier; 11 class ClosureCompilerService extends OnlineMinifier 12 { 13 public $compilationLevel = 'ADVANCED_OPTIMIZATIONS'; 14 public $excludeDefaultExterns = \true; 15 public $externs; 16 public $url = 'https://closure-compiler.appspot.com/compile'; 17 public function __construct() 18 { 19 parent::__construct(); 20 $this->externs = \file_get_contents(__DIR__ . '/../externs.service.js'); 21 } 22 public function getCacheDifferentiator() 23 { 24 $key = [$this->compilationLevel, $this->excludeDefaultExterns]; 25 if ($this->excludeDefaultExterns) 26 $key[] = $this->externs; 27 return $key; 28 } 29 public function minify($src) 30 { 31 $body = $this->generateRequestBody($src); 32 $response = $this->query($body); 33 if ($response === \false) 34 throw new RuntimeException('Could not contact the Closure Compiler service'); 35 return $this->decodeResponse($response); 36 } 37 protected function decodeResponse($response) 38 { 39 $response = \json_decode($response, \true); 40 if (\is_null($response)) 41 { 42 $msgs = array( 43 \JSON_ERROR_NONE => 'No error', 44 \JSON_ERROR_DEPTH => 'Maximum stack depth exceeded', 45 \JSON_ERROR_STATE_MISMATCH => 'State mismatch (invalid or malformed JSON)', 46 \JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded', 47 \JSON_ERROR_SYNTAX => 'Syntax error', 48 \JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded' 49 ); 50 throw new RuntimeException('Closure Compiler service returned invalid JSON: ' . (isset($msgs[\json_last_error()]) ? $msgs[\json_last_error()] : 'Unknown error')); 51 } 52 if (isset($response['serverErrors'][0])) 53 { 54 $error = $response['serverErrors'][0]; 55 throw new RuntimeException('Server error ' . $error['code'] . ': ' . $error['error']); 56 } 57 if (isset($response['errors'][0])) 58 { 59 $error = $response['errors'][0]; 60 throw new RuntimeException('Compilation error: ' . $error['error']); 61 } 62 return $response['compiledCode']; 63 } 64 protected function generateRequestBody($src) 65 { 66 $params = [ 67 'compilation_level' => $this->compilationLevel, 68 'js_code' => $src, 69 'output_format' => 'json', 70 'output_info' => 'compiled_code' 71 ]; 72 if ($this->excludeDefaultExterns && $this->compilationLevel === 'ADVANCED_OPTIMIZATIONS') 73 { 74 $params['exclude_default_externs'] = 'true'; 75 $params['js_externs'] = $this->externs; 76 } 77 $body = \http_build_query($params) . '&output_info=errors'; 78 return $body; 79 } 80 protected function query($body) 81 { 82 return $this->httpClient->post( 83 $this->url, 84 ['Content-Type: application/x-www-form-urlencoded'], 85 $body 86 ); 87 } 88 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 11 20:33:01 2020 | Cross-referenced by PHPXref 0.7.1 |