From 5b8697021ad7601c99a2eac02955038bbdb04279 Mon Sep 17 00:00:00 2001
From: Doug Partridge <doug.partridge@coolthings.com.au>
Date: Wed, 16 May 2012 10:39:50 +1000
Subject: [PATCH] Add support for ypass.net's conversion of jsmin to a php extension

See http://www.ypass.net/blog/2009/06/really-fast-javascript-minify-in-php/

Removes the javascript_aggregator_jsminplus checkbox setting, and replaces it with a radios setting to select which minifier will be used
---
 javascript_aggregator.module |   49 ++++++++++++++++++++++++++++++-----------
 1 files changed, 36 insertions(+), 13 deletions(-)

diff --git a/javascript_aggregator.module b/javascript_aggregator.module
index 82ca171..b792525 100644
--- a/javascript_aggregator.module
+++ b/javascript_aggregator.module
@@ -34,12 +34,27 @@ function javascript_aggregator_form_alter(&$form, $form_state, $form_id) {
       '#default_value' => variable_get('javascript_aggregator_no_htaccess', FALSE),
       '#weight' => 5,
     );
-    $form['bandwidth_optimizations']['javascript_aggregator_jsminplus'] = array(
-      '#type' => 'checkbox',
-      '#title' => t('Use JSMin+ instead of JSMin'),
-      '#description' => t('Check this option to use <a href="@jsminplus">JSMin+</a> instead of JSMin.', array('@jsminplus' => 'http://crisp.tweakblogs.net/blog/1665/a-new-javascript-minifier-jsmin+.html')),
-      '#default_value' => variable_get('javascript_aggregator_jsminplus', FALSE),
-      '#weight' => 4,
+
+    if (variable_get('javascript_aggregator_jsminplus', false)) {
+      variable_set('javascript_aggregator_minifier', 'jsmin+');
+    }
+    variable_del('javascript_aggregator_jsminplus');
+
+
+    $jsmin_plus_link = l(t('here'), 'http://crisp.tweakblogs.net/blog/1665/a-new-javascript-minifier-jsmin+.html', array('attributes' => array('target' => '_')));
+    $jsmin_so_link = l(t('here'), 'http://www.ypass.net/blog/2009/06/really-fast-javascript-minify-in-php/', array('attributes' => array('target' => '_')));
+
+    $form['bandwidth_optimizations']['javascript_aggregator_minifier'] = array(
+      '#type' => 'radios',
+      '#title' => t('Use the following javascript minifier'),
+      '#options' => array(
+        'jsmin' => t('JSMin'),
+        'jsmin+' => t('JSMin+ (see !jsmin_plus_link)', array('!jsmin_plus_link' => $jsmin_plus_link)),
+        'jsmin.so' => t('jsmin.so (php extension, see !jsmin_so_link)', array('!jsmin_so_link' => $jsmin_so_link)),
+      ),
+      '#default_value' => variable_get('javascript_aggregator_minifier', 'jsmin'),
+      '#weight' => 2.5,
+
     );
   }
 }
@@ -112,24 +127,31 @@ function _javascript_aggregator_minify($scripts) {
       // Create the JSMinified file if it doesn't exist yet.
       if (!file_exists($jsmin_file_path)) {
 
-        if (variable_get('javascript_aggregator_jsminplus', FALSE)) {
+        $minifier = variable_get('javascript_aggregator_minifier', 'jsmin');
+        if ('jsmin' === $minifier) {
+          // JSMin the contents of the aggregated file.
+          require_once(drupal_get_path('module', 'javascript_aggregator') .'/jsmin.php');
+          $contents = JSMin::minify(file_get_contents(file_directory_path() . $aggregated_file_name));
+        }
+        elseif ('jsmin+' === $minifier) {
           // JSMin+ the contents of the aggregated file.
           require_once(drupal_get_path('module', 'javascript_aggregator') .'/jsminplus.php');
           // Strip Byte Order Marks (BOM's) from the file, JSMin+ cannot parse these.
           $file = str_replace(pack("CCC", 0xef, 0xbb, 0xbf), "", file_get_contents(file_directory_path() . $aggregated_file_name));
           $contents = JSMinPlus::minify($file);
         }
-        else {
-          // JSMin the contents of the aggregated file.
-          require_once(drupal_get_path('module', 'javascript_aggregator') .'/jsmin.php');
-          $contents = JSMin::minify(file_get_contents(file_directory_path() . $aggregated_file_name));
+        elseif ('jsmin.so' === $minifier) {
+          if (!extension_loaded('jsmin') && function_exists('dl')) {
+            dl('jsmin.so');
+          }
+          $contents = jsmin(file_get_contents(file_directory_path() . $aggregated_file_name));
         }
 
         // Code comments containing copyright notices and licensing information
         // are stripped when the file is minified. GPL and most other open
         // source licenses require the license text to be included whenever the
         // file is distributed, so include a reference to the un-minified file.
-        $contents = '// Minified using Javascript Aggregator - see '. $path_to_files_directory . $aggregated_file_name ." for original source including licensing information.\n". $contents;
+        $contents = '// Minified using Javascript Aggregator (' . $minifier . ') - see ' . $path_to_files_directory . $aggregated_file_name . " for original source including licensing information.\n". $contents;
 
         // GZip the JavaScript if required.
         $htaccess = file_directory_path() . '/js/.htaccess';
@@ -178,4 +200,5 @@ EOT;
   }
 
   return $scripts;
-}
\ No newline at end of file
+}
+
-- 
1.7.4.1

