From 8c6a58981e137c60720b0d0211daa3f5fc8819fa Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Wed, 17 Jun 2015 18:50:51 +1000 Subject: [PATCH] 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 --- CHANGELOG.md | 1 - modules/cms/classes/CmsCompoundObject.php | 96 +++++++++++----- modules/cms/classes/CodeBase.php | 16 ++- modules/cms/classes/ComponentBase.php | 78 ++++++------- modules/cms/classes/ComponentPage.php | 130 ---------------------- modules/cms/classes/Controller.php | 14 ++- 6 files changed, 128 insertions(+), 207 deletions(-) delete mode 100644 modules/cms/classes/ComponentPage.php diff --git a/CHANGELOG.md b/CHANGELOG.md index db47cff60..12551a41a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/modules/cms/classes/CmsCompoundObject.php b/modules/cms/classes/CmsCompoundObject.php index 6803dd8a1..cb19bb005 100644 --- a/modules/cms/classes/CmsCompoundObject.php +++ b/modules/cms/classes/CmsCompoundObject.php @@ -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); + } } diff --git a/modules/cms/classes/CodeBase.php b/modules/cms/classes/CodeBase.php index 9b0e6bc5b..a3013cf4a 100644 --- a/modules/cms/classes/CodeBase.php +++ b/modules/cms/classes/CodeBase.php @@ -113,14 +113,24 @@ 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) { - return $this[$name]; + if (($value = $this->page->{$name}) !== null) { + return $value; + } + + if (array_key_exists($name, $this->controller->vars)) { + return $this[$name]; + } + + return null; } /** diff --git a/modules/cms/classes/ComponentBase.php b/modules/cms/classes/ComponentBase.php index 16c47b4e4..c10230550 100644 --- a/modules/cms/classes/ComponentBase.php +++ b/modules/cms/classes/ComponentBase.php @@ -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; + } } diff --git a/modules/cms/classes/ComponentPage.php b/modules/cms/classes/ComponentPage.php deleted file mode 100644 index 1e3eabde7..000000000 --- a/modules/cms/classes/ComponentPage.php +++ /dev/null @@ -1,130 +0,0 @@ -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 5f30a194a..4f7e0e274 100644 --- a/modules/cms/classes/Controller.php +++ b/modules/cms/classes/Controller.php @@ -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.