From 6d423f9eed38321ab2733126664a19bdd6052777 Mon Sep 17 00:00:00 2001 From: yama Date: Fri, 20 Sep 2019 14:23:43 +0900 Subject: [PATCH 1/3] Add new config casset.hash_method --- classes/casset.php | 84 +++++++++++++++++++++++++++++++++++----------- config/casset.php | 1 + 2 files changed, 66 insertions(+), 19 deletions(-) diff --git a/classes/casset.php b/classes/casset.php index 2e9e808..50e902e 100644 --- a/classes/casset.php +++ b/classes/casset.php @@ -811,7 +811,6 @@ public static function render_js($group = false, $options = array(), $attr_dep = else $ret[] = $content; } - else { $filepath = static::$asset_url.static::process_filepath(static::$cache_path.$filename, 'js'); @@ -1152,24 +1151,7 @@ protected static function css_rewrite_uris($content, $filename, $destination_fil */ protected static function combine($type, $file_group, $minify, $inline) { - // Get the last modified time of all of the component files - $last_mod = 0; - foreach ($file_group as $file) - { - // If it's a remote file just assume it isn't modified, otherwise - // we're stuck making a ton of HTTP requests - if (strpos($file['file'], '//') !== false) - continue; - - $mod = filemtime(static::$root_path.$file['file']); - if ($mod > $last_mod) - $last_mod = $mod; - } - - $filename = md5(implode('', array_map(function($a) { - return $a['file']; - }, $file_group)).($minify ? 'min' : '').$last_mod).'.'.$type; - + $filename = static::get_file_name($type, $file_group, $minify); $rel_filepath = static::$cache_path.'/'.$filename; $abs_filepath = static::$root_path.$rel_filepath; $needs_update = (!file_exists($abs_filepath)); @@ -1216,6 +1198,70 @@ protected static function combine($type, $file_group, $minify, $inline) return $filename; } + private static function get_file_name($type, $file_group, $minify) + { + if(\Config::get('casset.hash_method')==='md5_file') + { + $hash = array(); + foreach ($file_group as $file) + { + if (strpos($file['file'], '//') !== false) + { + continue; + } + $hash[] = md5_file(static::$root_path.$file['file']); + } + return md5(implode('', array_map(function($a) { + return $a['file']; + }, $file_group)).($minify ? 'min' : '').implode(',', $hash)).'.'.$type; + } + + + // Get the last modified time of all of the component files + $last_mod = 0; + foreach ($file_group as $file) + { + // If it's a remote file just assume it isn't modified, otherwise + // we're stuck making a ton of HTTP requests + if (strpos($file['file'], '//') !== false) + { + continue; + } + + $mod = filemtime(static::$root_path.$file['file']); + if ($mod > $last_mod) + { + $last_mod = $mod; + } + } + return md5( + implode( + '' + , array_map( + function($a) { + return $a['file']; + } + , $file_group + ) + ) . ($minify ? 'min' : '') . $last_mod + ) . '.' . $type; + } + + private static function get_last_mod($file_group) + { + $last_mod = 0; + foreach ($file_group as $file) + { + if (strpos($file['file'], '//') !== false) { + continue; + } + $mod = filemtime(static::$root_path.$file['file']); + if ($mod > $last_mod) { + $last_mod = $mod; + } + } + return $last_mod; + } /** * Renders the javascript added through js_inline(). * diff --git a/config/casset.php b/config/casset.php index c7b88e1..2bf1302 100644 --- a/config/casset.php +++ b/config/casset.php @@ -246,6 +246,7 @@ */ 'groups' => array( ), + 'hash_method' => 'filemtime', // filemtime | md5_file ); /* End of file config/casset.php */ From f27d7e06e180030013be4d71b7e23f7f7b42e5a8 Mon Sep 17 00:00:00 2001 From: yama Date: Fri, 20 Sep 2019 14:25:38 +0900 Subject: [PATCH 2/3] Add new config casset.hash_method (fixed_hash) --- classes/casset.php | 34 ++++++++++++++++++++++++++++++++++ config/casset.php | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/classes/casset.php b/classes/casset.php index 50e902e..7b4ce7f 100644 --- a/classes/casset.php +++ b/classes/casset.php @@ -814,6 +814,10 @@ public static function render_js($group = false, $options = array(), $attr_dep = else { $filepath = static::$asset_url.static::process_filepath(static::$cache_path.$filename, 'js'); + if(\Config::get('casset.hash_method')==='fixed_hash') + { + $filepath .= '?' . static::get_last_mod($file_group); + } if ($options['gen_tags']) $ret .= html_tag('script', array('src' => $filepath,)+$attr, '').PHP_EOL; else @@ -837,6 +841,10 @@ public static function render_js($group = false, $options = array(), $attr_dep = $remote = (strpos($file['file'], '//') !== false); $base = ($remote) ? '' : static::$asset_url; $filepath = $base.static::process_filepath($file['file'], 'js', $remote); + if(\Config::get('casset.hash_method')==='fixed_hash') + { + $filepath .= '?' . filemtime(static::$root_path.$file['file']); + } if ($options['gen_tags']) $ret .= html_tag('script', array('src' => $filepath,)+$attr, '').PHP_EOL; else @@ -916,6 +924,10 @@ public static function render_css($group = false, $options = array(), $attr_dep else { $filepath = static::$asset_url.static::process_filepath(static::$cache_path.$filename, 'css'); + if(\Config::get('casset.hash_method')==='fixed_hash') + { + $filepath .= '?' . static::get_last_mod($file_group); + } if ($options['gen_tags']) $ret .= html_tag('link', array('rel' => 'stylesheet', 'href' => $filepath)+$attr).PHP_EOL; else @@ -939,6 +951,10 @@ public static function render_css($group = false, $options = array(), $attr_dep $remote = (strpos($file['file'], '//') !== false); $base = ($remote) ? '' : static::$asset_url; $filepath = $base.static::process_filepath($file['file'], 'css', $remote); + if(\Config::get('casset.hash_method')==='fixed_hash') + { + $filepath .= '?' . filemtime(static::$root_path.$file['file']); + } if ($options['gen_tags']) $ret .= html_tag('link', array('rel' => 'stylesheet', 'href' => $filepath)+$attr).PHP_EOL; else @@ -1216,6 +1232,24 @@ private static function get_file_name($type, $file_group, $minify) }, $file_group)).($minify ? 'min' : '').implode(',', $hash)).'.'.$type; } + if(\Config::get('casset.hash_method')==='fixed_hash') + { + $files = array(); + foreach ($file_group as $file) + { + if (strpos($file['file'], '//') !== false) + { + continue; + } + $files[] = $file['file']; + } + return md5( + implode( + ',' + , $files + ) . ($minify ? 'min' : '') + ) . '.' . $type; + } // Get the last modified time of all of the component files $last_mod = 0; diff --git a/config/casset.php b/config/casset.php index 2bf1302..9724d35 100644 --- a/config/casset.php +++ b/config/casset.php @@ -246,7 +246,7 @@ */ 'groups' => array( ), - 'hash_method' => 'filemtime', // filemtime | md5_file + 'hash_method' => 'filemtime', // filemtime | md5_file | fixed_hash(with datetimeQueryString) ); /* End of file config/casset.php */ From 79980b36c7db1e6dccf88bd88c345f5b15723967 Mon Sep 17 00:00:00 2001 From: yama Date: Fri, 20 Sep 2019 17:08:40 +0900 Subject: [PATCH 3/3] Fix - Config::get('casset.hash_method')==='fixed_hash' --- classes/casset.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/classes/casset.php b/classes/casset.php index 7b4ce7f..c9d5dec 100644 --- a/classes/casset.php +++ b/classes/casset.php @@ -1170,9 +1170,7 @@ protected static function combine($type, $file_group, $minify, $inline) $filename = static::get_file_name($type, $file_group, $minify); $rel_filepath = static::$cache_path.'/'.$filename; $abs_filepath = static::$root_path.$rel_filepath; - $needs_update = (!file_exists($abs_filepath)); - - if ($needs_update) + if (!is_file($abs_filepath) || \Config::get('casset.hash_method')==='fixed_hash') { $content = ''; foreach ($file_group as $file) @@ -1208,7 +1206,6 @@ protected static function combine($type, $file_group, $minify, $inline) } file_put_contents($abs_filepath, $content, LOCK_EX); - $mtime = time(); } return $filename;