From 853e8a916749fc3bc535603344630f37b3960465 Mon Sep 17 00:00:00 2001 From: Fl0Cri Date: Mon, 14 Dec 2020 18:38:10 +0100 Subject: [PATCH 1/2] Use CMS Page passed from calling event instead of asking the controller --- Plugin.php | 8 ++++---- classes/SnippetLoader.php | 22 ++++++++++++++-------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Plugin.php b/Plugin.php index 0852217..fdeb6d5 100644 --- a/Plugin.php +++ b/Plugin.php @@ -69,16 +69,16 @@ public function boot() }); // Register components from cache for AJAX handlers - Event::listen('cms.page.initComponents', function($controller, $page) { + Event::listen('cms.page.initComponents', function($controller, $page, $layout) { if (Input::ajax()) { - SnippetLoader::restoreComponentSnippetsFromCache($controller); + SnippetLoader::restoreComponentSnippetsFromCache($controller, $page); } }); // Save the snippets loaded in this page for use inside subsequent AJAX calls - Event::listen('cms.page.postprocess', function() { + Event::listen('cms.page.postprocess', function($controller, $url, $page, $dataHolder) { if (!Input::ajax()) { - SnippetLoader::saveCachedSnippets(); + SnippetLoader::saveCachedSnippets($page); } }); } diff --git a/classes/SnippetLoader.php b/classes/SnippetLoader.php index 2f21e37..86f6f2f 100644 --- a/classes/SnippetLoader.php +++ b/classes/SnippetLoader.php @@ -5,6 +5,7 @@ use Carbon\Carbon; use Cms\Classes\CmsException; use Cms\Classes\Controller as CmsController; +use Cms\Classes\Page as CmsPage; use Cms\Classes\Theme; use Cms\Classes\ComponentManager; use RainLab\Pages\Classes\SnippetManager; @@ -59,11 +60,13 @@ public static function registerPartialSnippet($snippetInfo) /** * Save to the cache the component snippets loaded for this page. * Should be called once after all snippets are loaded to avoid multiple serializations. + * + * @param CmsPage $page The CMS Page to which the cache should be attached */ - public static function saveCachedSnippets() + public static function saveCachedSnippets(CmsPage $page) { Cache::put( - self::getMapCacheKey(), + self::getMapCacheKey($page), serialize(self::$pageSnippetsCache), Carbon::now()->addDay() ); @@ -74,10 +77,11 @@ public static function saveCachedSnippets() * This make AJAX handlers of these components available. * * @param CmsController $cmsController + * @param CmsPage $page The CMS page for which to load the cache */ - public static function restoreComponentSnippetsFromCache($cmsController) + public static function restoreComponentSnippetsFromCache($cmsController, CmsPage $page) { - $componentSnippets = self::fetchCachedSnippets(); + $componentSnippets = self::fetchCachedSnippets($page); foreach ($componentSnippets as $componentInfo) { try { @@ -146,10 +150,12 @@ protected static function cacheSnippet($alias, $snippetInfo) /** * Get cached component snippets from the cache. + * + * @param CmsPage $page The CMS page for which to load the cache */ - protected static function fetchCachedSnippets() + protected static function fetchCachedSnippets(CmsPage $page) { - $cache = @unserialize(Cache::get(self::getMapCacheKey(), serialize([]))); + $cache = @unserialize(Cache::get(self::getMapCacheKey($page), serialize([]))); return is_array($cache) ? $cache : []; } @@ -157,12 +163,12 @@ protected static function fetchCachedSnippets() /** * Get a cache key for the current page and the current user. * + * @param CmsPage $page The CMS page for which to load the cache * @return string */ - protected static function getMapCacheKey() + protected static function getMapCacheKey(CmsPage $page) { $theme = Theme::getActiveTheme(); - $page = CmsController::getController()->getPage(); return 'dynamic-snippet-map-' . md5($theme->getPath() . $page['url'] . Session::getId()); } From 3051fbf6dc77b8e4fa031452a0b23e3ff407914b Mon Sep 17 00:00:00 2001 From: Fl0Cri Date: Thu, 17 Dec 2020 11:32:36 +0100 Subject: [PATCH 2/2] Version bump --- updates/version.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/updates/version.yaml b/updates/version.yaml index 9b9eac7..35a4b97 100644 --- a/updates/version.yaml +++ b/updates/version.yaml @@ -18,3 +18,5 @@ 2.1.4: - Fix duplicate stuff on RainLab.Pages (open page twice, add snippet twice, etc.) - '!!! This update fixes a compatibility issue with RainLab.Pages >= v1.3.5. You should make sure you are using the latest version of RainLab.Pages' +2.1.5: + - Fix error "Trying to access array offset on value of type null" that could happen under unknown circumstances