Skip to content

Commit

Permalink
Removing the 3rd param from ObjectCollection::load() and adding a uni…
Browse files Browse the repository at this point in the history
…form setting of 'callbacks'. This setting is used to disable callbacks on objects by convention. Test cases updated.
  • Loading branch information
markstory committed Nov 7, 2010
1 parent 1ba28c2 commit 88c717d
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 15 deletions.
6 changes: 4 additions & 2 deletions cake/libs/controller/component_collection.php
Expand Up @@ -56,14 +56,15 @@ 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.
*
* @param string $component Component name to load
* @param array $settings Settings for the component.
* @param boolean $enable Whether or not this component should be enabled by default
* @return Component A component object, Either the existing loaded component or a new one.
* @throws MissingComponentFileException, MissingComponentClassException when the component could not be found
*/
public function load($component, $settings = array(), $enable = true) {
public function load($component, $settings = array()) {
list($plugin, $name) = pluginSplit($component);
if (isset($this->_loaded[$name])) {
return $this->_loaded[$name];
Expand All @@ -84,6 +85,7 @@ public function load($component, $settings = array(), $enable = true) {
}
}
$this->_loaded[$name] = new $componentClass($this, $settings);
$enable = isset($settings['callbacks']) ? $settings['callbacks'] : true;
if ($enable === true) {
$this->_enabled[] = $name;
}
Expand Down
8 changes: 4 additions & 4 deletions cake/libs/model/behavior_collection.php
Expand Up @@ -81,15 +81,15 @@ public function attach($behavior, $config = array()) {
}

/**
* Loads a behavior into the collection.
* 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.
*
* @param string $behavior CamelCased name of the behavior to load
* @param array $config Behavior configuration parameters
* @param boolean $enable Whether or not this helper should be enabled by default
* @return boolean True on success, false on failure
* @throws MissingBehaviorFileException or MissingBehaviorClassException when a behavior could not be found.
*/
public function load($behavior, $config = array(), $enable = true) {
public function load($behavior, $config = array()) {
list($plugin, $name) = pluginSplit($behavior);
$class = $name . 'Behavior';

Expand Down Expand Up @@ -150,7 +150,7 @@ public function load($behavior, $config = array(), $enable = true) {
}
}

$configDisabled = isset($config['enabled']) && $config['enabled'] === false;
$configDisabled = isset($config['callbacks']) && $config['callbacks'] === false;
if (!in_array($name, $this->_enabled) && !$configDisabled) {
$this->enable($name);
} elseif ($configDisabled) {
Expand Down
6 changes: 4 additions & 2 deletions cake/libs/object_collection.php
Expand Up @@ -38,12 +38,14 @@ abstract class ObjectCollection {
/**
* Loads a new object onto the collection. Can throw a variety of exceptions
*
* Implementations of this class support a `$options['callbacks']` flag which enables/disables
* a loaded object.
*
* @param string $name Name of object to load.
* @param array $options Array of configuration options for the object to be constructed.
* @param boolean $enable Whether or not this helper should be enabled by default
* @return object the constructed object
*/
abstract public function load($name, $options = array(), $enable = true);
abstract public function load($name, $options = array());

/**
* Trigger a callback method on every object in the collection.
Expand Down
5 changes: 2 additions & 3 deletions cake/libs/view/helper_collection.php
Expand Up @@ -44,11 +44,10 @@ public function __construct(View $view) {
*
* @param string $helper Helper name to load
* @param array $settings Settings for the helper.
* @param boolean $enable Whether or not this helper should be enabled by default
* @return Helper A helper object, Either the existing loaded helper or a new one.
* @throws MissingHelperFileException, MissingHelperClassException when the helper could not be found
*/
public function load($helper, $settings = array(), $enable = true) {
public function load($helper, $settings = array()) {
list($plugin, $name) = pluginSplit($helper, true);

if (isset($this->_loaded[$name])) {
Expand All @@ -75,7 +74,7 @@ public function load($helper, $settings = array(), $enable = true) {
foreach ($vars as $var) {
$this->_loaded[$name]->{$var} = $this->_View->{$var};
}
$enable = isset($settings['callbacks']) ? $settings['callbacks'] : $enable;
$enable = isset($settings['callbacks']) ? $settings['callbacks'] : false;
if ($enable === true) {
$this->_enabled[] = $name;
}
Expand Down
7 changes: 4 additions & 3 deletions cake/libs/view/view.php
Expand Up @@ -673,14 +673,15 @@ protected function _render($___viewFn, $___dataForView = array(), $loadHelpers =
}

/**
* Loads a helper. Delegates to the HelperCollection to load the helper
* Loads a helper. Delegates to the `HelperCollection::load()` to load the helper
*
* @param string $helperName Name of the helper to load.
* @param array $settings Settings for the helper
* @return Helper a constructed helper object.
* @see HelperCollection::load()
*/
public function loadHelper($helperName, $settings = array(), $attach = true) {
return $this->Helpers->load($helperName, $settings, $attach);
public function loadHelper($helperName, $settings = array()) {
return $this->Helpers->load($helperName, $settings);
}

/**
Expand Down
Expand Up @@ -65,7 +65,7 @@ function testLoad() {
* @return void
*/
function testLoadWithEnableFalse() {
$result = $this->Components->load('Cookie', array(), false);
$result = $this->Components->load('Cookie', array('callbacks' => false));
$this->assertType('CookieComponent', $result);
$this->assertType('CookieComponent', $this->Components->Cookie);

Expand Down

0 comments on commit 88c717d

Please sign in to comment.