Skip to content

Commit

Permalink
Removing the enable parameter on HelperCollection, BehaviorCollection…
Browse files Browse the repository at this point in the history
…, 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 3216c90
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 30 deletions.
5 changes: 2 additions & 3 deletions cake/libs/controller/component.php
Expand Up @@ -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};
Expand Down
6 changes: 3 additions & 3 deletions cake/libs/controller/component_collection.php
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down
7 changes: 4 additions & 3 deletions cake/libs/model/behavior_collection.php
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 2 additions & 3 deletions cake/libs/view/helper.php
Expand Up @@ -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};
Expand Down
4 changes: 2 additions & 2 deletions cake/libs/view/helper_collection.php
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
19 changes: 3 additions & 16 deletions cake/tests/cases/libs/view/helper_collection.test.php
Expand Up @@ -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);

Expand Down

0 comments on commit 3216c90

Please sign in to comment.