text changes to registration mail content
[namibia] / public / min / lib / Minify / ClosureCompiler.php
1 <?php
2 /**
3  * Class Minify_ClosureCompiler
4  * @package Minify
5  */
6
7 /**
8  * Compress Javascript using the Closure Compiler
9  *
10  * You must set $jarFile and $tempDir before calling the minify functions.
11  * Also, depending on your shell's environment, you may need to specify
12  * the full path to java in $javaExecutable or use putenv() to setup the
13  * Java environment.
14  *
15  * <code>
16  * Minify_ClosureCompiler::$jarFile = '/path/to/closure-compiler-20120123.jar';
17  * Minify_ClosureCompiler::$tempDir = '/tmp';
18  * $code = Minify_ClosureCompiler::minify(
19  *   $code,
20  *   array('compilation_level' => 'SIMPLE_OPTIMIZATIONS')
21  * );
22  *
23  * --compilation_level WHITESPACE_ONLY, SIMPLE_OPTIMIZATIONS, ADVANCED_OPTIMIZATIONS
24  *
25  * </code>
26  *
27  * @todo unit tests, $options docs
28  * @todo more options support (or should just passthru them all?)
29  *
30  * @package Minify
31  * @author Stephen Clay <steve@mrclay.org>
32  * @author Elan Ruusamäe <glen@delfi.ee>
33  */
34 class Minify_ClosureCompiler {
35
36     /**
37      * Filepath of the Closure Compiler jar file. This must be set before
38      * calling minifyJs().
39      *
40      * @var string
41      */
42     public static $jarFile = null;
43
44     /**
45      * Writable temp directory. This must be set before calling minifyJs().
46      *
47      * @var string
48      */
49     public static $tempDir = null;
50
51     /**
52      * Filepath of "java" executable (may be needed if not in shell's PATH)
53      *
54      * @var string
55      */
56     public static $javaExecutable = 'java';
57
58     /**
59      * Minify a Javascript string
60      *
61      * @param string $js
62      *
63      * @param array $options (verbose is ignored)
64      *
65      * @see https://code.google.com/p/closure-compiler/source/browse/trunk/README
66      *
67      * @return string
68      */
69     public static function minify($js, $options = array())
70     {
71         self::_prepare();
72         if (! ($tmpFile = tempnam(self::$tempDir, 'cc_'))) {
73             throw new Exception('Minify_ClosureCompiler : could not create temp file.');
74         }
75         file_put_contents($tmpFile, $js);
76         exec(self::_getCmd($options, $tmpFile), $output, $result_code);
77         unlink($tmpFile);
78         if ($result_code != 0) {
79             throw new Exception('Minify_ClosureCompiler : Closure Compiler execution failed.');
80         }
81         return implode("\n", $output);
82     }
83
84     private static function _getCmd($userOptions, $tmpFile)
85     {
86         $o = array_merge(
87             array(
88                 'charset' => 'utf-8',
89                 'compilation_level' => 'SIMPLE_OPTIMIZATIONS',
90             ),
91             $userOptions
92         );
93         $cmd = self::$javaExecutable . ' -jar ' . escapeshellarg(self::$jarFile)
94              . (preg_match('/^[\\da-zA-Z0-9\\-]+$/', $o['charset'])
95                 ? " --charset {$o['charset']}"
96                 : '');
97
98         foreach (array('compilation_level') as $opt) {
99             if ($o[$opt]) {
100                 $cmd .= " --{$opt} ". escapeshellarg($o[$opt]);
101             }
102         }
103         return $cmd . ' ' . escapeshellarg($tmpFile);
104     }
105
106     private static function _prepare()
107     {
108         if (! is_file(self::$jarFile)) {
109             throw new Exception('Minify_ClosureCompiler : $jarFile('.self::$jarFile.') is not a valid file.');
110         }
111         if (! is_readable(self::$jarFile)) {
112             throw new Exception('Minify_ClosureCompiler : $jarFile('.self::$jarFile.') is not readable.');
113         }
114         if (! is_dir(self::$tempDir)) {
115             throw new Exception('Minify_ClosureCompiler : $tempDir('.self::$tempDir.') is not a valid direcotry.');
116         }
117         if (! is_writable(self::$tempDir)) {
118             throw new Exception('Minify_ClosureCompiler : $tempDir('.self::$tempDir.') is not writable.');
119         }
120     }
121 }
122
123 /* vim:ts=4:sw=4:et */