Skip to content

Commit

Permalink
Simplifying logic and data structures used to store enabled-ness of o…
Browse files Browse the repository at this point in the history
…bjects in an object collection.
  • Loading branch information
markstory committed Aug 11, 2010
1 parent b225085 commit 409b129
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 49 deletions.
30 changes: 14 additions & 16 deletions cake/libs/model/behavior_collection.php
Expand Up @@ -144,12 +144,10 @@ public function load($behavior, $config = array(), $enable = true) {
}
}

if (!in_array($name, $this->_attached)) {
$this->_attached[] = $name;
}
if (in_array($name, $this->_disabled) && !(isset($config['enabled']) && $config['enabled'] === false)) {
$configDisabled = isset($config['enabled']) && $config['enabled'] === false;
if (!in_array($name, $this->_enabled) && !$configDisabled) {
$this->enable($name);
} elseif (isset($config['enabled']) && $config['enabled'] === false) {
} elseif ($configDisabled) {
$this->disable($name);
}
return true;
Expand All @@ -172,7 +170,7 @@ public function unload($name) {
unset($this->__methods[$m]);
}
}
$this->_attached = array_values(array_diff($this->_attached, (array)$name));
$this->_enabled = array_values(array_diff($this->_enabled, (array)$name));
}

/**
Expand Down Expand Up @@ -236,20 +234,20 @@ public function dispatchMethod(&$model, $method, $params = array(), $strict = fa
* @return mixed
*/
public function trigger(&$model, $callback, $params = array(), $options = array()) {
if (empty($this->_attached)) {
if (empty($this->_enabled)) {
return true;
}
$options = array_merge(array('break' => false, 'breakOn' => array(null, false), 'modParams' => false), $options);
$count = count($this->_attached);

for ($i = 0; $i < $count; $i++) {
$name = $this->_attached[$i];
if (in_array($name, $this->_disabled)) {
continue;
}
$options = array_merge(
array('break' => false, 'breakOn' => array(null, false), 'modParams' => false),
$options
);
foreach ($this->_enabled as $name) {
$result = $this->_loaded[$name]->dispatchMethod($model, $callback, $params);

if ($options['break'] && ($result === $options['breakOn'] || (is_array($options['breakOn']) && in_array($result, $options['breakOn'], true)))) {
if (
$options['break'] && ($result === $options['breakOn'] ||
(is_array($options['breakOn']) && in_array($result, $options['breakOn'], true)))
) {
return $result;
} elseif ($options['modParams'] && is_array($result)) {
$params[0] = $result;
Expand Down
46 changes: 19 additions & 27 deletions cake/libs/object_collection.php
Expand Up @@ -19,21 +19,14 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
abstract class ObjectCollection {
/**
* Lists the currently-attached objects
*
* @var array
* @access private
*/
protected $_attached = array();

/**
* Lists the currently-attached objects which are disabled
* List of the currently-enabled objects
*
* @var array
* @access private
* @access protected
*/
protected $_disabled = array();
protected $_enabled = array();

/**
* A hash of loaded helpers, indexed by the classname
Expand Down Expand Up @@ -78,19 +71,14 @@ abstract public function unload($name);
* @return Returns true.
*/
public function trigger($callback, $params = array(), $options = array()) {
if (empty($this->_attached)) {
if (empty($this->_enabled)) {
return true;
}
$options = array_merge(
array('break' => false, 'breakOn' => false),
$options
);
$count = count($this->_attached);
for ($i = 0; $i < $count; $i++) {
$name = $this->_attached[$i];
if (in_array($name, $this->_disabled)) {
continue;
}
foreach ($this->_enabled as $name) {
$result = call_user_func_array(array($this->{$name}, $callback), $params);

if (
Expand Down Expand Up @@ -127,13 +115,17 @@ public function __isset($name) {
}

/**
* Enables callbacks on a behavior or array of behaviors
* Enables callbacks on an object or array of objects
*
* @param mixed $name CamelCased name of the behavior(s) to enable (string or array)
* @param mixed $name CamelCased name of the object(s) to enable (string or array)
* @return void
*/
public function enable($name) {
$this->_disabled = array_diff($this->_disabled, (array)$name);
foreach ((array)$name as $object) {
if (isset($this->_loaded[$object]) && array_search($object, $this->_enabled) === false) {
$this->_enabled[] = $object;
}
}
}

/**
Expand All @@ -145,10 +137,10 @@ public function enable($name) {
*/
public function disable($name) {
foreach ((array)$name as $object) {
if (in_array($object, $this->_attached) && !in_array($object, $this->_disabled)) {
$this->_disabled[] = $object;
}
$index = array_search($object, $this->_enabled);
unset($this->_enabled[$index]);
}
$this->_enabled = array_values($this->_enabled);
}

/**
Expand All @@ -161,9 +153,9 @@ public function disable($name) {
*/
public function enabled($name = null) {
if (!empty($name)) {
return (in_array($name, $this->_attached) && !in_array($name, $this->_disabled));
return in_array($name, $this->_enabled);
}
return array_diff($this->_attached, $this->_disabled);
return $this->_enabled;
}

/**
Expand All @@ -176,9 +168,9 @@ public function enabled($name = null) {
*/
public function attached($name = null) {
if (!empty($name)) {
return (in_array($name, $this->_attached));
return isset($this->_loaded[$name]);
}
return $this->_attached;
return array_keys($this->_loaded);
}

/**
Expand Down
9 changes: 3 additions & 6 deletions cake/libs/view/helper_collection.php
Expand Up @@ -67,11 +67,8 @@ public function load($helper, $settings = array(), $enable = true) {
$this->_loaded[$name]->{$var} = $this->_View->{$var};
}

if (!in_array($name, $this->_attached)) {
$this->_attached[] = $name;
}
if ($enable === false) {
$this->_disabled[] = $name;
if ($enable === true) {
$this->_enabled[] = $name;
}
return $this->_loaded[$name];
}
Expand All @@ -85,7 +82,7 @@ public function load($helper, $settings = array(), $enable = true) {
public function unload($name) {
list($plugin, $name) = pluginSplit($name);
unset($this->_loaded[$name]);
$this->_attached = array_values(array_diff($this->_attached, (array)$name));
$this->_enabled = array_values(array_diff($this->_enabled, (array)$name));
}

}
Expand Down

0 comments on commit 409b129

Please sign in to comment.