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

added 'quiet' option. and now serve() and handleRequest() return an array of output content and headers.

This commit is contained in:
Steve Clay
2008-02-29 23:59:50 +00:00
parent 023066ad0a
commit f46e321831
2 changed files with 32 additions and 12 deletions

View File

@@ -61,7 +61,7 @@ class Minify {
*
* @param array $options options passed on to Minify
*
* @return mixed a Minify controller object
* @return mixed false on failure or array of content and headers sent
*/
public static function serve($type, $spec = array(), $options = array()) {
$class = 'Minify_Controller_' . $type;
@@ -69,11 +69,15 @@ class Minify {
require_once "Minify/Controller/{$type}.php";
}
$ctrl = new $class($spec, $options);
if (! self::handleRequest($ctrl)) {
$ret = self::handleRequest($ctrl);
if (false === $ret) {
if (! isset($ctrl->options['quiet']) || ! $ctrl->options['quiet']) {
header("HTTP/1.0 400 Bad Request");
exit('400 Bad Request');
}
}
return $ret;
}
/**
* Handle a request for a minified file.
@@ -83,12 +87,13 @@ class Minify {
*
* @param Minify_Controller $controller
*
* @return bool successfully sent a 304 or 200 with content
* @return mixed false on failure or array of content and headers sent
*/
public static function handleRequest($controller) {
if (! $controller->requestIsValid) {
return false;
}
self::$_controller = $controller;
self::_setOptions();
@@ -105,8 +110,14 @@ class Minify {
$cg = new HTTP_ConditionalGet($cgOptions);
if ($cg->cacheIsValid) {
// client's cache is valid
if (self::$_options['quiet']) {
return array(
'content' => ''
,'headers' => $cg->getHeaders()
);
} else {
$cg->sendHeaders();
return true;
}
}
// client will need output
$headers = $cg->getHeaders();
@@ -149,12 +160,17 @@ class Minify {
$headers['Vary'] = 'Accept-Encoding';
}
if (! self::$_options['quiet']) {
// output headers & content
foreach ($headers as $name => $val) {
header($name . ': ' . $val);
}
echo $content;
return true;
}
return array(
'content' => $content
,'headers' => $headers
);
}
/**
@@ -194,6 +210,7 @@ class Minify {
,'perType' => array() // per-type minifier options
,'contentTypeCharset' => null // leave out of Content-Type header
,'setExpires' => null // send Expires header
,'quiet' => false
), $given);
$defaultMinifiers = array(
'text/css' => array('Minify_CSS', 'minify')

View File

@@ -59,6 +59,9 @@ class Minify_Controller_Base {
* E.g. (time() + 86400 * 365) for 1yr (default null)
* This has nothing to do with server-side caching.
*
* 'quiet' : set to true to have Minify not output any content/headers
* (bool, default = false)
*
*/
public $options = array();