mirror of
https://github.com/mrclay/minify.git
synced 2025-02-23 16:33:02 +01:00
MinApp.php + utils.php : Issue 86
This commit is contained in:
parent
444335b28c
commit
bd1df7734b
@ -66,11 +66,11 @@ to the /js and /themes/default directories, use:
|
|||||||
$min_serveOptions['minApp']['allowDirs'] = array('//js', '//themes/default');
|
$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
|
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
|
to be combined under preset keys (note: keys should not contain commas). E.g.,
|
||||||
groupsConfig.php:
|
here's an example configuration in groupsConfig.php:
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
'js' => array('//js/Class.js', '//js/email.js')
|
'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
|
FAR-FUTURE EXPIRES HEADERS
|
||||||
|
|
||||||
Minify can send far-future (one year) Expires headers. To enable this you must
|
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
|
and alter it whenever a source file is changed. If you have a build process you
|
||||||
can use a build/source control revision number.
|
can use a build/source control revision number.
|
||||||
|
|
||||||
If you serve files as a group, you can use the utility function Minify_groupUri()
|
If you serve files as groups, you can use the utility function
|
||||||
to get a "versioned" Minify URI for use in your HTML. E.g.:
|
Minify_groupUri() to get a "versioned" Minify URI for use in your HTML. E.g.:
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
// add /min/lib to your include_path first!
|
// add /min/lib to your include_path first!
|
||||||
require $_SERVER['DOCUMENT_ROOT'] . '/min/utils.php';
|
require $_SERVER['DOCUMENT_ROOT'] . '/min/utils.php';
|
||||||
|
|
||||||
$jsUri = Minify_groupUri('js');
|
$jsUri = Minify_groupUri('js1,js2');
|
||||||
echo "<script type='text/javascript' src='{$jsUri}'></script>";
|
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
|
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
|
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
|
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.
|
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?
|
QUESTIONS?
|
||||||
|
|
||||||
|
http://code.google.com/p/minify/wiki/CommonProblems
|
||||||
http://groups.google.com/group/minify
|
http://groups.google.com/group/minify
|
@ -14,12 +14,13 @@ return array(
|
|||||||
// 'css' => array('//css/file1.css', '//css/file2.css'),
|
// 'css' => array('//css/file1.css', '//css/file2.css'),
|
||||||
|
|
||||||
// custom source example
|
// custom source example
|
||||||
|
// see http://code.google.com/p/minify/wiki/CustomSource
|
||||||
/*'js2' => array(
|
/*'js2' => array(
|
||||||
dirname(__FILE__) . '/../min_unit_tests/_test_files/js/before.js',
|
dirname(__FILE__) . '/../min_unit_tests/_test_files/js/before.js',
|
||||||
// do NOT process this file
|
// do NOT process this file
|
||||||
new Minify_Source(array(
|
new Minify_Source(array(
|
||||||
'filepath' => dirname(__FILE__) . '/../min_unit_tests/_test_files/js/before.js',
|
'filepath' => dirname(__FILE__) . '/../min_unit_tests/_test_files/js/before.js',
|
||||||
'minifier' => create_function('$a', 'return $a;')
|
'minifier' => ''
|
||||||
))
|
))
|
||||||
),//*/
|
),//*/
|
||||||
|
|
||||||
|
@ -15,9 +15,23 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
|
|||||||
/**
|
/**
|
||||||
* Set up groups of files as sources
|
* 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
|
* @param array $options controller and Minify options
|
||||||
* @return array Minify options
|
* @return array Minify options
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public function setupSources($options) {
|
public function setupSources($options) {
|
||||||
// filter controller options
|
// filter controller options
|
||||||
@ -34,17 +48,25 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
|
|||||||
$sources = array();
|
$sources = array();
|
||||||
if (isset($_GET['g'])) {
|
if (isset($_GET['g'])) {
|
||||||
// try groups
|
// try groups
|
||||||
if (! isset($cOptions['groups'][$_GET['g']])) {
|
$files = array();
|
||||||
$this->log("A group configuration for \"{$_GET['g']}\" was not set");
|
$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;
|
return $options;
|
||||||
}
|
}
|
||||||
|
foreach ($keys as $key) {
|
||||||
$files = $cOptions['groups'][$_GET['g']];
|
if (! isset($cOptions['groups'][$key])) {
|
||||||
// if $files is a single object, casting will break it
|
$this->log("A group configuration for \"{$key}\" was not set");
|
||||||
if (is_object($files)) {
|
return $options;
|
||||||
$files = array($files);
|
}
|
||||||
} elseif (! is_array($files)) {
|
$groupFiles = $cOptions['groups'][$key];
|
||||||
$files = (array)$files;
|
if (is_array($groupFiles)) {
|
||||||
|
array_splice($files, count($files), 0, $groupFiles);
|
||||||
|
} else {
|
||||||
|
$files[] = $groupFiles;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
foreach ($files as $file) {
|
foreach ($files as $file) {
|
||||||
if ($file instanceof Minify_Source) {
|
if ($file instanceof Minify_Source) {
|
||||||
@ -64,7 +86,8 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
|
|||||||
return $options;
|
return $options;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} elseif (! $cOptions['groupsOnly'] && isset($_GET['f'])) {
|
}
|
||||||
|
if (! $cOptions['groupsOnly'] && isset($_GET['f'])) {
|
||||||
// try user files
|
// try user files
|
||||||
// The following restrictions are to limit the URLs that minify will
|
// The following restrictions are to limit the URLs that minify will
|
||||||
// respond to. Ideally there should be only one way to reference a file.
|
// 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();
|
$allowDirs = array();
|
||||||
foreach ((array)$cOptions['allowDirs'] as $allowDir) {
|
foreach ((array)$cOptions['allowDirs'] as $allowDir) {
|
||||||
$allowDir = str_replace('//', $_SERVER['DOCUMENT_ROOT'] . '/', $allowDir);
|
$allowDirs[] = realpath(str_replace('//', $_SERVER['DOCUMENT_ROOT'] . '/', $allowDir));
|
||||||
$realAllowDir = realpath($allowDir);
|
|
||||||
if (false === $realAllowDir) {
|
|
||||||
$this->log("AllowDir path '{$allowDir}' failed realpath()");
|
|
||||||
} else {
|
|
||||||
$allowDirs[] = $realAllowDir;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
foreach ($files as $file) {
|
foreach ($files as $file) {
|
||||||
$path = $_SERVER['DOCUMENT_ROOT'] . $base . $file;
|
$path = $_SERVER['DOCUMENT_ROOT'] . $base . $file;
|
||||||
$file = realpath($path);
|
$file = realpath($path);
|
||||||
if (false === $file) {
|
if (false === $file) {
|
||||||
$this->log("Path '{$path}' failed realpath()");
|
$this->log("Path \"{$path}\" failed realpath()");
|
||||||
return $options;
|
return $options;
|
||||||
} elseif (! parent::_fileIsSafe($file, $allowDirs)) {
|
} elseif (! parent::_fileIsSafe($file, $allowDirs)) {
|
||||||
$this->log("File '{$file}' was not found, or not located"
|
$this->log("Path \"{$path}\" failed Minify_Controller_Base::_fileIsSafe()");
|
||||||
. " inside the 'allowDirs': " . var_export($allowDirs, 1));
|
|
||||||
return $options;
|
return $options;
|
||||||
} else {
|
} else {
|
||||||
$sources[] = new Minify_Source(array(
|
$sources[] = new Minify_Source(array(
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
* If you do not want ampersands as HTML entities, set Minify_Build::$ampersand = "&"
|
* If you do not want ampersands as HTML entities, set Minify_Build::$ampersand = "&"
|
||||||
* before using this function.
|
* 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
|
* @param boolean $forceAmpersand (default false) Set to true if the RewriteRule
|
||||||
* directives in .htaccess are functional. This will remove the "?" from URIs, making them
|
* directives in .htaccess are functional. This will remove the "?" from URIs, making them
|
||||||
* more cacheable by proxies.
|
* 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
|
* @return Minify_Build
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
@ -82,7 +82,16 @@ function _Minify_getBuild($group)
|
|||||||
$gc = (require dirname(__FILE__) . '/groupsConfig.php');
|
$gc = (require dirname(__FILE__) . '/groupsConfig.php');
|
||||||
}
|
}
|
||||||
if (! isset($builds[$group])) {
|
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];
|
return $builds[$group];
|
||||||
}
|
}
|
||||||
|
BIN
min_extras/hydrogen_logo.gif
Normal file
BIN
min_extras/hydrogen_logo.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 686 B |
Loading…
x
Reference in New Issue
Block a user