Moving methods down and adding some tests.

This commit is contained in:
mark_story 2010-07-01 23:00:48 -04:00
parent 6db3dbc680
commit 9fd881cb00
4 changed files with 64 additions and 55 deletions

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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};
}

View file

@ -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'));
}
/**