From ffc7e6fb867924eea8e187f4e2ceae4d8049d32a Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 13 Mar 2020 12:43:44 -0400 Subject: [PATCH] Upgrade $pages->uncache() method so that it can also accept page IDs in addition to Page objects --- wire/core/Pages.php | 4 ++-- wire/core/PagesLoaderCache.php | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/wire/core/Pages.php b/wire/core/Pages.php index 4d119533..12f1b052 100644 --- a/wire/core/Pages.php +++ b/wire/core/Pages.php @@ -1004,7 +1004,7 @@ class Pages extends Wire { * * #pw-internal * - * @param Page|PageArray|null $page Page to uncache, or omit to uncache all. + * @param Page|PageArray|int|null $page Page to uncache, PageArray of pages to uncache, ID of page to uncache (3.0.153+), or omit to uncache all. * @param array $options Additional options to modify behavior: * - `shallow` (bool): By default, this method also calls $page->uncache(). To prevent that call, set this to true. * @return int Number of pages uncached @@ -1014,7 +1014,7 @@ class Pages extends Wire { $cnt = 0; if(is_null($page)) { $cnt = $this->cacher->uncacheAll(null, $options); - } else if($page instanceof Page) { + } else if($page instanceof Page || is_int($page)) { if($this->cacher->uncache($page, $options)) $cnt++; } else if($page instanceof PageArray) { foreach($page as $p) { diff --git a/wire/core/PagesLoaderCache.php b/wire/core/PagesLoaderCache.php index b1449bf7..b08f4de0 100644 --- a/wire/core/PagesLoaderCache.php +++ b/wire/core/PagesLoaderCache.php @@ -78,16 +78,24 @@ class PagesLoaderCache extends Wire { * * Note: does not remove pages from selectorCache. Call uncacheAll to do that. * - * @param Page $page Page to uncache + * @param Page|int $page Page to uncache or ID of page (prior to 3.0.153 only Page object was accepted) * @param array $options Additional options to modify behavior: * - `shallow` (bool): By default, this method also calls $page->uncache(). To prevent call to $page->uncache(), set 'shallow' => true. * @return bool True if page was uncached, false if it didn't need to be * */ - public function uncache(Page $page, array $options = array()) { - if(empty($options['shallow'])) $page->uncache(); - if(isset($this->pageIdCache[$page->id])) { - unset($this->pageIdCache[$page->id]); + public function uncache($page, array $options = array()) { + if($page instanceof Page) { + $pageId = $page->id; + } else { + $pageId = is_int($page) ? $page : (int) "$page"; + $page = isset($this->pageIdCache[$pageId]) ? $this->pageIdCache[$pageId] : null; + } + if(empty($options['shallow']) && $page) { + $page->uncache(); + } + if(isset($this->pageIdCache[$pageId])) { + unset($this->pageIdCache[$pageId]); return true; } else { return false;