From bc869f8ec3f10dfc35b7568505ea4714f006f268 Mon Sep 17 00:00:00 2001 From: 3D-I <480857+3D-I@users.noreply.github.com> Date: Thu, 2 Jul 2020 23:01:11 +0200 Subject: [PATCH] [ticket/16543] Add script for automate it on builds PHPBB3-16543 --- build/patch_stylesheet_cache_busters.php | 54 ++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 build/patch_stylesheet_cache_busters.php diff --git a/build/patch_stylesheet_cache_busters.php b/build/patch_stylesheet_cache_busters.php new file mode 100644 index 0000000000..cc7db550f4 --- /dev/null +++ b/build/patch_stylesheet_cache_busters.php @@ -0,0 +1,54 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +if (php_sapi_name() !== 'cli') +{ + die("This program must be run from the command line.\n"); +} + +if (version_compare(PHP_VERSION, '7.1.3', '<')) +{ + die('patch_stylesheet_cache_busters.php requires at least PHP 7.1.3'); +} + +// Usage: "$ php build/patch_stylesheet_cache_busters.php" +$targets = [dirname(dirname(__FILE__)) . '/phpBB/styles/prosilver/theme/stylesheet.css']; + +array_map('patch_glob', $targets); + +function patch_glob($glob): void +{ + array_map('patch_file', glob($glob)); +} + +function patch_file(string $filepath): void +{ + $file = file_get_contents($filepath); + $old = $file; + $new = preg_replace_callback( + '(^@import\\s+url\\([\'"](?\\w++\\.css)\\?\\K(?:hash|v)=[^\'"]++)m', + function ($m) use ($filepath) + { + $path = dirname($filepath) . DIRECTORY_SEPARATOR . $m['basename']; + $hash = sprintf('%08x', crc32(file_get_contents($path))); + + return 'hash=' . $hash; + }, + $old + ); + + if ($new !== $old) + { + file_put_contents($filepath, $new); + } +}