]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Minify/extlib/minify/min/lib/Minify/YUICompressor.php
Added minify plugin
[quix0rs-gnu-social.git] / plugins / Minify / extlib / minify / 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.3.5.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  * @todo unit tests, $options docs
25  * 
26  * @package Minify
27  * @author Stephen Clay <steve@mrclay.org>
28  */
29 class Minify_YUICompressor {
30
31     /**
32      * Filepath of the YUI Compressor jar file. This must be set before
33      * calling minifyJs() or minifyCss().
34      *
35      * @var string
36      */
37     public static $jarFile = null;
38     
39     /**
40      * Writable temp directory. This must be set before calling minifyJs()
41      * or minifyCss().
42      *
43      * @var string
44      */
45     public static $tempDir = null;
46     
47     /**
48      * Filepath of "java" executable (may be needed if not in shell's PATH)
49      *
50      * @var string
51      */
52     public static $javaExecutable = 'java';
53     
54     /**
55      * Minify a Javascript string
56      * 
57      * @param string $js
58      * 
59      * @param array $options (verbose is ignored)
60      * 
61      * @see http://www.julienlecomte.net/yuicompressor/README
62      * 
63      * @return string 
64      */
65     public static function minifyJs($js, $options = array())
66     {
67         return self::_minify('js', $js, $options);
68     }
69     
70     /**
71      * Minify a CSS string
72      * 
73      * @param string $css
74      * 
75      * @param array $options (verbose is ignored)
76      * 
77      * @see http://www.julienlecomte.net/yuicompressor/README
78      * 
79      * @return string 
80      */
81     public static function minifyCss($css, $options = array())
82     {
83         return self::_minify('css', $css, $options);
84     }
85     
86     private static function _minify($type, $content, $options)
87     {
88         self::_prepare();
89         if (! ($tmpFile = tempnam(self::$tempDir, 'yuic_'))) {
90             throw new Exception('Minify_YUICompressor : could not create temp file.');
91         }
92         file_put_contents($tmpFile, $content);
93         exec(self::_getCmd($options, $type, $tmpFile), $output);
94         unlink($tmpFile);
95         return implode("\n", $output);
96     }
97     
98     private static function _getCmd($userOptions, $type, $tmpFile)
99     {
100         $o = array_merge(
101             array(
102                 'charset' => ''
103                 ,'line-break' => 5000
104                 ,'type' => $type
105                 ,'nomunge' => false
106                 ,'preserve-semi' => false
107                 ,'disable-optimizations' => false
108             )
109             ,$userOptions
110         );
111         $cmd = self::$javaExecutable . ' -jar ' . escapeshellarg(self::$jarFile)
112              . " --type {$type}"
113              . (preg_match('/^[a-zA-Z\\-]+$/', $o['charset']) 
114                 ? " --charset {$o['charset']}" 
115                 : '')
116              . (is_numeric($o['line-break']) && $o['line-break'] >= 0
117                 ? ' --line-break ' . (int)$o['line-break']
118                 : '');
119         if ($type === 'js') {
120             foreach (array('nomunge', 'preserve-semi', 'disable-optimizations') as $opt) {
121                 $cmd .= $o[$opt] 
122                     ? " --{$opt}"
123                     : '';
124             }
125         }
126         return $cmd . ' ' . escapeshellarg($tmpFile);
127     }
128     
129     private static function _prepare()
130     {
131         if (! is_file(self::$jarFile) 
132             || ! is_dir(self::$tempDir)
133             || ! is_writable(self::$tempDir)
134         ) {
135             throw new Exception('Minify_YUICompressor : $jarFile and $tempDir must be set.');
136         }
137     }
138 }
139