3 * Class Minify_YUICompressor
8 * Compress Javascript/CSS using the YUI Compressor
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
16 * Minify_YUICompressor::$jarFile = '/path/to/yuicompressor-2.4.6.jar';
17 * Minify_YUICompressor::$tempDir = '/tmp';
18 * $code = Minify_YUICompressor::minifyJs(
20 * ,array('nomunge' => true, 'line-break' => 1000)
24 * Note: In case you run out stack (default is 512k), you may increase stack size in $options:
25 * array('stack-size' => '2048k')
27 * @todo unit tests, $options docs
30 * @author Stephen Clay <steve@mrclay.org>
32 class Minify_YUICompressor {
35 * Filepath of the YUI Compressor jar file. This must be set before
36 * calling minifyJs() or minifyCss().
40 public static $jarFile = null;
43 * Writable temp directory. This must be set before calling minifyJs()
48 public static $tempDir = null;
51 * Filepath of "java" executable (may be needed if not in shell's PATH)
55 public static $javaExecutable = 'java';
58 * Minify a Javascript string
62 * @param array $options (verbose is ignored)
64 * @see http://www.julienlecomte.net/yuicompressor/README
68 public static function minifyJs($js, $options = array())
70 return self::_minify('js', $js, $options);
78 * @param array $options (verbose is ignored)
80 * @see http://www.julienlecomte.net/yuicompressor/README
84 public static function minifyCss($css, $options = array())
86 return self::_minify('css', $css, $options);
89 private static function _minify($type, $content, $options)
92 if (! ($tmpFile = tempnam(self::$tempDir, 'yuic_'))) {
93 throw new Exception('Minify_YUICompressor : could not create temp file.');
95 file_put_contents($tmpFile, $content);
96 exec(self::_getCmd($options, $type, $tmpFile), $output, $result_code);
98 if ($result_code != 0) {
99 throw new Exception('Minify_YUICompressor : YUI compressor execution failed.');
101 return implode("\n", $output);
104 private static function _getCmd($userOptions, $type, $tmpFile)
109 ,'line-break' => 5000
112 ,'preserve-semi' => false
113 ,'disable-optimizations' => false
118 $cmd = self::$javaExecutable
119 . (!empty($o['stack-size'])
120 ? ' -Xss' . $o['stack-size']
122 . ' -jar ' . escapeshellarg(self::$jarFile)
124 . (preg_match('/^[\\da-zA-Z0-9\\-]+$/', $o['charset'])
125 ? " --charset {$o['charset']}"
127 . (is_numeric($o['line-break']) && $o['line-break'] >= 0
128 ? ' --line-break ' . (int)$o['line-break']
130 if ($type === 'js') {
131 foreach (array('nomunge', 'preserve-semi', 'disable-optimizations') as $opt) {
137 return $cmd . ' ' . escapeshellarg($tmpFile);
140 private static function _prepare()
142 if (! is_file(self::$jarFile)) {
143 throw new Exception('Minify_YUICompressor : $jarFile('.self::$jarFile.') is not a valid file.');
145 if (! is_readable(self::$jarFile)) {
146 throw new Exception('Minify_YUICompressor : $jarFile('.self::$jarFile.') is not readable.');
148 if (! is_dir(self::$tempDir)) {
149 throw new Exception('Minify_YUICompressor : $tempDir('.self::$tempDir.') is not a valid direcotry.');
151 if (! is_writable(self::$tempDir)) {
152 throw new Exception('Minify_YUICompressor : $tempDir('.self::$tempDir.') is not writable.');