From dbba9349ab2119e53e6ee76a2c8f71c0c4991155 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Thu, 4 Jun 2015 19:16:35 +1000 Subject: [PATCH] Adds a method for defining which settings and viewBag properties should be visible as native ones --- modules/cms/classes/CmsCompoundObject.php | 30 ++++++++++++++++++++--- modules/cms/classes/Layout.php | 7 ++++++ modules/cms/classes/Page.php | 13 ++++++++++ modules/cms/classes/Partial.php | 7 ++++++ 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/modules/cms/classes/CmsCompoundObject.php b/modules/cms/classes/CmsCompoundObject.php index cb3460d39..b6ed95cda 100644 --- a/modules/cms/classes/CmsCompoundObject.php +++ b/modules/cms/classes/CmsCompoundObject.php @@ -56,10 +56,14 @@ class CmsCompoundObject extends CmsObject 'fileName' ]; + protected $settingsVisible = []; + protected $settingsValidationRules = []; protected $settingsValidationMessages = []; + protected $viewBagVisible = []; + protected $viewBagValidationRules = []; protected $viewBagValidationMessages = []; @@ -106,10 +110,22 @@ class CmsCompoundObject extends CmsObject */ public function __get($name) { - if (is_array($this->settings) && array_key_exists($name, $this->settings)) { + if ( + is_array($this->settings) && + array_key_exists($name, $this->settings) && + array_key_exists($name, array_flip($this->settingsVisible)) + ) { return $this->settings[$name]; } + if ( + is_array($this->viewBag) && + array_key_exists($name, $this->viewBag) && + array_key_exists($name, array_flip($this->viewBagVisible)) + ) { + return $this->viewBag[$name]; + } + return parent::__get($name); } @@ -125,6 +141,10 @@ class CmsCompoundObject extends CmsObject return true; } + if (isset($this->viewBag[$key]) === true) { + return true; + } + return isset($this->settings[$key]); } @@ -452,9 +472,10 @@ class CmsCompoundObject extends CmsObject */ protected function initFromCache($cached) { - $this->settings = $cached['settings']; - $this->code = $cached['code']; - $this->markup = $cached['markup']; + $this->viewBag = array_get($cached, 'viewBag', []); + $this->settings = array_get($cached, 'settings', []); + $this->code = array_get($cached, 'code'); + $this->markup = array_get($cached, 'markup'); } /** @@ -463,6 +484,7 @@ class CmsCompoundObject extends CmsObject */ protected function initCacheItem(&$item) { + $item['viewBag'] = $this->viewBag; $item['settings'] = $this->settings; $item['code'] = $this->code; $item['markup'] = $this->markup; diff --git a/modules/cms/classes/Layout.php b/modules/cms/classes/Layout.php index eeec4e785..ea9330c81 100644 --- a/modules/cms/classes/Layout.php +++ b/modules/cms/classes/Layout.php @@ -10,6 +10,13 @@ class Layout extends CmsCompoundObject { const FALLBACK_FILE_NAME = 'fallback'; + /** + * @var array These settings properties will be available as regular properties. + */ + protected $settingsVisible = [ + 'description' + ]; + protected function parseSettings() { } diff --git a/modules/cms/classes/Page.php b/modules/cms/classes/Page.php index a67335e5e..113899f49 100644 --- a/modules/cms/classes/Page.php +++ b/modules/cms/classes/Page.php @@ -18,6 +18,19 @@ class Page extends CmsCompoundObject */ public $apiBag = []; + /** + * @var array These settings properties will be available as regular properties. + */ + protected $settingsVisible = [ + 'title', + 'url', + 'layout', + 'description', + 'meta_title', + 'meta_description', + 'hidden' + ]; + protected $settingsValidationRules = [ 'title' => 'required', 'url' => ['required', 'regex:/^\/[a-z0-9\/\:_\-\*\[\]\+\?\|\.\^\$]*$/i'] diff --git a/modules/cms/classes/Partial.php b/modules/cms/classes/Partial.php index 690319e60..109a5b68b 100644 --- a/modules/cms/classes/Partial.php +++ b/modules/cms/classes/Partial.php @@ -8,6 +8,13 @@ */ class Partial extends CmsCompoundObject { + /** + * @var array These settings properties will be available as regular properties. + */ + protected $settingsVisible = [ + 'description' + ]; + /** * Returns the directory name corresponding to the object type. * For pages the directory name is "pages", for layouts - "layouts", etc.