From 9fd881cb00d8ce865b462cc91c0cf902c6a12d1d Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 1 Jul 2010 23:00:48 -0400 Subject: [PATCH] Moving methods down and adding some tests. --- cake/libs/model/model_behavior.php | 54 ------------------ cake/libs/object_collection.php | 55 +++++++++++++++++++ cake/libs/view/helper_collection.php | 5 +- .../libs/view/helper_collection.test.php | 5 ++ 4 files changed, 64 insertions(+), 55 deletions(-) diff --git a/cake/libs/model/model_behavior.php b/cake/libs/model/model_behavior.php index fd46cb4fd..befd383a9 100644 --- a/cake/libs/model/model_behavior.php +++ b/cake/libs/model/model_behavior.php @@ -357,46 +357,6 @@ class BehaviorCollection extends ObjectCollection { $this->_attached = array_values(array_diff($this->_attached, (array)$name)); } -/** - * Enables callbacks on a behavior or array of behaviors - * - * @param mixed $name CamelCased name of the behavior(s) to enable (string or array) - * @return void - */ - public function enable($name) { - $this->_disabled = array_diff($this->_disabled, (array)$name); - } - -/** - * Disables callbacks on a behavior or array of behaviors. Public behavior methods are still - * callable as normal. - * - * @param mixed $name CamelCased name of the behavior(s) to disable (string or array) - * @return void - */ - public function disable($name) { - foreach ((array)$name as $behavior) { - if (in_array($behavior, $this->_attached) && !in_array($behavior, $this->_disabled)) { - $this->_disabled[] = $behavior; - } - } - } - -/** - * Gets the list of currently-enabled behaviors, or, the current status of a single behavior - * - * @param string $name Optional. The name of the behavior to check the status of. If omitted, - * returns an array of currently-enabled behaviors - * @return mixed If $name is specified, returns the boolean status of the corresponding behavior. - * Otherwise, returns an array of all enabled behaviors. - */ - public function enabled($name = null) { - if (!empty($name)) { - return (in_array($name, $this->_attached) && !in_array($name, $this->_disabled)); - } - return array_diff($this->_attached, $this->_disabled); - } - /** * Dispatches a behavior method * @@ -477,18 +437,4 @@ class BehaviorCollection extends ObjectCollection { return $this->__methods; } -/** - * Gets the list of attached behaviors, or, whether the given behavior is attached - * - * @param string $name Optional. The name of the behavior to check the status of. If omitted, - * returns an array of currently-attached behaviors - * @return mixed If $name is specified, returns the boolean status of the corresponding behavior. - * Otherwise, returns an array of all attached behaviors. - */ - public function attached($name = null) { - if (!empty($name)) { - return (in_array($name, $this->_attached)); - } - return $this->_attached; - } } diff --git a/cake/libs/object_collection.php b/cake/libs/object_collection.php index a385927ed..8fe93d168 100644 --- a/cake/libs/object_collection.php +++ b/cake/libs/object_collection.php @@ -35,4 +35,59 @@ abstract class ObjectCollection { */ protected $_disabled = array(); +/** + * Enables callbacks on a behavior or array of behaviors + * + * @param mixed $name CamelCased name of the behavior(s) to enable (string or array) + * @return void + */ + public function enable($name) { + $this->_disabled = array_diff($this->_disabled, (array)$name); + } + +/** + * Disables callbacks on a object or array of objects. Public object methods are still + * callable as normal. + * + * @param mixed $name CamelCased name of the objects(s) to disable (string or array) + * @return void + */ + public function disable($name) { + foreach ((array)$name as $object) { + if (in_array($object, $this->_attached) && !in_array($object, $this->_disabled)) { + $this->_disabled[] = $object; + } + } + } + +/** + * Gets the list of currently-enabled objects, or, the current status of a single objects + * + * @param string $name Optional. The name of the object to check the status of. If omitted, + * returns an array of currently-enabled object + * @return mixed If $name is specified, returns the boolean status of the corresponding object. + * Otherwise, returns an array of all enabled objects. + */ + public function enabled($name = null) { + if (!empty($name)) { + return (in_array($name, $this->_attached) && !in_array($name, $this->_disabled)); + } + return array_diff($this->_attached, $this->_disabled); + } + +/** + * Gets the list of attached behaviors, or, whether the given behavior is attached + * + * @param string $name Optional. The name of the behavior to check the status of. If omitted, + * returns an array of currently-attached behaviors + * @return mixed If $name is specified, returns the boolean status of the corresponding behavior. + * Otherwise, returns an array of all attached behaviors. + */ + public function attached($name = null) { + if (!empty($name)) { + return (in_array($name, $this->_attached)); + } + return $this->_attached; + } + } \ No newline at end of file diff --git a/cake/libs/view/helper_collection.php b/cake/libs/view/helper_collection.php index 0896a28e5..59663eb44 100644 --- a/cake/libs/view/helper_collection.php +++ b/cake/libs/view/helper_collection.php @@ -57,7 +57,10 @@ class HelperCollection extends ObjectCollection { if (!class_exists($helperClass)) { throw new MissingHelperClassException($helperClass); } - $this->{$name} = new $helperClass($this->_View, $settings); + } + $this->{$name} = new $helperClass($this->_View, $settings); + if (!in_array($name, $this->_attached)) { + $this->_attached[] = $name; } return $this->{$name}; } diff --git a/cake/tests/cases/libs/view/helper_collection.test.php b/cake/tests/cases/libs/view/helper_collection.test.php index 747da30c3..04ced0d34 100644 --- a/cake/tests/cases/libs/view/helper_collection.test.php +++ b/cake/tests/cases/libs/view/helper_collection.test.php @@ -50,6 +50,11 @@ class HelperCollectionTest extends CakeTestCase { $result = $this->Helpers->load('Html'); $this->assertType('HtmlHelper', $result); $this->assertType('HtmlHelper', $this->Helpers->Html); + + $result = $this->Helpers->attached(); + $this->assertEquals(array('Html'), $result, 'attached() results are wrong.'); + + $this->assertTrue($this->Helpers->enabled('Html')); } /**