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.
This commit is contained in:
mark_story 2010-11-07 00:07:27 -04:00
parent 22497eb41c
commit 3216c902cd
6 changed files with 16 additions and 30 deletions

View file

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

View file

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

View file

@ -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) {

View file

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

View file

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

View file

@ -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);