From 3216c902cd2bf679673404c12853fdda0f1a51fb Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 7 Nov 2010 00:07:27 -0400 Subject: [PATCH] Removing the enable parameter on HelperCollection, BehaviorCollection, and ComponentCollection. They all now support the enabled option that Behaviors have historically supported. This provides a simpler API with fewer arguments, and allows callbacks to be disabled on objects in their declared arrays. Test cases updated. --- cake/libs/controller/component.php | 5 ++--- cake/libs/controller/component_collection.php | 6 +++--- cake/libs/model/behavior_collection.php | 7 ++++--- cake/libs/view/helper.php | 5 ++--- cake/libs/view/helper_collection.php | 4 ++-- .../libs/view/helper_collection.test.php | 19 +++---------------- 6 files changed, 16 insertions(+), 30 deletions(-) diff --git a/cake/libs/controller/component.php b/cake/libs/controller/component.php index c426fd8b7..c0d4b57a6 100644 --- a/cake/libs/controller/component.php +++ b/cake/libs/controller/component.php @@ -93,9 +93,8 @@ class Component extends Object { */ public function __get($name) { if (isset($this->_componentMap[$name]) && !isset($this->{$name})) { - $this->{$name} = $this->_Collection->load( - $this->_componentMap[$name]['class'], $this->_componentMap[$name]['settings'], false - ); + $settings = array_merge((array)$this->_componentMap[$name]['settings'], array('enabled' => false)); + $this->{$name} = $this->_Collection->load($this->_componentMap[$name]['class'], $settings); } if (isset($this->{$name})) { return $this->{$name}; diff --git a/cake/libs/controller/component_collection.php b/cake/libs/controller/component_collection.php index af27989b1..90ec17f17 100644 --- a/cake/libs/controller/component_collection.php +++ b/cake/libs/controller/component_collection.php @@ -56,8 +56,8 @@ class ComponentCollection extends ObjectCollection { /** * Loads/constructs a component. Will return the instance in the registry if it already exists. - * You can use `$settings['callbacks'] = false` to disable callbacks on a component when loading it. - * Callbacks default to on. + * You can use `$settings['enabled'] = false` to disable callbacks on a component when loading it. + * Callbacks default to on. Disabled component methods work as normal, only callbacks are disabled. * * @param string $component Component name to load * @param array $settings Settings for the component. @@ -85,7 +85,7 @@ class ComponentCollection extends ObjectCollection { } } $this->_loaded[$name] = new $componentClass($this, $settings); - $enable = isset($settings['callbacks']) ? $settings['callbacks'] : true; + $enable = isset($settings['enabled']) ? $settings['enabled'] : true; if ($enable === true) { $this->_enabled[] = $name; } diff --git a/cake/libs/model/behavior_collection.php b/cake/libs/model/behavior_collection.php index 2df40a978..9820d568b 100644 --- a/cake/libs/model/behavior_collection.php +++ b/cake/libs/model/behavior_collection.php @@ -81,8 +81,9 @@ class BehaviorCollection extends ObjectCollection { } /** - * Loads a behavior into the collection. You can use use `$config['callbacks'] = false` - * to load a behavior with callbacks disabled. By default callbacks are enabled. + * Loads a behavior into the collection. You can use use `$config['enabled'] = false` + * to load a behavior with callbacks disabled. By default callbacks are enabled. Disable behaviors + * can still be used as normal. * * @param string $behavior CamelCased name of the behavior to load * @param array $config Behavior configuration parameters @@ -150,7 +151,7 @@ class BehaviorCollection extends ObjectCollection { } } - $configDisabled = isset($config['callbacks']) && $config['callbacks'] === false; + $configDisabled = isset($config['enabled']) && $config['enabled'] === false; if (!in_array($name, $this->_enabled) && !$configDisabled) { $this->enable($name); } elseif ($configDisabled) { diff --git a/cake/libs/view/helper.php b/cake/libs/view/helper.php index 2b0be4091..68466a25f 100644 --- a/cake/libs/view/helper.php +++ b/cake/libs/view/helper.php @@ -140,9 +140,8 @@ class Helper extends Object { */ public function __get($name) { if (isset($this->_helperMap[$name]) && !isset($this->{$name})) { - $this->{$name} = $this->_View->loadHelper( - $this->_helperMap[$name]['class'], $this->_helperMap[$name]['settings'], false - ); + $settings = array_merge((array)$this->_helperMap[$name]['settings'], array('enabled' => false)); + $this->{$name} = $this->_View->loadHelper($this->_helperMap[$name]['class'], $settings); } if (isset($this->{$name})) { return $this->{$name}; diff --git a/cake/libs/view/helper_collection.php b/cake/libs/view/helper_collection.php index 61567c811..18b700812 100644 --- a/cake/libs/view/helper_collection.php +++ b/cake/libs/view/helper_collection.php @@ -39,7 +39,7 @@ class HelperCollection extends ObjectCollection { /** * Loads/constructs a helper. Will return the instance in the registry if it already exists. * By setting `$enable` to false you can disable callbacks for a helper. Alternatively you - * can set `$settings['callbacks'] = false` to disable callbacks. This alias is provided so that when + * can set `$settings['enabled'] = false` to disable callbacks. This alias is provided so that when * declaring $helpers arrays you can disable callbacks on helpers. * * @param string $helper Helper name to load @@ -74,7 +74,7 @@ class HelperCollection extends ObjectCollection { foreach ($vars as $var) { $this->_loaded[$name]->{$var} = $this->_View->{$var}; } - $enable = isset($settings['callbacks']) ? $settings['callbacks'] : false; + $enable = isset($settings['enabled']) ? $settings['enabled'] : true; if ($enable === true) { $this->_enabled[] = $name; } diff --git a/cake/tests/cases/libs/view/helper_collection.test.php b/cake/tests/cases/libs/view/helper_collection.test.php index 5d6dda543..8d60f0109 100644 --- a/cake/tests/cases/libs/view/helper_collection.test.php +++ b/cake/tests/cases/libs/view/helper_collection.test.php @@ -58,25 +58,12 @@ class HelperCollectionTest extends CakeTestCase { } /** - * test load and enable = false + * test that the enabled setting disables the helper. * * @return void */ - function testLoadWithEnableFalse() { - $result = $this->Helpers->load('Html', array(), false); - $this->assertType('HtmlHelper', $result); - $this->assertType('HtmlHelper', $this->Helpers->Html); - - $this->assertFalse($this->Helpers->enabled('Html'), 'Html should be disabled'); - } - -/** - * test that the callbacks setting disables the helper. - * - * @return void - */ - function testLoadWithCallbacksFalse() { - $result = $this->Helpers->load('Html', array('callbacks' => false)); + function testLoadWithEnabledFalse() { + $result = $this->Helpers->load('Html', array('enabled' => false)); $this->assertType('HtmlHelper', $result); $this->assertType('HtmlHelper', $this->Helpers->Html);