Skip to content

Instantly share code, notes, and snippets.

@hpbuniat
Last active September 28, 2015 05:18
Show Gist options
  • Select an option

  • Save hpbuniat/1390902 to your computer and use it in GitHub Desktop.

Select an option

Save hpbuniat/1390902 to your computer and use it in GitHub Desktop.

Revisions

  1. hpbuniat revised this gist Apr 10, 2013. 1 changed file with 17 additions and 3 deletions.
    20 changes: 17 additions & 3 deletions closure-wrapper.php
    Original file line number Diff line number Diff line change
    @@ -28,8 +28,18 @@ class ClosureCompile {
    '.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
    *
    @@ -46,6 +56,10 @@ public function read(array $aOptions = array()) {
    $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;
    @@ -61,7 +75,7 @@ public function optimize() {
    if ($this->_isIgnored($sFile) !== true) {
    $sNewFile = substr($sFile, 0, -3) . '.min.js';
    $sLevel = $this->_level($sFile);
    $this->_process(sprintf('java -jar /home/developer/bin/closure/closure-compiler.jar --charset "UTF-8" --compilation_level %s --js %s --js_output_file %s', $sLevel, $sFile, $sNewFile));
    $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);
    @@ -145,11 +159,11 @@ private function _isIgnored($sFile) {
    }
    }

    $aOpts = getopt('d:i:');
    $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);
    print_r($aResult);
  2. hpbuniat revised this gist Mar 22, 2012. 1 changed file with 44 additions and 11 deletions.
    55 changes: 44 additions & 11 deletions closure-wrapper.php
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    * Simple Closure-Compiler Wrapper
    * @author Hans-Peter Buniat
    */
    class Closure {
    class ClosureCompile {

    /**
    * The Files to work on
    @@ -19,6 +19,17 @@ class Closure {
    */
    protected $_aResult = array();

    /**
    * Pattern to ignore
    *
    * @var array
    */
    protected $_aIgnore = array(
    '.min.',
    '/min/',
    '-min.',
    );

    /**
    * Get all the js files in the specified folder
    *
    @@ -27,11 +38,16 @@ class Closure {
    * @return Closure
    */
    public function read(array $aOptions = array()) {
    if (empty($aOptions['i']) === true) {
    if (empty($aOptions['d']) === true) {
    throw new InvalidArgumentException('we do need a folder to work on!');
    }

    $this->_aFiles = explode(PHP_EOL, $this->_process('find ' . $aOptions['i'] . ' -type f -name "*.js"'));
    if (empty($aOptions['i']) !== true) {
    $aPattern = explode(',', $aOptions['i']);
    $this->_aIgnore = array_merge($aPattern, $this->_aIgnore);
    }

    $this->_aFiles = explode(PHP_EOL, $this->_process('find ' . $aOptions['d'] . ' -type f -name "*.js"'));
    return $this;
    }

    @@ -42,11 +58,10 @@ public function read(array $aOptions = array()) {
    */
    public function optimize() {
    foreach ($this->_aFiles as $sFile) {
    if (strpos($sFile, '.min.') === false and strpos($sFile, '/min/') === false) {

    if ($this->_isIgnored($sFile) !== true) {
    $sNewFile = substr($sFile, 0, -3) . '.min.js';
    $sLevel = $this->_level($sFile);
    $this->_process(sprintf('java -jar closure-compiler.jar --charset "UTF-8" --compilation_level %s --js %s --js_output_file %s', $sLevel, $sFile, $sNewFile));
    $this->_process(sprintf('java -jar /home/developer/bin/closure/closure-compiler.jar --charset "UTF-8" --compilation_level %s --js %s --js_output_file %s', $sLevel, $sFile, $sNewFile));
    if (file_exists($sNewFile) === true) {
    $iOldSize = filesize($sFile);
    $iNewSize = filesize($sNewFile);
    @@ -110,13 +125,31 @@ private function _level($sFile) {

    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('i:');
    if (empty($aOpts['i']) === true and empty($_GET['i']) !== true) {
    $aOpts['i'] = $_GET['i'];
    $aOpts = getopt('d:i:');
    if (empty($aOpts['d']) === true and empty($_GET['d']) !== true) {
    $aOpts['d'] = $_GET['d'];
    }

    $o = new Closure();
    $o = new ClosureCompile();
    $aResult = $o->read($aOpts)->optimize()->get();
    print_r($aResult);
    print_r($aResult);
  3. hpbuniat created this gist Nov 24, 2011.
    122 changes: 122 additions & 0 deletions closure-wrapper.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,122 @@
    <?php
    /**
    * Simple Closure-Compiler Wrapper
    * @author Hans-Peter Buniat
    */
    class Closure {

    /**
    * The Files to work on
    *
    * @var array
    */
    protected $_aFiles = array();

    /**
    * The compression-result
    *
    * @var array
    */
    protected $_aResult = array();

    /**
    * Get all the js files in the specified folder
    *
    * @param array $aOptions
    *
    * @return Closure
    */
    public function read(array $aOptions = array()) {
    if (empty($aOptions['i']) === true) {
    throw new InvalidArgumentException('we do need a folder to work on!');
    }

    $this->_aFiles = explode(PHP_EOL, $this->_process('find ' . $aOptions['i'] . ' -type f -name "*.js"'));
    return $this;
    }

    /**
    * Run the optimization
    *
    * @return Closure
    */
    public function optimize() {
    foreach ($this->_aFiles as $sFile) {
    if (strpos($sFile, '.min.') === false and strpos($sFile, '/min/') === false) {

    $sNewFile = substr($sFile, 0, -3) . '.min.js';
    $sLevel = $this->_level($sFile);
    $this->_process(sprintf('java -jar closure-compiler.jar --charset "UTF-8" --compilation_level %s --js %s --js_output_file %s', $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;
    }
    }

    $aOpts = getopt('i:');
    if (empty($aOpts['i']) === true and empty($_GET['i']) !== true) {
    $aOpts['i'] = $_GET['i'];
    }

    $o = new Closure();
    $aResult = $o->read($aOpts)->optimize()->get();
    print_r($aResult);