diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bf843ec7..da5295a1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ * **Build 25x** (2015-05-xx) - The `|page` filter now supports passing an empty string to generate a link to the current page. + - Component variables set with `$this->page` no longer leech in to other component partials. * **Build 258** (2015-05-09) - The hotkey for full screen mode is now Cmd+Shift+F or Ctrl+Shift+F in Windows. diff --git a/modules/cms/classes/CodeBase.php b/modules/cms/classes/CodeBase.php index 8744c057b..9b0e6bc5b 100644 --- a/modules/cms/classes/CodeBase.php +++ b/modules/cms/classes/CodeBase.php @@ -105,7 +105,7 @@ class CodeBase extends Extendable implements ArrayAccess */ public function __call($method, $parameters) { - if (method_exists($this, $method)) { + if ($this->methodExists($method)) { return call_user_func_array([$this, $method], $parameters); } @@ -113,28 +113,18 @@ class CodeBase extends Extendable implements ArrayAccess } /** - * This object is referenced as $this->page in Cms\Classes\ComponentBase, - * so to avoid $this->page->page this method will proxy there. This is also - * used as a helper for accessing controller variables/components easier - * in the page code, eg. $this->foo instead of $this['foo'] + * This is used as a helper for accessing controller variables/components + * easier in the page code, eg. $this->foo instead of $this['foo'] * @param string $name * @return void */ public function __get($name) { - if (($value = $this->page->{$name}) !== null) { - return $value; - } - - if (array_key_exists($name, $this->controller->vars)) { - return $this[$name]; - } - - return null; + return $this[$name]; } /** - * As per __get, this will set a variable instead. + * This will set a property on the CMS Page object. * @param string $name * @param mixed $value * @return void @@ -145,7 +135,7 @@ class CodeBase extends Extendable implements ArrayAccess } /** - * As per __get, this will check if a variable isset instead. + * This will check if a property isset on the CMS Page object. * @param string $name * @return void */ diff --git a/modules/cms/classes/ComponentBase.php b/modules/cms/classes/ComponentBase.php index 00d37e48c..16c47b4e4 100644 --- a/modules/cms/classes/ComponentBase.php +++ b/modules/cms/classes/ComponentBase.php @@ -6,6 +6,7 @@ use Event; use Config; use Cms\Classes\CodeBase; use Cms\Classes\CmsException; +use Cms\Classes\ComponentPage; use October\Rain\Extension\Extendable; /** @@ -69,7 +70,7 @@ abstract class ComponentBase extends Extendable protected $controller; /** - * @var Cms\Classes\PageCode Page object object. + * @var Cms\Classes\ComponentPage Page proxy object. */ protected $page; @@ -86,7 +87,7 @@ abstract class ComponentBase extends Extendable { if ($cmsObject !== null) { $this->controller = $cmsObject->controller; - $this->page = $cmsObject; + $this->page = new ComponentPage($cmsObject); } $this->properties = $this->validateProperties($properties); @@ -108,7 +109,16 @@ abstract class ComponentBase extends Extendable */ public function getPath() { - return plugins_path().$this->dirName; + return plugins_path() . $this->dirName; + } + + /** + * Get any variables set via $this->page, exclusively for this component. + * @return array + */ + public function getVars() + { + return $this->page->vars; } /** diff --git a/modules/cms/classes/ComponentPage.php b/modules/cms/classes/ComponentPage.php new file mode 100644 index 000000000..1e3eabde7 --- /dev/null +++ b/modules/cms/classes/ComponentPage.php @@ -0,0 +1,130 @@ +cmsObject = $cmsObject; + $this->controller = $cmsObject->controller; + } + + /** + * ArrayAccess implementation + */ + public function offsetSet($offset, $value) + { + $this->vars[$offset] = $value; + $this->controller->vars[$offset] = $value; + } + + /** + * ArrayAccess implementation + */ + public function offsetExists($offset) + { + return isset($this->vars[$offset]) || isset($this->controller->vars[$offset]); + } + + /** + * ArrayAccess implementation + */ + public function offsetUnset($offset) + { + unset($this->vars[$offset]); + unset($this->controller->vars[$offset]); + } + + /** + * ArrayAccess implementation + */ + public function offsetGet($offset) + { + if (isset($this->vars[$offset])) { + return $this->vars[$offset]; + } + + if (isset($this->controller->vars[$offset])) { + return $this->controller->vars[$offset]; + } + + return null; + } + + /** + * Dynamically handle calls into the controller instance. + * @param string $method + * @param array $parameters + * @return mixed + */ + public function __call($method, $parameters) + { + return call_user_func_array([$this->cmsObject, $method], $parameters); + } + + /** + * This object is referenced as $this->page in Cms\Classes\ComponentBase, + * so to avoid $this->page->page this method will proxy there. This is also + * used as a helper for accessing controller variables/components easier + * in the page code, eg. $this->foo instead of $this['foo'] + * @param string $name + * @return void + */ + public function __get($name) + { + if (($value = $this->cmsObject->page->{$name}) !== null) { + return $value; + } + + return $this[$name]; + } + + /** + * This will set a property as a proxy to Cms\Classes\CodeBase. + * @param string $name + * @param mixed $value + * @return void + */ + public function __set($name, $value) + { + return $this->cmsObject->{$name} = $value; + } + + /** + * This will check if a variable isset as a proxy to Cms\Classes\CodeBase. + * @param string $name + * @return void + */ + public function __isset($name) + { + return isset($this->cmsObject->{$name}); + } + +} \ No newline at end of file diff --git a/modules/cms/classes/Controller.php b/modules/cms/classes/Controller.php index 9041a5aa1..f7cb6e964 100644 --- a/modules/cms/classes/Controller.php +++ b/modules/cms/classes/Controller.php @@ -952,7 +952,7 @@ class Controller } } - return $this->renderPartial($name.'::default', [], false); + return $this->renderPartial($name.'::default', $componentObj->getVars(), false); } //