From b2c2e80cd031dfc85db212972031fd2056d3e8b0 Mon Sep 17 00:00:00 2001 From: Jan Brauer Date: Wed, 15 Jan 2014 16:54:47 +0100 Subject: [PATCH] Support for fast-closure-compiler --- min/lib/Minify/FastClosureCompiler.php | 114 +++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 min/lib/Minify/FastClosureCompiler.php diff --git a/min/lib/Minify/FastClosureCompiler.php b/min/lib/Minify/FastClosureCompiler.php new file mode 100644 index 0000000..2ace4d8 --- /dev/null +++ b/min/lib/Minify/FastClosureCompiler.php @@ -0,0 +1,114 @@ + + * Minify_ClosureCompiler::$binPath = '/usr/local/bin/closure'; + * Minify_ClosureCompiler::$tempDir = '/tmp'; + * $code = Minify_ClosureCompiler::minify( + * $code, + * array('compilation_level' => 'SIMPLE_OPTIMIZATIONS') + * ); + * + * --compilation_level WHITESPACE_ONLY, SIMPLE_OPTIMIZATIONS, ADVANCED_OPTIMIZATIONS + * + * + * + * @todo unit tests, $options docs + * @todo more options support (or should just passthru them all?) + * + * @package Minify + * @author Stephen Clay + * @author Elan Ruusamäe + */ +class Minify_ClosureCompiler { + + /** + * Filepath of the Closure Compiler jar file. This must be set before + * calling minifyJs(). + * + * @var string + */ + public static $binPath = '/usr/local/bin/closure'; + + /** + * Writable temp directory. This must be set before calling minifyJs(). + * + * @var string + */ + public static $tempDir = '/tmp'; + + /** + * Minify a Javascript string + * + * @param string $js + * + * @param array $options (verbose is ignored) + * + * @see https://code.google.com/p/closure-compiler/source/browse/trunk/README + * + * @return string + */ + public static function minify($js, $options = array()) + { + self::_prepare(); + if (! ($tmpFile = tempnam(self::$tempDir, 'cc_'))) { + throw new Exception('Minify_FastClosureCompiler : could not create temp file in "'.self::$tempDir.'".'); + } + file_put_contents($tmpFile, $js); + exec(self::_getCmd($options, $tmpFile), $output, $result_code); + unlink($tmpFile); + if ($result_code != 0) { + throw new Exception('Minify_FastClosureCompiler : Closure Compiler execution failed.'); + } + return implode("\n", $output); + } + + private static function _getCmd($userOptions, $tmpFile) + { + $o = array_merge( + array( + 'charset' => 'utf-8', + 'compilation_level' => 'SIMPLE_OPTIMIZATIONS', + ), + $userOptions + ); + $cmd = self::$binPath . ' ' . + . (preg_match('/^[\\da-zA-Z0-9\\-]+$/', $o['charset']) + ? " --charset {$o['charset']}" + : ''); + + foreach (array('compilation_level') as $opt) { + if ($o[$opt]) { + $cmd .= " --{$opt} ". escapeshellarg($o[$opt]); + } + } + return $cmd . ' ' . escapeshellarg($tmpFile); + } + + private static function _prepare() + { + if (! is_file(self::$binPath)) { + throw new Exception('Minify_FastClosureCompiler : $binPath('.self::$binPath.') is not a valid file.'); + } + if (! is_readable(self::$binPath)) { + throw new Exception('Minify_FastClosureCompiler : $binPath('.self::$binPath.') is not readable.'); + } + if (! is_dir(self::$tempDir)) { + throw new Exception('Minify_FastClosureCompiler : $tempDir('.self::$tempDir.') is not a valid directory.'); + } + if (! is_writable(self::$tempDir)) { + throw new Exception('Minify_FastClosureCompiler : $tempDir('.self::$tempDir.') is not writable.'); + } + } +} + +/* vim:ts=4:sw=4:et */