mirror of
https://github.com/mrclay/minify.git
synced 2025-08-16 11:03:59 +02:00
added 'quiet' option. and now serve() and handleRequest() return an array of output content and headers.
This commit is contained in:
@@ -61,7 +61,7 @@ class Minify {
|
|||||||
*
|
*
|
||||||
* @param array $options options passed on to 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()) {
|
public static function serve($type, $spec = array(), $options = array()) {
|
||||||
$class = 'Minify_Controller_' . $type;
|
$class = 'Minify_Controller_' . $type;
|
||||||
@@ -69,10 +69,14 @@ class Minify {
|
|||||||
require_once "Minify/Controller/{$type}.php";
|
require_once "Minify/Controller/{$type}.php";
|
||||||
}
|
}
|
||||||
$ctrl = new $class($spec, $options);
|
$ctrl = new $class($spec, $options);
|
||||||
if (! self::handleRequest($ctrl)) {
|
$ret = self::handleRequest($ctrl);
|
||||||
header("HTTP/1.0 400 Bad Request");
|
if (false === $ret) {
|
||||||
exit('400 Bad Request');
|
if (! isset($ctrl->options['quiet']) || ! $ctrl->options['quiet']) {
|
||||||
|
header("HTTP/1.0 400 Bad Request");
|
||||||
|
exit('400 Bad Request');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -83,12 +87,13 @@ class Minify {
|
|||||||
*
|
*
|
||||||
* @param Minify_Controller $controller
|
* @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) {
|
public static function handleRequest($controller) {
|
||||||
if (! $controller->requestIsValid) {
|
if (! $controller->requestIsValid) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
self::$_controller = $controller;
|
self::$_controller = $controller;
|
||||||
self::_setOptions();
|
self::_setOptions();
|
||||||
|
|
||||||
@@ -105,8 +110,14 @@ class Minify {
|
|||||||
$cg = new HTTP_ConditionalGet($cgOptions);
|
$cg = new HTTP_ConditionalGet($cgOptions);
|
||||||
if ($cg->cacheIsValid) {
|
if ($cg->cacheIsValid) {
|
||||||
// client's cache is valid
|
// client's cache is valid
|
||||||
$cg->sendHeaders();
|
if (self::$_options['quiet']) {
|
||||||
return true;
|
return array(
|
||||||
|
'content' => ''
|
||||||
|
,'headers' => $cg->getHeaders()
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
$cg->sendHeaders();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// client will need output
|
// client will need output
|
||||||
$headers = $cg->getHeaders();
|
$headers = $cg->getHeaders();
|
||||||
@@ -149,12 +160,17 @@ class Minify {
|
|||||||
$headers['Vary'] = 'Accept-Encoding';
|
$headers['Vary'] = 'Accept-Encoding';
|
||||||
}
|
}
|
||||||
|
|
||||||
// output headers & content
|
if (! self::$_options['quiet']) {
|
||||||
foreach ($headers as $name => $val) {
|
// output headers & content
|
||||||
header($name . ': ' . $val);
|
foreach ($headers as $name => $val) {
|
||||||
|
header($name . ': ' . $val);
|
||||||
|
}
|
||||||
|
echo $content;
|
||||||
}
|
}
|
||||||
echo $content;
|
return array(
|
||||||
return true;
|
'content' => $content
|
||||||
|
,'headers' => $headers
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -194,6 +210,7 @@ class Minify {
|
|||||||
,'perType' => array() // per-type minifier options
|
,'perType' => array() // per-type minifier options
|
||||||
,'contentTypeCharset' => null // leave out of Content-Type header
|
,'contentTypeCharset' => null // leave out of Content-Type header
|
||||||
,'setExpires' => null // send Expires header
|
,'setExpires' => null // send Expires header
|
||||||
|
,'quiet' => false
|
||||||
), $given);
|
), $given);
|
||||||
$defaultMinifiers = array(
|
$defaultMinifiers = array(
|
||||||
'text/css' => array('Minify_CSS', 'minify')
|
'text/css' => array('Minify_CSS', 'minify')
|
||||||
|
@@ -58,6 +58,9 @@ class Minify_Controller_Base {
|
|||||||
* an HTTP Expires header instead of checking for conditional GET.
|
* an HTTP Expires header instead of checking for conditional GET.
|
||||||
* E.g. (time() + 86400 * 365) for 1yr (default null)
|
* E.g. (time() + 86400 * 365) for 1yr (default null)
|
||||||
* This has nothing to do with server-side caching.
|
* 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();
|
public $options = array();
|
||||||
|
Reference in New Issue
Block a user