From 92ba959863775e9f24e17fa3f3787a03a63c9db2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Wed, 6 Jun 2012 00:58:03 +0300 Subject: [PATCH] add file path checksum calculation. issue #28 --- min/lib/Minify/HTML/Helper.php | 36 ++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/min/lib/Minify/HTML/Helper.php b/min/lib/Minify/HTML/Helper.php index 807fdc9c..f199ac48 100644 --- a/min/lib/Minify/HTML/Helper.php +++ b/min/lib/Minify/HTML/Helper.php @@ -69,7 +69,7 @@ public function getRawUri($farExpires = true, $debug = false) if ($debug) { $path .= "&debug"; } elseif ($farExpires && $this->_lastModified) { - $path .= "&" . $this->_lastModified; + $path .= "&" . ($this->_lastModified + $this->_filePathChecksum); } return $path; } @@ -105,11 +105,39 @@ public function setGroup($key, $checkLastModified = true) $gc = (require $this->groupsConfigFile); if (isset($gc[$key])) { $this->_lastModified = self::getLastModified($gc[$key]); + $this->_filePathChecksum = self::getFilePathCheckSum($gc[$key]); } } } } - + + /** + * @param Minify_Source[] $sources + * @return float|null the checksum + */ + public static function getFilePathCheckSum($sources) + { + $paths = array(); + foreach ((array)$sources as $source) { + if (is_object($source) && isset($source->filepath)) { + $paths[] = $source->filepath; + } elseif (is_string($source)) { + if (0 === strpos($source, '//')) { + $source = $_SERVER['DOCUMENT_ROOT'] . substr($source, 1); + } + if (is_file($source)) { + $paths[] = $source; + } + } + } + + if (!empty($paths)) { + // cast to float so arithmetic would work on 32 and 64bit PHP + return (float )sprintf("%u", crc32(serialize($paths))); + } + return null; + } + public static function getLastModified($sources, $lastModified = 0) { $max = $lastModified; @@ -130,9 +158,9 @@ public static function getLastModified($sources, $lastModified = 0) protected $_groupKey = null; // if present, URI will be like g=... protected $_filePaths = array(); + protected $_filePathChecksum = null; protected $_lastModified = null; - /** * In a given array of strings, find the character they all have at * a particular index @@ -157,11 +185,11 @@ protected static function _getCommonCharAtPos($arr, $pos) { * * @param array $paths root-relative URIs of files * @param string $minRoot root-relative URI of the "min" application + * @return string */ protected static function _getShortestUri($paths, $minRoot = '/min/') { $pos = 0; $base = ''; - $c; while (true) { $c = self::_getCommonCharAtPos($paths, $pos); if ($c === '') {