mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Simplifying logic and data structures used to store enabled-ness of objects in an object collection.
This commit is contained in:
parent
b2250852e4
commit
409b12954b
3 changed files with 36 additions and 49 deletions
|
@ -144,12 +144,10 @@ class BehaviorCollection extends ObjectCollection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!in_array($name, $this->_attached)) {
|
$configDisabled = isset($config['enabled']) && $config['enabled'] === false;
|
||||||
$this->_attached[] = $name;
|
if (!in_array($name, $this->_enabled) && !$configDisabled) {
|
||||||
}
|
|
||||||
if (in_array($name, $this->_disabled) && !(isset($config['enabled']) && $config['enabled'] === false)) {
|
|
||||||
$this->enable($name);
|
$this->enable($name);
|
||||||
} elseif (isset($config['enabled']) && $config['enabled'] === false) {
|
} elseif ($configDisabled) {
|
||||||
$this->disable($name);
|
$this->disable($name);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -172,7 +170,7 @@ class BehaviorCollection extends ObjectCollection {
|
||||||
unset($this->__methods[$m]);
|
unset($this->__methods[$m]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->_attached = array_values(array_diff($this->_attached, (array)$name));
|
$this->_enabled = array_values(array_diff($this->_enabled, (array)$name));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -236,20 +234,20 @@ class BehaviorCollection extends ObjectCollection {
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function trigger(&$model, $callback, $params = array(), $options = array()) {
|
public function trigger(&$model, $callback, $params = array(), $options = array()) {
|
||||||
if (empty($this->_attached)) {
|
if (empty($this->_enabled)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
$options = array_merge(array('break' => false, 'breakOn' => array(null, false), 'modParams' => false), $options);
|
$options = array_merge(
|
||||||
$count = count($this->_attached);
|
array('break' => false, 'breakOn' => array(null, false), 'modParams' => false),
|
||||||
|
$options
|
||||||
for ($i = 0; $i < $count; $i++) {
|
);
|
||||||
$name = $this->_attached[$i];
|
foreach ($this->_enabled as $name) {
|
||||||
if (in_array($name, $this->_disabled)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$result = $this->_loaded[$name]->dispatchMethod($model, $callback, $params);
|
$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;
|
return $result;
|
||||||
} elseif ($options['modParams'] && is_array($result)) {
|
} elseif ($options['modParams'] && is_array($result)) {
|
||||||
$params[0] = $result;
|
$params[0] = $result;
|
||||||
|
|
|
@ -19,21 +19,14 @@
|
||||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||||
*/
|
*/
|
||||||
abstract class ObjectCollection {
|
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
|
* @var array
|
||||||
* @access private
|
* @access protected
|
||||||
*/
|
*/
|
||||||
protected $_disabled = array();
|
protected $_enabled = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A hash of loaded helpers, indexed by the classname
|
* A hash of loaded helpers, indexed by the classname
|
||||||
|
@ -78,19 +71,14 @@ abstract class ObjectCollection {
|
||||||
* @return Returns true.
|
* @return Returns true.
|
||||||
*/
|
*/
|
||||||
public function trigger($callback, $params = array(), $options = array()) {
|
public function trigger($callback, $params = array(), $options = array()) {
|
||||||
if (empty($this->_attached)) {
|
if (empty($this->_enabled)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
$options = array_merge(
|
$options = array_merge(
|
||||||
array('break' => false, 'breakOn' => false),
|
array('break' => false, 'breakOn' => false),
|
||||||
$options
|
$options
|
||||||
);
|
);
|
||||||
$count = count($this->_attached);
|
foreach ($this->_enabled as $name) {
|
||||||
for ($i = 0; $i < $count; $i++) {
|
|
||||||
$name = $this->_attached[$i];
|
|
||||||
if (in_array($name, $this->_disabled)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$result = call_user_func_array(array($this->{$name}, $callback), $params);
|
$result = call_user_func_array(array($this->{$name}, $callback), $params);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
|
@ -127,13 +115,17 @@ abstract class ObjectCollection {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function enable($name) {
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -145,10 +137,10 @@ abstract class ObjectCollection {
|
||||||
*/
|
*/
|
||||||
public function disable($name) {
|
public function disable($name) {
|
||||||
foreach ((array)$name as $object) {
|
foreach ((array)$name as $object) {
|
||||||
if (in_array($object, $this->_attached) && !in_array($object, $this->_disabled)) {
|
$index = array_search($object, $this->_enabled);
|
||||||
$this->_disabled[] = $object;
|
unset($this->_enabled[$index]);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
$this->_enabled = array_values($this->_enabled);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -161,9 +153,9 @@ abstract class ObjectCollection {
|
||||||
*/
|
*/
|
||||||
public function enabled($name = null) {
|
public function enabled($name = null) {
|
||||||
if (!empty($name)) {
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -176,9 +168,9 @@ abstract class ObjectCollection {
|
||||||
*/
|
*/
|
||||||
public function attached($name = null) {
|
public function attached($name = null) {
|
||||||
if (!empty($name)) {
|
if (!empty($name)) {
|
||||||
return (in_array($name, $this->_attached));
|
return isset($this->_loaded[$name]);
|
||||||
}
|
}
|
||||||
return $this->_attached;
|
return array_keys($this->_loaded);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -67,11 +67,8 @@ class HelperCollection extends ObjectCollection {
|
||||||
$this->_loaded[$name]->{$var} = $this->_View->{$var};
|
$this->_loaded[$name]->{$var} = $this->_View->{$var};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!in_array($name, $this->_attached)) {
|
if ($enable === true) {
|
||||||
$this->_attached[] = $name;
|
$this->_enabled[] = $name;
|
||||||
}
|
|
||||||
if ($enable === false) {
|
|
||||||
$this->_disabled[] = $name;
|
|
||||||
}
|
}
|
||||||
return $this->_loaded[$name];
|
return $this->_loaded[$name];
|
||||||
}
|
}
|
||||||
|
@ -85,7 +82,7 @@ class HelperCollection extends ObjectCollection {
|
||||||
public function unload($name) {
|
public function unload($name) {
|
||||||
list($plugin, $name) = pluginSplit($name);
|
list($plugin, $name) = pluginSplit($name);
|
||||||
unset($this->_loaded[$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));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue