From a714255c693d8c6e98e5dcdf5901e2f3c44441fa Mon Sep 17 00:00:00 2001 From: Steve Clay Date: Thu, 27 Mar 2008 18:51:55 +0000 Subject: [PATCH] + Minify/Build.php and example 2 showing use --- lib/HTTP/ConditionalGet/Build.php | 143 ------------------------------ lib/Minify/Build.php | 95 ++++++++++++++++++++ web/examples/1/index.php | 7 +- web/examples/2/_groupsSources.php | 11 +++ web/examples/2/index.php | 76 ++++++++++++++++ web/examples/2/m.php | 14 +++ web/test/test_Minify_Build.php | 35 ++++++++ web/test/test_all.php | 2 +- 8 files changed, 237 insertions(+), 146 deletions(-) delete mode 100644 lib/HTTP/ConditionalGet/Build.php create mode 100644 lib/Minify/Build.php create mode 100644 web/examples/2/_groupsSources.php create mode 100644 web/examples/2/index.php create mode 100644 web/examples/2/m.php create mode 100644 web/test/test_Minify_Build.php diff --git a/lib/HTTP/ConditionalGet/Build.php b/lib/HTTP/ConditionalGet/Build.php deleted file mode 100644 index 28af159..0000000 --- a/lib/HTTP/ConditionalGet/Build.php +++ /dev/null @@ -1,143 +0,0 @@ - - */ -class HTTP_ConditionalGet_Build { - - /** - * Last modification time of all files in the build - * - * @var int - */ - public $lastModified = 0; - - /** - * String to use as ampersand in uri(). Set this to '&' if - * you are not HTML-escaping URIs. - * - * @var string - */ - public static $ampersand = '&'; - - /** - * Get a time-stamped URI - * - * - * echo 'src="' . $b->uri('/site.js') . '"'; - * // outputs src="/site.js?1678242" - * - * echo 'src="' . $b->uri('/scriptaculous.js?load=effects') . '"'; - * // outputs src="/scriptaculous.js?load=effects&1678242" - * - * - * @param string $uri - * @return string - */ - public function uri($uri) { - $sep = strpos($uri, '?') === false - ? '?' - : self::$ampersand; - return "{$uri}{$sep}{$this->lastModified}"; - } - - /** - * Create a build object - * - * @param array $options - * - * 'id': (required) unique string for this build. - * - * 'savePath': PHP-writeable directory to store build info. If not - * specified, sys_get_temp_dir() will be used. - * - * 'scanPaths': (required) array of directory paths to scan. A single path - * string is also accepted. - * - * 'scanDeep': should we scan descendant directories? (default true) - * - * 'scanPattern': filenames must match this (default matches css & js - * files not starting with ".") - * - * 'delay': seconds to wait before scanning again (default 300) - * - * @return int last modified timestamp - */ - public function __construct($options) - { - $savePath = isset($options['savePath']) - ? $options['savePath'] - : sys_get_temp_dir(); - $file = $savePath . DIRECTORY_SEPARATOR - . 'HTTP_ConditionalGet_Build_' - . md5($options['id']) - . '.txt'; - - // check build file - $loaded = file_get_contents($file); - if ($loaded) { - list($this->lastModified, $nextCheck) = explode('|', $loaded); - } else { - $nextCheck = 0; - } - if ($nextCheck > $_SERVER['REQUEST_TIME']) { - // done here - return; - } - - // scan last modified times - $options['scanPattern'] = isset($options['scanPattern']) - ? $options['scanPattern'] - : '/^[^\\.].*\\.(?:css|js)$/'; - $options['scanDeep'] = isset($options['scanDeep']) - ? $options['scanDeep'] - : true; - $max = 0; - foreach ((array)$options['scanPaths'] as $path) { - $max = max($max, self::_scan($max, $path, $options)); - } - $this->lastModified = $max; - - $nextCheck = $_SERVER['REQUEST_TIME'] - + (isset($options['delay']) - ? (int)$options['delay'] - : 300); - - // save build file - file_put_contents($file, "{$this->lastModified}|{$nextCheck}"); - } - - protected static function _scan($max, $path, $options) - { - $d = dir($path); - while (false !== ($entry = $d->read())) { - if ('.' === $entry[0]) { - continue; - } - $fullPath = $path . DIRECTORY_SEPARATOR . $entry; - if (is_dir($entry)) { - if ($options['scanDeep']) { - $max = max($max, self::_scan($max, $fullPath, $options)); - } - } else { - if (preg_match($options['scanPattern'], $entry)) { - $max = max($max, filemtime($fullPath)); - } - } - } - $d->close(); - return $max; - } -} \ No newline at end of file diff --git a/lib/Minify/Build.php b/lib/Minify/Build.php new file mode 100644 index 0000000..ea30f0f --- /dev/null +++ b/lib/Minify/Build.php @@ -0,0 +1,95 @@ + + * // in config file + * $groupSources = array( + * 'js' => array('file1.js', 'file2.js') + * ,'css' => array('file1.css', 'file2.css', 'file3.css') + * ) + * + * // during HTML generation + * $jsBuild = new Minify_Build($groupSources['js']); + * $cssBuild = new Minify_Build($groupSources['css']); + * + * $script = ""; + * $link = ""; + * + * // in min.php + * Minify::serve('Groups', array( + * 'groups' => $groupSources + * )); + * + * + * @package Minify + * @author Stephen Clay + */ +class Minify_Build { + + /** + * Last modification time of all files in the build + * + * @var int + */ + public $lastModified = 0; + + /** + * String to use as ampersand in uri(). Set this to '&' if + * you are not HTML-escaping URIs. + * + * @var string + */ + public static $ampersand = '&'; + + /** + * Get a time-stamped URI + * + * + * echo $b->uri('/site.js'); + * // outputs "/site.js?1678242" + * + * echo $b->uri('/scriptaculous.js?load=effects'); + * // outputs "/scriptaculous.js?load=effects&1678242" + * + * + * @param string $uri + * @return string + */ + public function uri($uri) { + $sep = strpos($uri, '?') === false + ? '?' + : self::$ampersand; + return "{$uri}{$sep}{$this->lastModified}"; + } + + /** + * Create a build object + * + * @param array $sources array of Minify_Source objects and/or file paths + * + * @return null + */ + public function __construct($sources) + { + $max = 0; + foreach ((array)$sources as $source) { + if ($source instanceof Minify_Source) { + $max = max($max, $source->lastModified); + } elseif (is_string($source) && is_file($source)) { + $max = max($max, filemtime($source)); + } + } + $this->lastModified = $max; + } +} diff --git a/web/examples/1/index.php b/web/examples/1/index.php index ce7b3a5..804d7e5 100644 --- a/web/examples/1/index.php +++ b/web/examples/1/index.php @@ -20,13 +20,16 @@ ob_start();

Note: You should always enable caching using Minify::useServerCache(). For the examples this can be set in -config.php. Notice that minifying jquery.js takes several seconds!.

+config.php. Notice that minifying jQuery takes several seconds!.

Minify Example 1

This is an example of Minify serving a directory of single css/js files. -Each file is minified and sent with HTTP encoding (browser-permitting).

+Each file is minified and sent with HTTP encoding (browser-permitting).

+ +

In this example, if m.php detects $_GET['v'], a 30-day Expires header is +sent rather than the conditional GET.