Skip to content

Commit

Permalink
Merge pull request #16 from inetis-ch/dev
Browse files Browse the repository at this point in the history
Fix a "Trying to access array offset on value of type null" error
  • Loading branch information
Fl0Cri authored Dec 17, 2020
2 parents d881195 + 3051fbf commit 930ecfb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
8 changes: 4 additions & 4 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}
Expand Down
22 changes: 14 additions & 8 deletions classes/SnippetLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
);
Expand All @@ -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 {
Expand Down Expand Up @@ -146,23 +150,25 @@ 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 : [];
}

/**
* 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());
}
Expand Down
2 changes: 2 additions & 0 deletions updates/version.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 930ecfb

Please sign in to comment.