From: Michael M Slusarz Date: Mon, 11 Oct 2010 03:59:02 +0000 (-0600) Subject: Allow ability to search session subkeys. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=4d2812f01520cc485dcac545c5f738b7d7b54025;p=horde.git Allow ability to search session subkeys. We store values with key & subkey together to save on storage/serialization costs. To retrieve array like output, simply need to call $session['app:name/'] - returns an array with subkeys as keys and session values as values. Ticket #8023 --- diff --git a/framework/Core/lib/Horde/Session.php b/framework/Core/lib/Horde/Session.php index ec26cddb6..c3e3468ef 100644 --- a/framework/Core/lib/Horde/Session.php +++ b/framework/Core/lib/Horde/Session.php @@ -148,6 +148,29 @@ class Horde_Session implements ArrayAccess $this->_cleansession = true; } + /** + * Return the list of subkeys for a master key. + * + * @param object $ob See _parseOffset(). + * + * @return array Subkeyname (keys) and session variable name (values). + */ + private function _subkeys($ob) + { + $ret = array(); + + if (isset($_SESSION[$ob->app]) && + ($ob->name[strlen($ob->name) - 1] == '/')) { + foreach (array_keys($_SESSION[$ob->app]) as $k) { + if (strpos($k, $ob->name) === 0) { + $ret[substr($k, strlen($ob->name))] = $k; + } + } + } + + return $ret; + } + /* Session object storage. */ /** @@ -238,6 +261,15 @@ class Horde_Session implements ArrayAccess $ob = $this->_parseOffset($offset); if (!isset($_SESSION[$ob->app][$ob->name])) { + $subkeys = $this->_subkeys($ob); + if (!empty($subkeys)) { + $ret = array(); + foreach ($subkeys as $k => $v) { + $ret[$k] = $this[$v]; + } + return $ret; + } + switch ($ob->type) { case 'array': return array(); @@ -322,6 +354,10 @@ class Horde_Session implements ArrayAccess $_SESSION[self::PRUNE][$ob->key], $_SESSION[self::SERIALIZED][$ob->key] ); + } else { + foreach ($this->_subkeys($ob) as $val) { + unset($this[$val]); + } } } } @@ -332,13 +368,16 @@ class Horde_Session implements ArrayAccess * Parses a session variable identifier. * Format: *
-     * [app:]name[;default]
+     * [app:]name[/subkey][;default]
      *
      * app - Application name.
      *       DEFAULT: horde
      * default - Default value type to return if value doesn't exist.
      *           Valid types: array, object
      *           DEFAULT: none
+     * subkey - Indicate that this entry is a subkey of the master name key.
+     *          Requesting a session key with a trailing '/' will retrieve all
+     *          subkeys of the given master key.
      * 
* * @return object Object with the following properties: