From bd1df7734b88fa710709b7162f14317d6da949c7 Mon Sep 17 00:00:00 2001 From: Steve Clay Date: Tue, 13 Oct 2009 00:56:45 +0000 Subject: [PATCH] MinApp.php + utils.php : Issue 86 --- min/README.txt | 34 ++++++++++++---- min/groupsConfig.php | 3 +- min/lib/Minify/Controller/MinApp.php | 58 +++++++++++++++++---------- min/utils.php | 15 +++++-- min_extras/hydrogen_logo.gif | Bin 0 -> 686 bytes 5 files changed, 78 insertions(+), 32 deletions(-) create mode 100644 min_extras/hydrogen_logo.gif diff --git a/min/README.txt b/min/README.txt index a7cf774..ff1a7f1 100644 --- a/min/README.txt +++ b/min/README.txt @@ -66,11 +66,11 @@ to the /js and /themes/default directories, use: $min_serveOptions['minApp']['allowDirs'] = array('//js', '//themes/default'); -GROUPS: FASTER PERFORMANCE AND BETTER URLS +GROUPS: SHORTER URLS For the best performance, edit groupsConfig.php to pre-specify groups of files -to be combined under preset keys. E.g., here's an example configuration in -groupsConfig.php: +to be combined under preset keys (note: keys should not contain commas). E.g., +here's an example configuration in groupsConfig.php: return array( 'js' => array('//js/Class.js', '//js/email.js') @@ -99,6 +99,20 @@ return array( ); +COMBINING GROUPS AND FILES + +In a single URL you can combine multiple groups and files: + http://example.com/min/?g=js1,js2&f=page1.js + +Caveats: + * Groups always come before files in the output + E.g. /g=js1&f=page.js == /f=page.js&g=js1 + * Groups will be combined in the order specified. + E.g. /g=js1,js2 != /g=js2,js1 + * Minify will NOT keep you from trying to combine Javascript and CSS. + Combining the wrong files will cause an exception in JSMin or break CSS. + + FAR-FUTURE EXPIRES HEADERS Minify can send far-future (one year) Expires headers. To enable this you must @@ -106,14 +120,14 @@ add a number to the querystring (e.g. /min/?g=js&1234 or /min/f=file.js&1234) and alter it whenever a source file is changed. If you have a build process you can use a build/source control revision number. -If you serve files as a group, you can use the utility function Minify_groupUri() -to get a "versioned" Minify URI for use in your HTML. E.g.: +If you serve files as groups, you can use the utility function +Minify_groupUri() to get a "versioned" Minify URI for use in your HTML. E.g.: "; @@ -122,11 +136,17 @@ DEBUG MODE In debug mode, instead of compressing files, Minify sends combined files with comments prepended to each line to show the line number in the original source file. To enable this, set $min_allowDebugFlag to true in config.php and append -"&debug=1" to your URIs. E.g. /min/?f=script1.js,script2.js&debug=1 +"&debug=1" to your URIs. E.g. /min/?f=script1.js,script2.js&debug Known issue: files with comment-like strings/regexps can cause problems in this mode. +SEE ALSO + +http://code.google.com/p/minify/wiki/CookBook + + QUESTIONS? +http://code.google.com/p/minify/wiki/CommonProblems http://groups.google.com/group/minify \ No newline at end of file diff --git a/min/groupsConfig.php b/min/groupsConfig.php index d6e7de1..d85cc23 100644 --- a/min/groupsConfig.php +++ b/min/groupsConfig.php @@ -14,12 +14,13 @@ return array( // 'css' => array('//css/file1.css', '//css/file2.css'), // custom source example + // see http://code.google.com/p/minify/wiki/CustomSource /*'js2' => array( dirname(__FILE__) . '/../min_unit_tests/_test_files/js/before.js', // do NOT process this file new Minify_Source(array( 'filepath' => dirname(__FILE__) . '/../min_unit_tests/_test_files/js/before.js', - 'minifier' => create_function('$a', 'return $a;') + 'minifier' => '' )) ),//*/ diff --git a/min/lib/Minify/Controller/MinApp.php b/min/lib/Minify/Controller/MinApp.php index 5b1e499..36aa71b 100644 --- a/min/lib/Minify/Controller/MinApp.php +++ b/min/lib/Minify/Controller/MinApp.php @@ -14,10 +14,24 @@ class Minify_Controller_MinApp extends Minify_Controller_Base { /** * Set up groups of files as sources + * + * If $_GET['g'] is set, it's split with "," and each piece is looked up as + * a key in groupsConfig.php, and the found arrays are added to the sources. + * + * If $_GET['f'] is set and $options['groupsOnly'] is false, sources are + * added based on validity of name and location. + * + * Caveat: Groups always come before files, but the groups are ordered as + * requested. E.g. + * /f=file.js&g=js1,js2 == /g=js1,js2&f=file.js != /g=js2,js1&f=file.js + * + * Caveat: If JS group(s) are specified, the method will still allow you to + * add CSS files and vice-versa. E.g.: + * /g=js1,js2&f=site.css => Minify will try to process site.css through the + * Javascript minifier, possibly causing an exception. * * @param array $options controller and Minify options * @return array Minify options - * */ public function setupSources($options) { // filter controller options @@ -34,17 +48,25 @@ class Minify_Controller_MinApp extends Minify_Controller_Base { $sources = array(); if (isset($_GET['g'])) { // try groups - if (! isset($cOptions['groups'][$_GET['g']])) { - $this->log("A group configuration for \"{$_GET['g']}\" was not set"); + $files = array(); + $keys = explode(',', $_GET['g']); + // check for duplicate key + // note: no check for duplicate files + if ($keys != array_unique($keys)) { + $this->log("Duplicate key given in \"{$_GET['g']}\""); return $options; } - - $files = $cOptions['groups'][$_GET['g']]; - // if $files is a single object, casting will break it - if (is_object($files)) { - $files = array($files); - } elseif (! is_array($files)) { - $files = (array)$files; + foreach ($keys as $key) { + if (! isset($cOptions['groups'][$key])) { + $this->log("A group configuration for \"{$key}\" was not set"); + return $options; + } + $groupFiles = $cOptions['groups'][$key]; + if (is_array($groupFiles)) { + array_splice($files, count($files), 0, $groupFiles); + } else { + $files[] = $groupFiles; + } } foreach ($files as $file) { if ($file instanceof Minify_Source) { @@ -64,7 +86,8 @@ class Minify_Controller_MinApp extends Minify_Controller_Base { return $options; } } - } elseif (! $cOptions['groupsOnly'] && isset($_GET['f'])) { + } + if (! $cOptions['groupsOnly'] && isset($_GET['f'])) { // try user files // The following restrictions are to limit the URLs that minify will // respond to. Ideally there should be only one way to reference a file. @@ -102,23 +125,16 @@ class Minify_Controller_MinApp extends Minify_Controller_Base { } $allowDirs = array(); foreach ((array)$cOptions['allowDirs'] as $allowDir) { - $allowDir = str_replace('//', $_SERVER['DOCUMENT_ROOT'] . '/', $allowDir); - $realAllowDir = realpath($allowDir); - if (false === $realAllowDir) { - $this->log("AllowDir path '{$allowDir}' failed realpath()"); - } else { - $allowDirs[] = $realAllowDir; - } + $allowDirs[] = realpath(str_replace('//', $_SERVER['DOCUMENT_ROOT'] . '/', $allowDir)); } foreach ($files as $file) { $path = $_SERVER['DOCUMENT_ROOT'] . $base . $file; $file = realpath($path); if (false === $file) { - $this->log("Path '{$path}' failed realpath()"); + $this->log("Path \"{$path}\" failed realpath()"); return $options; } elseif (! parent::_fileIsSafe($file, $allowDirs)) { - $this->log("File '{$file}' was not found, or not located" - . " inside the 'allowDirs': " . var_export($allowDirs, 1)); + $this->log("Path \"{$path}\" failed Minify_Controller_Base::_fileIsSafe()"); return $options; } else { $sources[] = new Minify_Source(array( diff --git a/min/utils.php b/min/utils.php index 5428560..0d8ecf4 100644 --- a/min/utils.php +++ b/min/utils.php @@ -19,7 +19,7 @@ * If you do not want ampersands as HTML entities, set Minify_Build::$ampersand = "&" * before using this function. * - * @param string $group a key from groupsConfig.php + * @param string $group a key (or comma-separated keys) from groupsConfig.php * @param boolean $forceAmpersand (default false) Set to true if the RewriteRule * directives in .htaccess are functional. This will remove the "?" from URIs, making them * more cacheable by proxies. @@ -70,7 +70,7 @@ function Minify_groupsMtime($groups) } /** - * @param string $group a key from groupsConfig.php + * @param string $group a key (or comma-separated keys) from groupsConfig.php * @return Minify_Build * @private */ @@ -82,7 +82,16 @@ function _Minify_getBuild($group) $gc = (require dirname(__FILE__) . '/groupsConfig.php'); } if (! isset($builds[$group])) { - $builds[$group] = new Minify_Build($gc[$group]); + $sources = array(); + foreach (explode(',', $group) as $key) { + $val = $gc[$key]; + if (is_array($val)) { + array_splice($sources, count($sources), 0, $val); + } else { + $sources[] = $val; + } + } + $builds[$group] = new Minify_Build($sources); } return $builds[$group]; } diff --git a/min_extras/hydrogen_logo.gif b/min_extras/hydrogen_logo.gif new file mode 100644 index 0000000000000000000000000000000000000000..7b9e645cf8a942a7f9f5b8e772d8a7198982e729 GIT binary patch literal 686 zcmV;f0#W@(Nk%w1VKM+N0K@CcxnVH_ez`eb_ z*4EbE-rlyv*2t@?A^8LV2LS&7EC2ui05SkB000I5ASI4uDP9alAOL_67;~9)ZI>Jd zOxRX+k4LAn!At-c&TVPqAP_Pp>ZxQ}JB>-jTB=MM9j=W5*obKx0OMTBARcVm1CcQW zgSEgbep>}d0)YZG2pA#(1$;CZ2owQ-OBnM@ z0?(Ld(~?-rHlf=ZFiCW^0k*EfZ2HVnh-!nX-+EYUXeD9PM$*IJEHw4>l)_HDJ`V@A zk%8z5xHM)SzG)*T3DY!A9t^vZr^|_+Y5c-ymE(<&)dbU6xOCx?310QOvmuXmLVQaq z=pJE>Nq}wL4vHgZj>!b6CU{vfhD=!kW8Z*0!w+mj$rhf5*( zBp9916aT!QIBu{q${h9aLB>&hr14%W99U=(hZkVT=L8htwekr9*h#1Z4?X}P1QA9c Uks*E~pr8T^F2E3lA|U_(J7>fm+yDRo literal 0 HcmV?d00001