Removing the 3rd param from ObjectCollection::load() and adding a uniform setting of 'callbacks'. This setting is used to disable callbacks on objects by convention. Test cases updated.

This commit is contained in:
mark_story 2010-11-06 23:48:27 -04:00
parent 1ba28c246b
commit 88c717dbd8
6 changed files with 19 additions and 15 deletions

View file

@ -56,14 +56,15 @@ class ComponentCollection extends ObjectCollection {
/** /**
* Loads/constructs a component. Will return the instance in the registry if it already exists. * 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 string $component Component name to load
* @param array $settings Settings for the component. * @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. * @return Component A component object, Either the existing loaded component or a new one.
* @throws MissingComponentFileException, MissingComponentClassException when the component could not be found * @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); list($plugin, $name) = pluginSplit($component);
if (isset($this->_loaded[$name])) { if (isset($this->_loaded[$name])) {
return $this->_loaded[$name]; return $this->_loaded[$name];
@ -84,6 +85,7 @@ class ComponentCollection extends ObjectCollection {
} }
} }
$this->_loaded[$name] = new $componentClass($this, $settings); $this->_loaded[$name] = new $componentClass($this, $settings);
$enable = isset($settings['callbacks']) ? $settings['callbacks'] : true;
if ($enable === true) { if ($enable === true) {
$this->_enabled[] = $name; $this->_enabled[] = $name;
} }

View file

@ -81,15 +81,15 @@ class BehaviorCollection extends ObjectCollection {
} }
/** /**
* 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 string $behavior CamelCased name of the behavior to load
* @param array $config Behavior configuration parameters * @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 * @return boolean True on success, false on failure
* @throws MissingBehaviorFileException or MissingBehaviorClassException when a behavior could not be found. * @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); list($plugin, $name) = pluginSplit($behavior);
$class = $name . 'Behavior'; $class = $name . 'Behavior';
@ -150,7 +150,7 @@ class BehaviorCollection extends ObjectCollection {
} }
} }
$configDisabled = isset($config['enabled']) && $config['enabled'] === false; $configDisabled = isset($config['callbacks']) && $config['callbacks'] === false;
if (!in_array($name, $this->_enabled) && !$configDisabled) { if (!in_array($name, $this->_enabled) && !$configDisabled) {
$this->enable($name); $this->enable($name);
} elseif ($configDisabled) { } elseif ($configDisabled) {

View file

@ -38,12 +38,14 @@ abstract class ObjectCollection {
/** /**
* Loads a new object onto the collection. Can throw a variety of exceptions * 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 string $name Name of object to load.
* @param array $options Array of configuration options for the object to be constructed. * @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 * @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. * Trigger a callback method on every object in the collection.

View file

@ -44,11 +44,10 @@ class HelperCollection extends ObjectCollection {
* *
* @param string $helper Helper name to load * @param string $helper Helper name to load
* @param array $settings Settings for the helper. * @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. * @return Helper A helper object, Either the existing loaded helper or a new one.
* @throws MissingHelperFileException, MissingHelperClassException when the helper could not be found * @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); list($plugin, $name) = pluginSplit($helper, true);
if (isset($this->_loaded[$name])) { if (isset($this->_loaded[$name])) {
@ -75,7 +74,7 @@ class HelperCollection extends ObjectCollection {
foreach ($vars as $var) { foreach ($vars as $var) {
$this->_loaded[$name]->{$var} = $this->_View->{$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) { if ($enable === true) {
$this->_enabled[] = $name; $this->_enabled[] = $name;
} }

View file

@ -673,14 +673,15 @@ class View extends Object {
} }
/** /**
* 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 string $helperName Name of the helper to load.
* @param array $settings Settings for the helper * @param array $settings Settings for the helper
* @return Helper a constructed helper object. * @return Helper a constructed helper object.
* @see HelperCollection::load()
*/ */
public function loadHelper($helperName, $settings = array(), $attach = true) { public function loadHelper($helperName, $settings = array()) {
return $this->Helpers->load($helperName, $settings, $attach); return $this->Helpers->load($helperName, $settings);
} }
/** /**

View file

@ -65,7 +65,7 @@ class ComponentCollectionTest extends CakeTestCase {
* @return void * @return void
*/ */
function testLoadWithEnableFalse() { function testLoadWithEnableFalse() {
$result = $this->Components->load('Cookie', array(), false); $result = $this->Components->load('Cookie', array('callbacks' => false));
$this->assertType('CookieComponent', $result); $this->assertType('CookieComponent', $result);
$this->assertType('CookieComponent', $this->Components->Cookie); $this->assertType('CookieComponent', $this->Components->Cookie);