Use simple PHP-based javascript minification.
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 30 Jul 2009 19:58:08 +0000 (13:58 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 30 Jul 2009 20:08:36 +0000 (14:08 -0600)
In the future, possibly allow more powerful compressors (e.g. YUI
compressor). Or allow automatic compression of all files, and then piece
these compressed files into a single, servable file. Any method will be
better than having to manually compress the javascript files.

framework/Core/lib/Horde.php

index e8327a8..508c47b 100644 (file)
@@ -310,7 +310,7 @@ HTML;
         foreach ($s_list as $app => $files) {
             foreach ($files as $file) {
                 if ($file['d'] && ($file['f'][0] != '/') && empty($file['e'])) {
-                    $js_tocache[$file['p'] . $file['f']] = false;
+                    $js_tocache[] = $file['p'] . $file['f'];
                     $mtime[] = filemtime($file['p'] . $file['f']);
                 } elseif (!empty($file['e'])) {
                     $js_external[] = $file['u'];
@@ -342,22 +342,21 @@ HTML;
 
         if (!$exists) {
             $out = '';
-            foreach ($js_tocache as $key => $val) {
+            foreach ($js_tocache as $val) {
                 // Separate JS files with a newline since some compressors may
                 // strip trailing terminators.
-                if ($val) {
-                    // Minify these files a bit by removing newlines and
-                    // comments.
-                    $out .= preg_replace(array('/\n+/', '/\/\*.*?\*\//'), array('', ''), file_get_contents($key)) . "\n";
-                } else {
-                    $out .= file_get_contents($key) . "\n";
+                $js_text = file_get_contents($val);
+                try {
+                    $out .= Horde_Text_Filter::filter($js_text, 'JavascriptMinify') . "\n";
+                } catch (Horde_Exception $e) {
+                    $out .= $js_text . "\n";
                 }
             }
 
             switch ($cache_type) {
             case 'filesystem':
                 if (!file_put_contents($js_path, $out)) {
-                    throw new Horde_Exception('Could not write cached CSS file to disk.');
+                    throw new Horde_Exception('Could not write cached JS file to disk.');
                 }
                 break;