FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Script.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Rsi\Fred\Log;
6 
7 class Script extends \Rsi\Fred\Minify\Handler{
8 
9  protected $_java = null; //!< Location of Java executable.
10  protected $_jar = null; //!< Location of Closure Compiler jar file.
11  protected $_url = 'http://mist.shitware.nl/tool/minify-js.php?key=********'; //!< URL for Closure Compiler service.
12  protected $_ignore = []; //!< Regex's for compiler messages to ignore.
13  protected $_levels = ['error' => Log::ERROR,'warning' => Log::NOTICE]; //!< Translation from compiler message levels to log
14  // levels (defaults to Log::INFO).
15 
16  protected function processMessages($messages,$filename){
17  foreach($messages as $message){
18  foreach($this->_ignore as $ignore) if(($descr = $message['description'] ?? null) && preg_match($ignore,$descr)) continue 2;
19  $prio = $this->_levels[$level = $message['level'] ?? null] ?? Log::INFO;
20  $this->_minify->log->add($prio,"Script $level: $descr",$filename,$message['line'] ?? null,$message);
21  }
22  }
23 
24  protected function local($script,$filename = null){
25  if(!$this->_java || !$this->_jar) return false;
26  $source = \Rsi\File::tempFile('js',$script);
27  $target = \Rsi\File::tempFile('js');
28  $this->processMessages(json_decode(shell_exec("{$this->_java} -jar {$this->_jar} --js $source --language_out ECMASCRIPT_2015 --rewrite_polyfills=false --js_output_file $target --warning_level VERBOSE --error_format JSON 2>&1"),true) ?: [],$filename);
29  $minified = file_get_contents($target);
30  unlink($source);
31  unlink($target);
32  return $minified;
33  }
34 
35  protected function service($script,$filename = null){
36  $ch = curl_init($this->_url);
37  curl_setopt($ch,CURLOPT_POSTFIELDS,$script);
38  curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
39  $response = curl_exec($ch);
40  curl_close($ch);
41  if($response){
42  if($data = json_decode($response,true)){
43  $this->processMessages($data['messages'] ?? [],$filename);
44  return $data['minified'] ?? null;
45  }
46  else $this->_minify->log->error('Invalid response',__FILE__,__LINE__,['response' => $response,'error' => json_last_error_msg()]);
47  }
48  else $this->_minify->log->error('No response',__FILE__,__LINE__,['url' => $this->_url]);
49  return false;
50  }
51 
52  public function source($source,$filename = null){
53  try{
54  return ($this->local($source,$filename) ?: $this->service($source,$filename)) ?: $source;
55  }
56  catch(\Exception $e){
57  $this->_minify->log->error('Error while minimizing script: ' . $e->getMessage(),$e->getFile(),$e->getLine());
58  return $source;
59  }
60  }
61 
62 }
Rsi\Fred\Minify\Handler\Script\$_ignore
$_ignore
Regex's for compiler messages to ignore.
Definition: Script.php:12
Rsi\Fred\Log\NOTICE
const NOTICE
Normal, but significant, condition.
Definition: Log.php:13
Rsi\Fred\Minify\Handler\Script\$_url
$_url
URL for Closure Compiler service.
Definition: Script.php:11
Rsi\Fred\Minify\Handler
Definition: Script.php:3
Rsi\Fred\Log\INFO
const INFO
Informational message.
Definition: Log.php:14
Rsi\Fred\Minify\Handler\Script\$_levels
$_levels
Translation from compiler message levels to log.
Definition: Script.php:13
Rsi\Fred\Log
Rsi\Fred\Minify\Handler\Script\service
service($script, $filename=null)
Definition: Script.php:35
Rsi\Fred\Minify\Handler\Script\local
local($script, $filename=null)
Definition: Script.php:24
Rsi\Fred\Minify\Handler\Script\$_java
$_java
Location of Java executable.
Definition: Script.php:9
Rsi\Fred\Minify\Handler\Script
Definition: Script.php:7
Rsi\Fred\Log\ERROR
const ERROR
Error conditions.
Definition: Log.php:11
Rsi\Fred\Minify\Handler\Script\$_jar
$_jar
Location of Closure Compiler jar file.
Definition: Script.php:10
Rsi\Fred\Minify\Handler\Script\source
source($source, $filename=null)
Minify a source string.
Definition: Script.php:52
Rsi\Fred\Minify\Handler\Script\processMessages
processMessages($messages, $filename)
Definition: Script.php:16
Rsi\Fred\Exception
Definition: Exception.php:5