Component variables set with `$this->page` no longer leech in to other component partials.

Fixes #852
This commit is contained in:
Samuel Georges 2015-05-16 09:29:27 +10:00
parent 4f2c11e9db
commit 8b3ab93786
5 changed files with 151 additions and 20 deletions

View File

@ -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.

View File

@ -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;
}
/**
* 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
*/

View File

@ -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;
}
/**

View File

@ -0,0 +1,130 @@
<?php namespace Cms\Classes;
use ArrayAccess;
use Cms\Classes\CodeBase;
/**
* The 'page' object when referenced in a component.
*
* @package october\cms
* @author Alexey Bobkov, Samuel Georges
*/
class ComponentPage implements ArrayAccess
{
/**
* @var \Cms\Classes\CodeBase The CMS object the component is bound to.
*/
public $cmsObject;
/**
* @var \Cms\Classes\controller Specifies the CMS controller
*/
public $controller;
/**
* @var array A list of variables to pass to the component.
*/
public $vars = [];
/**
* Creates the object instance.
* @param \Cms\Classes\CodeBase $cmsObject The bound CMS object.
*/
public function __construct(CodeBase $cmsObject)
{
$this->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});
}
}

View File

@ -952,7 +952,7 @@ class Controller
}
}
return $this->renderPartial($name.'::default', [], false);
return $this->renderPartial($name.'::default', $componentObj->getVars(), false);
}
//