Skip to content

Commit

Permalink
Moving methods down and adding some tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Aug 11, 2010
1 parent 6db3dbc commit 9fd881c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 55 deletions.
54 changes: 0 additions & 54 deletions cake/libs/model/model_behavior.php
Expand Up @@ -357,46 +357,6 @@ public function detach($name) {
$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
*
Expand Down Expand Up @@ -477,18 +437,4 @@ public function methods() {
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;
}
}
55 changes: 55 additions & 0 deletions cake/libs/object_collection.php
Expand Up @@ -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;
}

}
5 changes: 4 additions & 1 deletion cake/libs/view/helper_collection.php
Expand Up @@ -57,7 +57,10 @@ public function load($helper, $settings = array()) {
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};
}
Expand Down
5 changes: 5 additions & 0 deletions cake/tests/cases/libs/view/helper_collection.test.php
Expand Up @@ -50,6 +50,11 @@ function testLoad() {
$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'));
}

/**
Expand Down

0 comments on commit 9fd881c

Please sign in to comment.