MDL-62801 themes: Remove old mustache caches when new one generated
authorMichael Hawkins <michaelh@moodle.com>
Wed, 27 Jun 2018 03:04:33 +0000 (11:04 +0800)
committerMichael Hawkins <michaelh@moodle.com>
Fri, 29 Jun 2018 02:49:21 +0000 (10:49 +0800)
lib/outputrenderers.php

index 5b50374..24330ac 100644 (file)
@@ -84,8 +84,20 @@ class renderer_base {
             $themename = $this->page->theme->name;
             $themerev = theme_get_revision();
 
+            // Create new localcache directory.
             $cachedir = make_localcache_directory("mustache/$themerev/$themename");
 
+            // Remove old localcache directories.
+            $mustachecachedirs = glob("{$CFG->localcachedir}/mustache/*", GLOB_ONLYDIR);
+            foreach ($mustachecachedirs as $localcachedir) {
+                $cachedrev = [];
+                preg_match("/\/mustache\/([0-9]+)$/", $localcachedir, $cachedrev);
+                $cachedrev = isset($cachedrev[1]) ? intval($cachedrev[1]) : 0;
+                if ($cachedrev > 0 && $cachedrev < $themerev) {
+                    fulldelete($localcachedir);
+                }
+            }
+
             $loader = new \core\output\mustache_filesystem_loader();
             $stringhelper = new \core\output\mustache_string_helper();
             $quotehelper = new \core\output\mustache_quote_helper();