Skip to content
Permalink
Browse files

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.
  • Loading branch information...
markstory committed Nov 7, 2010
1 parent 22497eb commit 3216c902cd2bf679673404c12853fdda0f1a51fb
@@ -93,9 +93,8 @@ public function __construct(ComponentCollection $collection, $settings = array()
*/
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};
@@ -56,8 +56,8 @@ public function getController() {
/**
* 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 @@ public function load($component, $settings = array()) {
}
}
$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;
}
@@ -81,8 +81,9 @@ public function attach($behavior, $config = array()) {
}
/**
* 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 @@ public function load($behavior, $config = array()) {
}
}
$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) {
@@ -140,9 +140,8 @@ public function __call($method, $params) {
*/
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};
@@ -39,7 +39,7 @@ public function __construct(View $view) {
/**
* 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 @@ public function load($helper, $settings = array()) {
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;
}
@@ -58,25 +58,12 @@ function testLoad() {
}
/**
* 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);

0 comments on commit 3216c90

Please sign in to comment.
You can’t perform that action at this time.