Created
May 24, 2011 09:05
-
-
Save gergoerdosi/988391 to your computer and use it in GitHub Desktop.
Patch to fix KModelAbstract and KConfig deep cloning
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Index: code/libraries/koowa/config/config.php | |
=================================================================== | |
--- code/libraries/koowa/config/config.php (revision 3321) | |
+++ code/libraries/koowa/config/config.php (working copy) | |
@@ -10,7 +10,7 @@ | |
/** | |
* Config Class | |
- * | |
+ * | |
* KConfig provides a property based interface to an array | |
* | |
* @author Johan Janssens <johan@nooku.org> | |
@@ -25,24 +25,24 @@ | |
* @var array | |
*/ | |
protected $_data; | |
- | |
+ | |
/** | |
* Constructor. | |
* | |
* @param array|KConfig An associative array of configuration settings or a KConfig instance. | |
*/ | |
public function __construct( $config = array() ) | |
- { | |
+ { | |
if ($config instanceof KConfig) { | |
$config = clone $config; | |
} | |
- | |
+ | |
$this->_data = array(); | |
foreach ($config as $key => $value) { | |
$this->__set($key, $value); | |
} | |
- } | |
- | |
+ } | |
+ | |
/** | |
* Deep clone of this instance to ensure that nested KConfigs | |
* are also cloned. | |
@@ -52,23 +52,23 @@ | |
public function __clone() | |
{ | |
$array = array(); | |
- foreach ($this->_data as $key => $value) | |
+ foreach ($this->_data as $key => $value) | |
{ | |
- if ($value instanceof KConfig) { | |
+ if($value instanceof KConfig || $value instanceof stdClass) { | |
$array[$key] = clone $value; | |
} else { | |
$array[$key] = $value; | |
} | |
} | |
- | |
- $this->_data = $array; | |
+ | |
+ $this->_data = $array; | |
} | |
- | |
+ | |
/** | |
* Retrieve a configuration item and return $default if there is no element set. | |
* | |
- * @param string | |
- * @param mixed | |
+ * @param string | |
+ * @param mixed | |
* @return mixed | |
*/ | |
public function get($name, $default = null) | |
@@ -77,26 +77,26 @@ | |
if(isset($this->_data[$name])) { | |
$result = $this->_data[$name]; | |
} | |
- | |
+ | |
return $result; | |
} | |
/** | |
* Retrieve a configuration element | |
* | |
- * @param string | |
+ * @param string | |
* @return mixed | |
*/ | |
public function __get($name) | |
{ | |
return $this->get($name); | |
} | |
- | |
+ | |
/** | |
* Set a configuration element | |
* | |
- * @param string | |
- * @param mixed | |
+ * @param string | |
+ * @param mixed | |
* @return void | |
*/ | |
public function __set($name, $value) | |
@@ -107,11 +107,11 @@ | |
$this->_data[$name] = $value; | |
} | |
} | |
- | |
+ | |
/** | |
* Test existence of a configuration element | |
* | |
- * @param string | |
+ * @param string | |
* @return boolean | |
*/ | |
public function __isset($name) | |
@@ -122,20 +122,20 @@ | |
/** | |
* Unset a configuration element | |
* | |
- * @param string | |
+ * @param string | |
* @return void | |
*/ | |
public function __unset($name) | |
{ | |
unset($this->_data[$name]); | |
} | |
- | |
+ | |
/** | |
* Get a new iterator | |
- * | |
+ * | |
* @return ArrayIterator | |
*/ | |
- public function getIterator() | |
+ public function getIterator() | |
{ | |
return new ArrayIterator($this->_data); | |
} | |
@@ -151,7 +151,7 @@ | |
{ | |
return count($this->_data); | |
} | |
- | |
+ | |
/** | |
* Check if the offset exists | |
* | |
@@ -176,15 +176,15 @@ | |
public function offsetGet($offset) | |
{ | |
$result = null; | |
- if(isset($this->_data[$offset])) | |
- { | |
+ if(isset($this->_data[$offset])) | |
+ { | |
$result = $this->_data[$offset]; | |
if($result instanceof KConfig) { | |
$result = $result->toArray(); | |
} | |
- } | |
- | |
- return $result; | |
+ } | |
+ | |
+ return $result; | |
} | |
/** | |
@@ -226,9 +226,9 @@ | |
*/ | |
public function toArray() | |
{ | |
- $array = array(); | |
+ $array = array(); | |
$data = $this->_data; | |
- foreach ($data as $key => $value) | |
+ foreach ($data as $key => $value) | |
{ | |
if ($value instanceof KConfig) { | |
$array[$key] = $value->toArray(); | |
@@ -236,12 +236,12 @@ | |
$array[$key] = $value; | |
} | |
} | |
- | |
+ | |
return $array; | |
} | |
- | |
+ | |
/** | |
- * Return the data | |
+ * Return the data | |
* | |
* If the data being passed is an instance of KConfig the data will be transformed | |
* to an associative array. | |
@@ -252,10 +252,10 @@ | |
{ | |
return ($data instanceof KConfig) ? $data->toArray() : $data; | |
} | |
- | |
+ | |
/** | |
- * Append values | |
- * | |
+ * Append values | |
+ * | |
* This funciton only adds keys that don't exist and it filters out any duplicate values | |
* | |
* @param mixed A value of an or array of values to be appended | |
@@ -263,34 +263,34 @@ | |
*/ | |
public function append($config) | |
{ | |
- $config = KConfig::toData($config); | |
- | |
+ $config = KConfig::toData($config); | |
+ | |
if(is_array($config)) | |
{ | |
- if(!is_numeric(key($config))) | |
+ if(!is_numeric(key($config))) | |
{ | |
- foreach($config as $key => $value) | |
+ foreach($config as $key => $value) | |
{ | |
- if(array_key_exists($key, $this->_data)) | |
+ if(array_key_exists($key, $this->_data)) | |
{ | |
if(!empty($value) && ($this->_data[$key] instanceof KConfig)) { | |
$this->_data[$key] = $this->_data[$key]->append($value); | |
} | |
- } | |
+ } | |
else $this->__set($key, $value); | |
} | |
} | |
- else | |
+ else | |
{ | |
- foreach($config as $value) | |
- { | |
+ foreach($config as $value) | |
+ { | |
if (!in_array($value, $this->_data, true)) { | |
- $this->_data[] = $value; | |
+ $this->_data[] = $value; | |
} | |
- } | |
+ } | |
} | |
} | |
- | |
+ | |
return $this; | |
- } | |
+ } | |
} | |
\ No newline at end of file | |
Index: code/libraries/koowa/model/abstract.php | |
=================================================================== | |
--- code/libraries/koowa/model/abstract.php (revision 3321) | |
+++ code/libraries/koowa/model/abstract.php (working copy) | |
@@ -45,7 +45,7 @@ | |
* @var mixed | |
*/ | |
protected $_item; | |
- | |
+ | |
/** | |
* Model column data | |
* | |
@@ -155,7 +155,7 @@ | |
/** | |
* Reset all cached data and reset the model state to it's default | |
- * | |
+ * | |
* @param boolean If TRUE use defaults when resetting. Default is TRUE | |
* @return KModelAbstract | |
*/ | |
@@ -164,7 +164,7 @@ | |
unset($this->_list); | |
unset($this->_item); | |
unset($this->_total); | |
- | |
+ | |
$this->_state->reset($default); | |
return $this; | |
@@ -209,20 +209,20 @@ | |
{ | |
return $this->_total; | |
} | |
- | |
+ | |
/** | |
* Get the distinct values of a column | |
* | |
* @return object | |
*/ | |
public function getColumn($column) | |
- { | |
+ { | |
return $this->_column[$column]; | |
} | |
- | |
+ | |
/** | |
* Get the model data | |
- * | |
+ * | |
* If the model state is unique this function will call getItem(), otherwise | |
* it will calle getList(). | |
* | |
@@ -259,4 +259,14 @@ | |
return parent::__call($method, $args); | |
} | |
+ | |
+ /** | |
+ * Preform a deep clone of the object. | |
+ * | |
+ * @retun void | |
+ */ | |
+ public function __clone() | |
+ { | |
+ $this->_state = clone $this->_state; | |
+ } | |
} | |
\ No newline at end of file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment