includes/js-compressor.php
changeset 420 301f546688d1
parent 411 d1a95497b68f
child 507 586fd7d3202d
--- a/includes/js-compressor.php	Tue Feb 12 22:17:58 2008 -0500
+++ b/includes/js-compressor.php	Wed Feb 13 21:59:07 2008 -0500
@@ -107,7 +107,7 @@
 	 * public constructor
          * 	creates a new BaseConvert class variable (base 36)
 	 */
-	function JavaScriptCompressor() {
+	function __construct() {
 		$this->__SourceMap = new SourceMap();
 		$this->__BC = new BaseConvert('0123456789abcdefghijklmnopqrstuvwxyz');
 		$this->__delimeter = array(
@@ -493,4 +493,35 @@
         return (($next - 1) % 2 === 0);
     }
 }
+
+/**
+ * Wrapper for the JavaScriptCompressor class.
+ * @param string Javascript code to compact. If this doesn't contain any newline characters, will be treated as a filename.
+ * @param bool If true, aggressively compresses the code. Otherwise, just strips comments and some whitespace.
+ * @return string Compressed JS
+ */
+
+function perform_js_compress($text_or_file, $aggressive = false)
+{
+  static $compressor = false;
+  
+  if ( !is_object($compressor) )
+    $compressor = new JavaScriptCompressor();
+  
+  if ( strpos($text_or_file, "\n") )
+  {
+    $text =& $text_or_file;
+  }
+  else if ( file_exists($text_or_file) )
+  {
+    $text = file_get_contents($text_or_file);
+  }
+  else
+  {
+    $text =& $text_or_file;
+  }
+  
+  return ( $aggressive ) ? $compressor->getPacked($text) : $compressor->getClean($text);
+}
+
 ?>
\ No newline at end of file