From a714255c693d8c6e98e5dcdf5901e2f3c44441fa Mon Sep 17 00:00:00 2001
From: Steve Clay Note: You should always enable caching using
- * 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
+ * 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();
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!.
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.
Note: You should always enable caching using
+Minify::useServerCache()
. For the examples this can be set in
+config.php
. Notice that minifying jQuery takes several seconds!.
This is an example using Minify_Build and the Groups controller to +automatically create versioned minify URLs
+ +