Last active
September 28, 2015 05:18
-
-
Save hpbuniat/1390902 to your computer and use it in GitHub Desktop.
Simple wrapper for google closure compiler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Simple Closure-Compiler Wrapper | |
* @author Hans-Peter Buniat | |
*/ | |
class ClosureCompile { | |
/** | |
* The Files to work on | |
* | |
* @var array | |
*/ | |
protected $_aFiles = array(); | |
/** | |
* The compression-result | |
* | |
* @var array | |
*/ | |
protected $_aResult = array(); | |
/** | |
* Pattern to ignore | |
* | |
* @var array | |
*/ | |
protected $_aIgnore = array( | |
'.min.', | |
'/min/', | |
'-min.', | |
'vendor/', | |
'components/', | |
); | |
/** | |
* Path to the closure-compiler | |
* | |
* @var string | |
*/ | |
protected $_sClosure = '/home/developer/bin/closure/closure-compiler.jar'; | |
/** | |
* Get all the js files in the specified folder | |
* | |
* @param array $aOptions | |
* | |
* @return Closure | |
*/ | |
public function read(array $aOptions = array()) { | |
if (empty($aOptions['d']) === true) { | |
throw new InvalidArgumentException('we do need a folder to work on!'); | |
} | |
if (empty($aOptions['i']) !== true) { | |
$aPattern = explode(',', $aOptions['i']); | |
$this->_aIgnore = array_merge($aPattern, $this->_aIgnore); | |
} | |
if (empty($aOptions['c']) !== true and file_exists($aOptions['c']) === tue) { | |
$this->_sClosure = $aOptions['c']; | |
} | |
$this->_aFiles = explode(PHP_EOL, $this->_process('find ' . $aOptions['d'] . ' -type f -name "*.js"')); | |
return $this; | |
} | |
/** | |
* Run the optimization | |
* | |
* @return Closure | |
*/ | |
public function optimize() { | |
foreach ($this->_aFiles as $sFile) { | |
if ($this->_isIgnored($sFile) !== true) { | |
$sNewFile = substr($sFile, 0, -3) . '.min.js'; | |
$sLevel = $this->_level($sFile); | |
$this->_process(sprintf('java -jar %s --charset "UTF-8" --compilation_level %s --js %s --js_output_file %s', $this->_sClosure, $sLevel, $sFile, $sNewFile)); | |
if (file_exists($sNewFile) === true) { | |
$iOldSize = filesize($sFile); | |
$iNewSize = filesize($sNewFile); | |
if ($iNewSize > $iOldSize) { | |
unlink($sNewFile); | |
} | |
else { | |
$this->_aResult[] = array( | |
'file' => $sFile, | |
'level' => $sLevel, | |
'o' => $iOldSize, | |
'n' => $iNewSize, | |
'r' => round($iNewSize / $iOldSize, 2) | |
); | |
} | |
} | |
} | |
} | |
return $this; | |
} | |
/** | |
* Return the results | |
* | |
* @return array | |
*/ | |
public function get() { | |
return $this->_aResult; | |
} | |
/** | |
* Process a command | |
* | |
* @return string | |
*/ | |
private function _process($sCommand) { | |
$rCommand = popen($sCommand, 'r'); | |
$sReturn = ''; | |
while (feof($rCommand) !== true) { | |
$sReturn .= fread($rCommand, 4096); | |
} | |
pclose($rCommand); | |
return trim($sReturn); | |
} | |
/** | |
* Get the compression level | |
* | |
* @param string $sFile The current file | |
* | |
* @return string | |
*/ | |
private function _level($sFile) { | |
$sLevel = 'SIMPLE_OPTIMIZATIONS'; | |
$sContent = file_get_contents($sFile); | |
if (stristr($sContent, '@compilation_level WHITESPACE_ONLY') !== false) { | |
$sLevel = 'WHITESPACE_ONLY'; | |
} | |
return $sLevel; | |
} | |
/** | |
* Check if a file should be ignored | |
* | |
* @param string $sFile | |
* | |
* @return boolean | |
*/ | |
private function _isIgnored($sFile) { | |
$bReturn = false; | |
foreach ($this->_aIgnore as $sIgnore) { | |
if (stripos($sFile, $sIgnore) !== false) { | |
$bReturn = true; | |
} | |
} | |
return $bReturn; | |
} | |
} | |
$aOpts = getopt('d:i:c:'); | |
if (empty($aOpts['d']) === true and empty($_GET['d']) !== true) { | |
$aOpts['d'] = $_GET['d']; | |
} | |
$o = new ClosureCompile(); | |
$aResult = $o->read($aOpts)->optimize()->get(); | |
print_r($aResult); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment