1
0
mirror of https://github.com/mrclay/minify.git synced 2025-02-22 16:04:08 +01:00

MinApp.php + utils.php : Issue 86

This commit is contained in:
Steve Clay 2009-10-13 00:56:45 +00:00
parent 444335b28c
commit bd1df7734b
5 changed files with 78 additions and 32 deletions

View File

@ -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.:
<?php
// add /min/lib to your include_path first!
require $_SERVER['DOCUMENT_ROOT'] . '/min/utils.php';
$jsUri = Minify_groupUri('js');
$jsUri = Minify_groupUri('js1,js2');
echo "<script type='text/javascript' src='{$jsUri}'></script>";
@ -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

View File

@ -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' => ''
))
),//*/

View File

@ -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(

View File

@ -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];
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 686 B