From f46e321831899519b921297acb50f6a51ee71906 Mon Sep 17 00:00:00 2001 From: Steve Clay Date: Fri, 29 Feb 2008 23:59:50 +0000 Subject: [PATCH] added 'quiet' option. and now serve() and handleRequest() return an array of output content and headers. --- lib/Minify.php | 41 ++++++++++++++++++++++++---------- lib/Minify/Controller/Base.php | 3 +++ 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/lib/Minify.php b/lib/Minify.php index 63a80c8..a8aa859 100644 --- a/lib/Minify.php +++ b/lib/Minify.php @@ -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,10 +69,14 @@ class Minify { require_once "Minify/Controller/{$type}.php"; } $ctrl = new $class($spec, $options); - if (! self::handleRequest($ctrl)) { - header("HTTP/1.0 400 Bad Request"); - exit('400 Bad Request'); + $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; } /** @@ -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 - $cg->sendHeaders(); - return true; + if (self::$_options['quiet']) { + return array( + 'content' => '' + ,'headers' => $cg->getHeaders() + ); + } else { + $cg->sendHeaders(); + } } // client will need output $headers = $cg->getHeaders(); @@ -149,12 +160,17 @@ class Minify { $headers['Vary'] = 'Accept-Encoding'; } - // output headers & content - foreach ($headers as $name => $val) { - header($name . ': ' . $val); + if (! self::$_options['quiet']) { + // output headers & content + foreach ($headers as $name => $val) { + header($name . ': ' . $val); + } + echo $content; } - 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') diff --git a/lib/Minify/Controller/Base.php b/lib/Minify/Controller/Base.php index a6ce291..67d9b54 100644 --- a/lib/Minify/Controller/Base.php +++ b/lib/Minify/Controller/Base.php @@ -58,6 +58,9 @@ class Minify_Controller_Base { * an HTTP Expires header instead of checking for conditional GET. * 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();