diff --git a/README.md b/README.md index bc7711d..04cfa60 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ The example below will bust the homepage when the about page is updated. ``` "about-us": - - "page.md" + - "/" ``` You can also use a directory pattern. In this example, when a blog entry @@ -25,18 +25,35 @@ busted. ``` "blog/*": - - "blog/page.md" - - "page.md" + - "/blog" + - "/" ``` ## API -If you need to bust some cache within your own add-on, there's a -`bustCache` method for your busting pleasure. Pass in a filepath and -you're good to go! +Cache busting: ``` $this->addon->api('relative_cache_buster')->bustCache('/path/to/file.md'); ``` + +or + +``` +$this->addon->api('relative_cache_buster')->bustAllCaches(); +``` + +Cache regeneration: + +``` +$this->addon->api('relative_cache_buster')->regenerateACaches('/path/to/folder'); +``` + +or + +``` +$this->addon->api('relative_cache_buster')->regenerateAllCaches(); +``` + Courtesy of @edalzell. Thanks Erin! ## Feedback & Contribute diff --git a/relative_cache_buster/api.relative_cache_buster.php b/relative_cache_buster/api.relative_cache_buster.php index 6d7b7f4..0e9eb2c 100755 --- a/relative_cache_buster/api.relative_cache_buster.php +++ b/relative_cache_buster/api.relative_cache_buster.php @@ -2,14 +2,26 @@ class API_relative_cache_buster extends API { - public $meta = array( - 'name' => 'Relative Cache Buster', - 'version' => '1.1.0', - 'author' => 'Jamie Wagner', - 'author_url' => 'http://thisbythem.com' - ); - - public function bustCache($file) { - return $this->tasks->bustCache($file); - } + public $meta = array( + 'name' => 'Relative Cache Buster', + 'version' => '1.2.0', + 'author' => 'Jamie Wagner', + 'author_url' => 'http://thisbythem.com' + ); + + public function bustCache($file) { + return $this->tasks->bustCache($file); + } + + public function bustAllCaches() { + return $this->tasks->bustAllCaches(); + } + + public function regenerateCache($page) { + return $this->tasks->regenerateCache($page); + } + + public function regenerateAllCaches() { + return $this->tasks->regenerateAllCaches(); + } } diff --git a/relative_cache_buster/hooks.relative_cache_buster.php b/relative_cache_buster/hooks.relative_cache_buster.php index bf6951a..45c08eb 100644 --- a/relative_cache_buster/hooks.relative_cache_buster.php +++ b/relative_cache_buster/hooks.relative_cache_buster.php @@ -2,8 +2,16 @@ class Hooks_relative_cache_buster extends Hooks { - public function control_panel__publish($data) { - $this->tasks->bustCache($data['file']); - return $data; - } -} + public function control_panel__publish($data) { + $this->tasks->bustCache($data['file']); + return $data; + } + + public function relative_cache_buster__bust() { + $this->tasks->bustAllCaches(); + } + + public function relative_cache_buster__regen() { + $this->tasks->regenerateAllCaches(); + } +} \ No newline at end of file diff --git a/relative_cache_buster/tasks.relative_cache_buster.php b/relative_cache_buster/tasks.relative_cache_buster.php index ab3e35a..4fc3b4d 100644 --- a/relative_cache_buster/tasks.relative_cache_buster.php +++ b/relative_cache_buster/tasks.relative_cache_buster.php @@ -2,55 +2,78 @@ class Tasks_relative_cache_buster extends Tasks { - protected $file_updated; + protected $file_updated; - public function bustCache($file) { - if ($this->busterIsNeeded()) { - $this->file_updated = $file; - - foreach ($this->config as $url_pattern => $pages_to_bust) { - $actual_file_path = Path::resolve($url_pattern); - if ($this->shouldBustRelativePagesCache($actual_file_path)) { - $this->bustCacheForPages($pages_to_bust); - return true; + public function bustAllCaches() { + if ($this->busterIsNeeded()) { + foreach ($this->config as $url_pattern => $pages_to_bust) { + $actual_file_path = Path::resolve($url_pattern); + $this->bustCacheForPages($pages_to_bust); + } + return true; } - } + return false; } - return false; - } + public function bustCache($file) { + if ($this->busterIsNeeded()) { + $this->file_updated = $file; - private function busterIsNeeded() { - $html_caching_addon = Addon::getApi('html_caching'); - $html_cache_enabled = $html_caching_addon->fetchConfig('enable'); - $cache_length = $html_caching_addon->fetchConfig('cache_length'); - $valid_cache_lengths = array('on last modified', 'on cache update'); - $valid_cache_length = in_array($cache_length, $valid_cache_lengths); + foreach ($this->config as $url_pattern => $pages_to_bust) { + $actual_file_path = Path::resolve($url_pattern); + if ($this->shouldBustRelativePagesCache($actual_file_path)) { + $this->bustCacheForPages($pages_to_bust); + } + } + return true; + } - return $html_cache_enabled && $valid_cache_length; - } + return false; + } + + public function regenerateCache($page) { + $this->bustCacheForPages(array($page)); + file_get_contents(URL::makeFull($page)); + } - private function shouldBustRelativePagesCache($filepath) { - return (bool) $this->isUpdatedFile($filepath) || $this->isInDirectory($filepath); - } + public function regenerateAllCaches() { + $this->bustAllCaches(); + foreach ($this->config as $url_pattern => $pages) { + foreach ($pages => $page) { + file_get_contents(URL::makeFull($page)); + } + } + } - private function isUpdatedFile($actual_file_path) { - return (bool) strpos($this->file_updated, $actual_file_path); - } + private function busterIsNeeded() { + $html_caching_addon = Addon::getApi('html_caching'); + $html_cache_enabled = $html_caching_addon->fetchConfig('enable'); + $cache_length = $html_caching_addon->fetchConfig('cache_length'); + $valid_cache_lengths = array('on last modified', 'on cache update'); + $valid_cache_length = in_array($cache_length, $valid_cache_lengths); - private function isInDirectory($actual_file_path) { - return (bool) Folder::matchesPattern($this->file_updated, $actual_file_path); - } + return $html_cache_enabled && $valid_cache_length; + } - private function bustCacheForPages($pages) { - $content_root = Config::getContentRoot(); - $full_content_root = rtrim(Path::tidy(BASE_PATH . "/" . $content_root), "/"); + private function shouldBustRelativePagesCache($filepath) { + return (bool) $this->isUpdatedFile($filepath) || $this->isInDirectory($filepath); + } + + private function isUpdatedFile($actual_file_path) { + return (bool) strpos($this->file_updated, $actual_file_path); + } - foreach ($pages as $page) { - $page_full_path = $full_content_root . Path::resolve($page); - if (File::exists($page_full_path)) { - touch($page_full_path); - } + private function isInDirectory($actual_file_path) { + return (bool) Folder::matchesPattern($this->file_updated, $actual_file_path); + } + + private function bustCacheForPages($pages) { + // we delete the actual cache file because of this bug: https://lodge.statamic.com/public-house/1244-bug-in-rendered-html-code-somewhere + foreach ($pages as $page) { + $full_path = Path::assemble(BASE_PATH, '_cache', '_add-ons', 'html_caching', Helper::makeHash($page)); + if (File::exists($full_path)) { + File::delete($full_path); + } + } } - } }