[Solar-svn] Revision 2793
pmjones at solarphp.com
pmjones at solarphp.com
Fri Sep 28 20:45:32 CDT 2007
branch: Solar_Filter:
* [ADD] Added method __destruct(). Turns out it's not enough to unset($filter). To release *all* memory, especially that consumed by the internal filter processor objects, you need to call $filter->__destruct() first, **then** unset($filter).
* [BRK] Method getData() now returns by copy, not by reference. Turns out the reference was not necessary, as the filter-chain copies the sanitized value back into the data array directly.
* [FIX] Method getData() now properly checks for key existence in Solar_Struct objects.
* [CHG] Method applyChain() now loops through the data elements, not the filter elements, as the outer loop.
Modified: branches/orm/Solar/Filter.php
===================================================================
--- branches/orm/Solar/Filter.php 2007-09-29 01:38:33 UTC (rev 2792)
+++ branches/orm/Solar/Filter.php 2007-09-29 01:45:32 UTC (rev 2793)
@@ -177,6 +177,29 @@
/**
*
+ * Call this method before you unset() this instance to fully recover
+ * memory from circular-referenced objects.
+ *
+ * @return void
+ *
+ */
+ public function __destruct()
+ {
+ // each filter object instance
+ foreach ($this->_filter as $key => $val) {
+ unset($this->_filter[$key]);
+ }
+
+ // certain to be an object
+ unset($this->_stack);
+
+ // might be objects
+ unset($this->_chain_locale_object);
+ unset($this->_data);
+ }
+
+ /**
+ *
* Magic call to filter methods represented as classes.
*
* @param string $method The filter method to call; e.g., 'sanitizeTrim'
@@ -436,28 +459,32 @@
/**
*
- * Gets a **reference** to the data array, or a specific element of data,
- * being processed by [[applyChain()]].
+ * Gets a copy of the data array, or a specific element of data, being
+ * processed by [[applyChain()]].
*
- * We use references because sanitizing methods in the filter chain will
- * need to modify the value directly, not a copy of the value.
- *
* @param string $key If empty, returns the whole data array; otherwise,
* returns just that key element of data.
*
- * @return &mixed A reference to the data array or element.
+ * @return mixed A copy of the data array or element.
*
* @see applyChain()
*
*/
- public function &getData($key = null)
+ public function getData($key = null)
{
if ($key === null) {
- $data =& $this->_data;
- } elseif (array_key_exists($key, $this->_data)) {
- $data =& $this->_data[$key];
+ return $this->_data;
}
- return $data;
+
+ if ($this->_data instanceof Solar_Struct && isset($this->_data[$key])) {
+ return $this->_data[$key];
+ }
+
+ if (array_key_exists($key, $this->_data)) {
+ return $this->_data[$key];
+ }
+
+ return null;
}
/**
@@ -539,9 +566,8 @@
}
}
- // loop through each filter set, where the key is the
- // data element name.
- foreach ($this->_chain_filters as $key => $filters) {
+ // loop through each data element
+ foreach ($this->_data as $key => $val) {
// keep the key name
$this->_data_key = $key;
@@ -553,6 +579,11 @@
continue;
}
+ // are there filters on this key?
+ if (empty($this->_chain_filters[$key])) {
+ continue;
+ }
+
// is this key required?
if (! empty($this->_chain_require[$key])) {
$this->setRequire(true);
@@ -561,13 +592,13 @@
}
// run the filters for the data element
- foreach ($filters as $params) {
+ foreach ($this->_chain_filters[$key] as $params) {
// take the method name off the top of the params ...
$method = array_shift($params);
// ... and put the value in its place.
- array_unshift($params, $this->_data[$key]);
+ array_unshift($params, $val);
// call the filtering method
$result = $this->__call($method, $params);
More information about the Solar-svn
mailing list