text changes to registration mail content
[namibia] / public / min / lib / Minify / YUICompressor.php
1 <?php
2 /**
3  * Class Minify_YUICompressor 
4  * @package Minify
5  */
6
7 /**
8  * Compress Javascript/CSS using the YUI Compressor
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_YUICompressor::$jarFile = '/path/to/yuicompressor-2.4.6.jar';
17  * Minify_YUICompressor::$tempDir = '/tmp';
18  * $code = Minify_YUICompressor::minifyJs(
19  *   $code
20  *   ,array('nomunge' => true, 'line-break' => 1000)
21  * );
22  * </code>
23  *
24  * Note: In case you run out stack (default is 512k), you may increase stack size in $options:
25  *   array('stack-size' => '2048k')
26  *
27  * @todo unit tests, $options docs
28  * 
29  * @package Minify
30  * @author Stephen Clay <steve@mrclay.org>
31  */
32 class Minify_YUICompressor {
33
34     /**
35      * Filepath of the YUI Compressor jar file. This must be set before
36      * calling minifyJs() or minifyCss().
37      *
38      * @var string
39      */
40     public static $jarFile = null;
41     
42     /**
43      * Writable temp directory. This must be set before calling minifyJs()
44      * or minifyCss().
45      *
46      * @var string
47      */
48     public static $tempDir = null;
49     
50     /**
51      * Filepath of "java" executable (may be needed if not in shell's PATH)
52      *
53      * @var string
54      */
55     public static $javaExecutable = 'java';
56     
57     /**
58      * Minify a Javascript string
59      * 
60      * @param string $js
61      * 
62      * @param array $options (verbose is ignored)
63      * 
64      * @see http://www.julienlecomte.net/yuicompressor/README
65      * 
66      * @return string 
67      */
68     public static function minifyJs($js, $options = array())
69     {
70         return self::_minify('js', $js, $options);
71     }
72     
73     /**
74      * Minify a CSS string
75      * 
76      * @param string $css
77      * 
78      * @param array $options (verbose is ignored)
79      * 
80      * @see http://www.julienlecomte.net/yuicompressor/README
81      * 
82      * @return string 
83      */
84     public static function minifyCss($css, $options = array())
85     {
86         return self::_minify('css', $css, $options);
87     }
88     
89     private static function _minify($type, $content, $options)
90     {
91         self::_prepare();
92         if (! ($tmpFile = tempnam(self::$tempDir, 'yuic_'))) {
93             throw new Exception('Minify_YUICompressor : could not create temp file.');
94         }
95         file_put_contents($tmpFile, $content);
96         exec(self::_getCmd($options, $type, $tmpFile), $output, $result_code);
97         unlink($tmpFile);
98         if ($result_code != 0) {
99             throw new Exception('Minify_YUICompressor : YUI compressor execution failed.');
100         }
101         return implode("\n", $output);
102     }
103     
104     private static function _getCmd($userOptions, $type, $tmpFile)
105     {
106         $o = array_merge(
107             array(
108                 'charset' => ''
109                 ,'line-break' => 5000
110                 ,'type' => $type
111                 ,'nomunge' => false
112                 ,'preserve-semi' => false
113                 ,'disable-optimizations' => false
114                     ,'stack-size' => ''
115             )
116             ,$userOptions
117         );
118         $cmd = self::$javaExecutable
119                  . (!empty($o['stack-size'])
120                     ? ' -Xss' . $o['stack-size']
121                     : '')
122                  . ' -jar ' . escapeshellarg(self::$jarFile)
123              . " --type {$type}"
124              . (preg_match('/^[\\da-zA-Z0-9\\-]+$/', $o['charset'])
125                 ? " --charset {$o['charset']}" 
126                 : '')
127              . (is_numeric($o['line-break']) && $o['line-break'] >= 0
128                 ? ' --line-break ' . (int)$o['line-break']
129                 : '');
130         if ($type === 'js') {
131             foreach (array('nomunge', 'preserve-semi', 'disable-optimizations') as $opt) {
132                 $cmd .= $o[$opt] 
133                     ? " --{$opt}"
134                     : '';
135             }
136         }
137         return $cmd . ' ' . escapeshellarg($tmpFile);
138     }
139     
140     private static function _prepare()
141     {
142         if (! is_file(self::$jarFile)) {
143             throw new Exception('Minify_YUICompressor : $jarFile('.self::$jarFile.') is not a valid file.');
144         }
145         if (! is_readable(self::$jarFile)) {
146             throw new Exception('Minify_YUICompressor : $jarFile('.self::$jarFile.') is not readable.');
147         }
148         if (! is_dir(self::$tempDir)) {
149             throw new Exception('Minify_YUICompressor : $tempDir('.self::$tempDir.') is not a valid direcotry.');
150         }
151         if (! is_writable(self::$tempDir)) {
152             throw new Exception('Minify_YUICompressor : $tempDir('.self::$tempDir.') is not writable.');
153         }
154     }
155 }
156