|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00029 class Minify_YUICompressor { 00030 00037 public static $jarFile = null; 00038 00045 public static $tempDir = null; 00046 00052 public static $javaExecutable = 'java'; 00053 00065 public static function minifyJs($js, $options = array()) 00066 { 00067 return self::_minify('js', $js, $options); 00068 } 00069 00081 public static function minifyCss($css, $options = array()) 00082 { 00083 return self::_minify('css', $css, $options); 00084 } 00085 00086 private static function _minify($type, $content, $options) 00087 { 00088 self::_prepare(); 00089 if (! ($tmpFile = tempnam(self::$tempDir, 'yuic_'))) { 00090 throw new Exception('Minify_YUICompressor : could not create temp file.'); 00091 } 00092 file_put_contents($tmpFile, $content); 00093 exec(self::_getCmd($options, $type, $tmpFile), $output); 00094 unlink($tmpFile); 00095 return implode("\n", $output); 00096 } 00097 00098 private static function _getCmd($userOptions, $type, $tmpFile) 00099 { 00100 $o = array_merge( 00101 array( 00102 'charset' => '' 00103 ,'line-break' => 5000 00104 ,'type' => $type 00105 ,'nomunge' => false 00106 ,'preserve-semi' => false 00107 ,'disable-optimizations' => false 00108 ) 00109 ,$userOptions 00110 ); 00111 $cmd = self::$javaExecutable . ' -jar ' . escapeshellarg(self::$jarFile) 00112 . " --type {$type}" 00113 . (preg_match('/^[a-zA-Z\\-]+$/', $o['charset']) 00114 ? " --charset {$o['charset']}" 00115 : '') 00116 . (is_numeric($o['line-break']) && $o['line-break'] >= 0 00117 ? ' --line-break ' . (int)$o['line-break'] 00118 : ''); 00119 if ($type === 'js') { 00120 foreach (array('nomunge', 'preserve-semi', 'disable-optimizations') as $opt) { 00121 $cmd .= $o[$opt] 00122 ? " --{$opt}" 00123 : ''; 00124 } 00125 } 00126 return $cmd . ' ' . escapeshellarg($tmpFile); 00127 } 00128 00129 private static function _prepare() 00130 { 00131 if (! is_file(self::$jarFile) 00132 || ! is_dir(self::$tempDir) 00133 || ! is_writable(self::$tempDir) 00134 ) { 00135 throw new Exception('Minify_YUICompressor : $jarFile and $tempDir must be set.'); 00136 } 00137 } 00138 } 00139