Roll back the changes from #852 in the name of KISS

The page vars are just that, view variables, if used incorrectly (relaxed) they can leech out
This commit is contained in:
Samuel Georges 2015-06-17 18:50:51 +10:00
parent d827ce686a
commit 8c6a58981e
6 changed files with 128 additions and 207 deletions

View File

@ -7,7 +7,6 @@
* **Build 260** (2015-05-16)
- 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

@ -155,14 +155,6 @@ class CmsCompoundObject extends CmsObject
return isset($this->settings[$key]);
}
/**
* Returns the Twig content string
*/
public function getTwigContent()
{
return $this->markup;
}
/**
* Runs components defined in the settings
* Process halts if a component returns a value
@ -430,26 +422,6 @@ class CmsCompoundObject extends CmsObject
Cache::forget($key);
}
/**
* Returns Twig node tree generated from the object's markup.
* This method is used by the system internally and shouldn't
* participate in the front-end request processing.
* @link http://twig.sensiolabs.org/doc/internals.html Twig internals
* @param mixed $markup Specifies the markup content.
* Use FALSE to load the content from the markup section.
* @return Twig_Node_Module A node tree
*/
public function getTwigNodeTree($markup = false)
{
$loader = new TwigLoader();
$twig = new Twig_Environment($loader, []);
$twig->addExtension(new CmsTwigExtension());
$twig->addExtension(new SystemTwigExtension);
$stream = $twig->tokenize($markup === false ? $this->markup : $markup, 'getTwigNodeTree');
return $twig->parse($stream);
}
/**
* Parses the settings array.
* Child classes can override this method in order to update
@ -532,4 +504,72 @@ class CmsCompoundObject extends CmsObject
{
return true;
}
//
// Twig
//
/**
* Returns the Twig content string
* @return string
*/
public function getTwigContent()
{
return $this->markup;
}
/**
* Returns Twig node tree generated from the object's markup.
* This method is used by the system internally and shouldn't
* participate in the front-end request processing.
* @link http://twig.sensiolabs.org/doc/internals.html Twig internals
* @param mixed $markup Specifies the markup content.
* Use FALSE to load the content from the markup section.
* @return Twig_Node_Module A node tree
*/
public function getTwigNodeTree($markup = false)
{
$loader = new TwigLoader();
$twig = new Twig_Environment($loader, []);
$twig->addExtension(new CmsTwigExtension());
$twig->addExtension(new SystemTwigExtension);
$stream = $twig->tokenize($markup === false ? $this->markup : $markup, 'getTwigNodeTree');
return $twig->parse($stream);
}
//
// Visibility
//
/**
* Get the visible attributes for the object.
* @return array
*/
public function getVisible()
{
return $this->visible;
}
/**
* Set the visible attributes for the object.
* @param array $visible
* @return void
*/
public function setVisible(array $visible)
{
$this->visible = $visible;
}
/**
* Add visible attributes for the object.
* @param array|string|null $attributes
* @return void
*/
public function addVisible($attributes = null)
{
$attributes = is_array($attributes) ? $attributes : func_get_args();
$this->visible = array_merge($this->visible, $attributes);
}
}

View File

@ -113,16 +113,26 @@ class CodeBase extends Extendable implements ArrayAccess
}
/**
* This is used as a helper for accessing controller variables/components
* easier in the page code, eg. $this->foo instead of $this['foo']
* 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->page->{$name}) !== null) {
return $value;
}
if (array_key_exists($name, $this->controller->vars)) {
return $this[$name];
}
return null;
}
/**
* This will set a property on the CMS Page object.
* @param string $name

View File

@ -6,7 +6,6 @@ use Event;
use Config;
use Cms\Classes\CodeBase;
use Cms\Classes\CmsException;
use Cms\Classes\ComponentPage;
use October\Rain\Extension\Extendable;
/**
@ -70,7 +69,7 @@ abstract class ComponentBase extends Extendable
protected $controller;
/**
* @var Cms\Classes\ComponentPage Page proxy object.
* @var Cms\Classes\PageCode Page object object.
*/
protected $page;
@ -86,8 +85,8 @@ abstract class ComponentBase extends Extendable
public function __construct(CodeBase $cmsObject = null, $properties = [])
{
if ($cmsObject !== null) {
$this->page = $cmsObject;
$this->controller = $cmsObject->controller;
$this->page = new ComponentPage($cmsObject);
}
$this->properties = $this->validateProperties($properties);
@ -112,15 +111,6 @@ abstract class ComponentBase extends Extendable
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;
}
/**
* Executed when this component is first initialized, before AJAX requests.
*/
@ -143,36 +133,6 @@ abstract class ComponentBase extends Extendable
{
}
/**
* Dynamically handle calls into the controller instance.
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
if (method_exists($this, $method)) {
return call_user_func_array([$this, $method], $parameters);
}
if (method_exists($this->controller, $method)) {
return call_user_func_array([$this->controller, $method], $parameters);
}
throw new CmsException(Lang::get('cms::lang.component.method_not_found', [
'name' => get_class($this),
'method' => $method
]));
}
/**
* Returns the component's alias, used by __SELF__
*/
public function __toString()
{
return $this->alias;
}
/**
* Renders a requested partial in context of this component,
* see Cms\Classes\Controller@renderPartial for usage.
@ -287,4 +247,38 @@ abstract class ComponentBase extends Extendable
return $default;
}
//
// Magic methods
//
/**
* Dynamically handle calls into the controller instance.
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
if (method_exists($this, $method)) {
return call_user_func_array([$this, $method], $parameters);
}
if (method_exists($this->controller, $method)) {
return call_user_func_array([$this->controller, $method], $parameters);
}
throw new CmsException(Lang::get('cms::lang.component.method_not_found', [
'name' => get_class($this),
'method' => $method
]));
}
/**
* Returns the component's alias, used by __SELF__
*/
public function __toString()
{
return $this->alias;
}
}

View File

@ -1,130 +0,0 @@
<?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

@ -945,17 +945,15 @@ class Controller
*/
public function renderComponent($name, $parameters = [])
{
$componentVars = [];
if ($componentObj = $this->findComponentByName($name)) {
$componentObj->id = uniqid($name);
$componentObj->setProperties(array_merge($componentObj->getProperties(), $parameters));
$componentVars = $componentObj->getVars();
if ($result = $componentObj->onRender()) {
return $result;
}
}
return $this->renderPartial($name.'::default', $componentVars, false);
return $this->renderPartial($name.'::default', [], false);
}
//
@ -1136,6 +1134,16 @@ class Controller
return $_url;
}
/**
* Converts supplied file to a URL relative to the media library.
* @param string $file Specifies the media-relative file
* @return string
*/
public function mediaUrl($file = null)
{
return MediaLibrary::url($file);
}
/**
* Returns a routing parameter.
* @param string Routing parameter name.