Added Horde_SessionObjects::prune()
authorMichael M Slusarz <slusarz@curecanti.org>
Fri, 26 Jun 2009 07:34:21 +0000 (01:34 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Fri, 26 Jun 2009 07:46:08 +0000 (01:46 -0600)
framework/SessionObjects/lib/Horde/SessionObjects.php
framework/SessionObjects/package.xml

index 1213e7d..e9c6939 100644 (file)
@@ -101,17 +101,18 @@ class Horde_SessionObjects
         if (isset($_SESSION[$this->_name]['__prune']) &&
             ($_SESSION[$this->_name]['__prune'] > $this->_size)) {
             $pruneList = array();
+            $prune_count = $_SESSION[$this->_name]['__prune'] - $this->_size;
+
             foreach ($_SESSION[$this->_name] as $key => $val) {
                 if ($val['prune']) {
                     $pruneList[] = $key;
+                    if (!--$prune_count) {
+                        break;
+                    }
                 }
             }
 
-            $pruneOids = array_slice($pruneList, 0, $_SESSION[$this->_name]['__prune'] - $this->_size);
-            foreach ($pruneOids as $val) {
-                unset($_SESSION[$this->_name][$val]);
-            }
-            $_SESSION[$this->_name]['__prune'] -= count($pruneOids);
+            $this->prune($pruneList);
         }
     }
 
@@ -223,7 +224,8 @@ class Horde_SessionObjects
     }
 
     /**
-     * Sets the prune flag on a store object.
+     * Sets the prune flag on a store object.  The object will be pruned
+     * when the maximum storage size is reached.
      *
      * @param string $oid     The object ID.
      * @param boolean $prune  True to allow pruning, false for no pruning.
@@ -242,6 +244,33 @@ class Horde_SessionObjects
     }
 
     /**
+     * Immediately prune an object.
+     *
+     * @param mixed $oid  The object ID or an array of object IDs.
+     */
+    public function prune($oid)
+    {
+        if (!isset($_SESSION[$this->_name])) {
+            return;
+        }
+
+        if (!is_array($oid)) {
+            $oid = array($oid);
+        }
+
+        $prune_count = 0;
+
+        foreach ($oid as $val) {
+            if (isset($_SESSION[$this->_name][$val])) {
+                ++$prune_count;
+                unset($_SESSION[$this->_name][$val]);
+            }
+        }
+
+        $_SESSION[$this->_name]['__prune'] -= $prune_count;
+    }
+
+    /**
      * Generates an OID for an object.
      *
      * @param mixed $data  The data to store in the store.
index 48ca22c..008a4ec 100644 (file)
@@ -26,7 +26,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
   <api>beta</api>
  </stability>
  <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
- <notes>* Initial Horde 4 package</notes>
+ <notes>* Added prune() function.
+ * Initial Horde 4 package</notes>
  <contents>
   <dir name="/">
    <dir name="lib">