From 88c717dbd8c886d31d9d2a4551e299f73fcbc4b6 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 6 Nov 2010 23:48:27 -0400 Subject: [PATCH] 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. --- cake/libs/controller/component_collection.php | 6 ++++-- cake/libs/model/behavior_collection.php | 8 ++++---- cake/libs/object_collection.php | 6 ++++-- cake/libs/view/helper_collection.php | 5 ++--- cake/libs/view/view.php | 7 ++++--- .../cases/libs/controller/component_collection.test.php | 2 +- 6 files changed, 19 insertions(+), 15 deletions(-) diff --git a/cake/libs/controller/component_collection.php b/cake/libs/controller/component_collection.php index 2f8e6b286..af27989b1 100644 --- a/cake/libs/controller/component_collection.php +++ b/cake/libs/controller/component_collection.php @@ -56,14 +56,15 @@ 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. * * @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]; @@ -84,6 +85,7 @@ class ComponentCollection extends ObjectCollection { } } $this->_loaded[$name] = new $componentClass($this, $settings); + $enable = isset($settings['callbacks']) ? $settings['callbacks'] : true; if ($enable === true) { $this->_enabled[] = $name; } diff --git a/cake/libs/model/behavior_collection.php b/cake/libs/model/behavior_collection.php index c339586ad..2df40a978 100644 --- a/cake/libs/model/behavior_collection.php +++ b/cake/libs/model/behavior_collection.php @@ -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 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'; @@ -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) { $this->enable($name); } elseif ($configDisabled) { diff --git a/cake/libs/object_collection.php b/cake/libs/object_collection.php index 26947641b..b2a6eb443 100644 --- a/cake/libs/object_collection.php +++ b/cake/libs/object_collection.php @@ -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. diff --git a/cake/libs/view/helper_collection.php b/cake/libs/view/helper_collection.php index 6640bb8a2..61567c811 100644 --- a/cake/libs/view/helper_collection.php +++ b/cake/libs/view/helper_collection.php @@ -44,11 +44,10 @@ class HelperCollection extends ObjectCollection { * * @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])) { @@ -75,7 +74,7 @@ class HelperCollection extends ObjectCollection { 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; } diff --git a/cake/libs/view/view.php b/cake/libs/view/view.php index 698b7fe4d..c3343af5f 100644 --- a/cake/libs/view/view.php +++ b/cake/libs/view/view.php @@ -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 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); } /** diff --git a/cake/tests/cases/libs/controller/component_collection.test.php b/cake/tests/cases/libs/controller/component_collection.test.php index cb0e83b83..4e38e6938 100644 --- a/cake/tests/cases/libs/controller/component_collection.test.php +++ b/cake/tests/cases/libs/controller/component_collection.test.php @@ -65,7 +65,7 @@ class ComponentCollectionTest extends CakeTestCase { * @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);