-
Notifications
You must be signed in to change notification settings - Fork 0
/
closure.php
347 lines (294 loc) · 8.6 KB
/
closure.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?php
/**
* Communication library for Google's online JavaScript compiler.
*
* This small library is used to send JavaScript files or URL list to the
* server and compile it with specified options. Library only handles
* communication and will not handle caching for you.
*
* Library requires PHP version 5.3+
*
* For additional information refer to:
* https://developers.google.com/closure/compiler/
*
* Copyright © 2016 Way2CU. All Rights Reserved.
* Author: Mladen Mijatov <[email protected]>
*/
namespace Library\Closure;
class RemoteServerError extends \Exception {};
class InvalidResponseError extends \Exception {};
class Level {
const WHITESPACE = 'WHITESPACE_ONLY';
const SIMPLE = 'SIMPLE_OPTIMIZATIONS';
const ADVANCED = 'ADVANCED_OPTIMIZATIONS';
}
class Compiler {
private $secure = false;
private $level = Level::SIMPLE;
private $use_library = false;
private $warning_level = 'QUIET';
private $externals = null;
private $externals_url = null;
private $language = 'STABLE';
private $errors = array();
private $warnings = array();
private $files = array();
private $links = array();
private $input_list = array();
private $code = null;
private $optimization_levels = array('WHITESPACE_ONLY', 'SIMPLE_OPTIMIZATIONS', 'ADVANCED_OPTIMIZATIONS');
private $warning_levels = array('QUIET', 'DEFAULT', 'VERBOSE');
private $supported_languages = array(
'ECMASCRIPT3', 'ECMASCRIPT5', 'ECMASCRIPT5_STRICT', 'ECMASCRIPT_2016', 'ECMASCRIPT_2017',
'ECMASCRIPT_2018', 'ECMASCRIPT_2019', 'ECMASCRIPT_2020', 'CMASCRIPT_2021', 'STABLE',
'ECMASCRIPT_NEXT', 'UNSTABLE'
);
// communication endpoint
private $endpoint = '/compile';
private $hostname = 'closure-compiler.appspot.com';
/**
* Add local file to be compiled. If at least one file is specified
* compilation from source instead from URL list will take precedence.
*
* @param string $file_name
*/
public function add_file($file_name) {
if (!in_array($file_name, $this->files) && file_exists($file_name)) {
$index = 'Input_'.count($this->files);
$this->input_list[$index] = $file_name;
$this->files[] = $file_name;
}
}
/**
* Add file to be compiled from specified URL.
*
* @param string $url
*/
public function add_url($url) {
$this->links[] = $url;
}
/**
* Manually set code to be compiler.
*
* @param string $code
*/
public function set_code($code) {
$this->code = $code;
}
/**
* Use secure connection to send request to server.
*
* @param boolean $use_ssl
*/
public function set_secure($use_ssl) {
if (is_bool($use_ssl))
$this->secure = $use_ssl;
}
/**
* Configure API endpoint to allow custom compiler implementations.
*
* @param string $hostname
* @param string $endpoint
*/
public function set_endpoint($hostname=null, $endpoint=null) {
if (!is_null($hostname))
$this->hostname = $hostname;
if (!is_null($endpoint))
$this->endpoint = $endpoint;
}
/**
* Set optimization level.
*
* @param string $level
*/
public function set_level($level) {
if (in_array($level, $this->optimization_levels))
$this->level = $level;
}
/**
* Specify which version of ECMAScript to assume when checking for errors.
* Currently available: ECMASCRIPT3, ECMASCRIPT5, ECMASCRIPT5_STRICT
*
* @param string $language
*/
public function set_language($language) {
if (in_array($language, $this->supported_languages))
$this->language = $language;
}
/**
* Set JavaScript code that declares function names or other symbols. Use this feature to
* preserve symbols that are defined outside of code you are compiling.
*
* @param string $code
*/
public function set_externals($code) {
$this->externals = $code;
}
/**
* Set URL for JavaScript file containing function names and/or other symbols. Use this
* feature to preserve symbols that are defined outside of code you are compiling.
*
* @param string $url
*/
public function set_externals_url($url) {
$this->externals_url = $url;
}
/**
* Get list of errors reported by the compiler.
*
* @return array
*/
public function get_errors() {
return $this->errors;
}
/**
* Get list of warnings reported by the compiler.
*
* @return array
*/
public function get_warnings() {
return $this->warnings;
}
/**
* Get list of files sent for compilation along with their index
* names. These index names will appear in compilation warnings and errors.
*
* @return array
*/
public function get_input_list() {
return $this->input_list;
}
/**
* Compile added files and code and return result as string.
*
* @return string
*/
public function compile() {
$result = null;
// prepare headers and content
$params = $this->prepare_params();
$content = $this->build_query($params);
// send data for compilation
$url = ($this->secure ? 'https' : 'http').'://'.$this->hostname.'/'.$this->endpoint;
$options = array('http' => array(
'method' => 'POST',
'ignore_errors' => true,
'header' => $this->prepare_headers($content),
'content' => $content
));
$context = stream_context_create($options);
$raw_data = file_get_contents($url, false, $context);
if ($raw_data === false)
throw new InvalidResponseError('Closure compilation server did not provide response!');
// decode response
$response = json_decode($raw_data, true);
if (is_null($response))
throw new InvalidResponseError('Closure compilation server did not provide response!');
// handle server side errors
if (array_key_exists('serverErrors', $response)) {
$count = count($response['serverErrors']);
foreach ($response['serverErrors'] as $index => $error) {
$message = 'Compilation error '.$index.'/'.$count.': ';
$message .= (int) $error['code'].' - ';
$message .= (string) $error['message'];
error_log($message);
}
throw new RemoteServerError('Error compiling provided files!');
} else {
if (isset($response['errors']))
$this->errors = $response['errors'];
if (isset($response['warnings']))
$this->warnings = $response['warnings'];
// store response
$result = $response['compiledCode'];
}
return $result;
}
/**
* Compile added files and code and save them to specified file name.
* Return boolean value denotes success in saving to specified file.
*
* @param string $file_name
* @return boolean
* @throws InvalidResponseError
* @throws RemoteServerError
*/
public function compile_and_save($file_name) {
$code = $this->compile();
$result = !is_null($code) && file_put_contents($file_name, $code);
return $result;
}
/**
* Prepare HTTP headers.
*
* @param string $content
* @return string
*/
private function prepare_headers($content) {
$header = array();
$content_length = strlen($content);
// compile default headers
$header[] = 'Content-Type: application/x-www-form-urlencoded';
$header[] = 'Content-Length: '.$content_length;
$header[] = 'Connect-time: 0';
$header[] = 'Connection: close';
return implode("\r\n", $header);
}
/**
* Prepare paramters for sending.
*
* @return array
*/
private function prepare_params() {
$result = array();
// include code to be compiled
if (count($this->files) > 0) {
// join all the files
$code = '';
foreach ($this->files as $file_name)
$code .= file_get_contents($file_name);
// add combined files as parameter
$result['js_code'] = $code;
} else if (!is_null($this->code)) {
// add manually set code as parameter
$result['js_code'] = $this->code;
} else {
// add links
$result['code_url'] = $this->links;
}
// required configuration
$result['compilation_level'] = $this->level;
$result['output_format'] = 'json';
$result['output_info'] = array('compiled_code', 'warnings', 'errors');
// configure externals
if (!is_null($this->externals))
$result['js_externs'] = $this->externals; else
$result['externs_url'] = $this->externals_url;
// optional configuration
$result['use_closure_library'] = $this->use_library ? 'true' : 'false';
$result['warning_level'] = $this->warning_level;
$result['language'] = $this->language;
return $result;
}
/**
* Build query string the proper way. PHP's http_build_query doesn't know
* how to properly create list items so we have to do it manually.
*
* @param array $params
* @return string
*/
private function build_query($params) {
$result = array();
foreach ($params as $key => $value)
if (!is_array($value)) {
// add normal value
$result[] = $key.'='.rawurlencode($value);
} else {
// add param list
foreach ($value as $list_item)
$result[] = $key.'='.rawurlencode($list_item);
}
return implode('&', $result);
}
}
?>