History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: ZF-1139
Type: New Feature New Feature
Status: Resolved Resolved
Resolution: Fixed
Priority: Major Major
Assignee: Darby Felton
Reporter: Gavin
Votes: 0
Watchers: 2
Operations

If you were logged in you would be able to see more operations.
Google issue summary
Zend Framework

Enable applying user-specified callbacks to namespaces

Created: 26/Mar/07 01:56 PM   Updated: 19/Sep/07 02:19 PM
Component/s: Zend_Session
Affects Version/s: 0.8.0, 0.9.0, 0.9.1
Fix Version/s: 0.9.3

Time Tracking:
Not Specified

File Attachments: 1. Text File Namespace.patch (2 kb)


Tags:
Participants: Bill Karwin, Darby Felton and Gavin


 Description  « Hide
Simple operations like counting and filtering are supported indirectly via Zend_Session_Namespace::getIterator().

However, there are many other logical operations for set data types (e.g. Zend Framework session namespaces) that are not supported, including elementary "union". Thus, if a developer has an array of key => value pairs, the developer must iterate over the entire array, setting corresponding members in the desired namespace, one pair at a time.

require_once 'Zend/Session.php';
$namespace = new Zend_Session_Namespace('myspace'); 
$namespace->tree = 'cherry';
$namespace->rock = 'marble';
$namespace->apply(
	'array_merge',
	array('tree' => 'apple', 'fruit' => 'peach'),
	array('flower' => 'rose'));
echo "After applySet(): ";
print_r($_SESSION);

$namespace->applySet(
	'array_merge',
	array('tree' => 'apple', 'fruit' => 'peach'),
	array('flower' => 'rose'));
echo "After applySet(): ";
print_r($_SESSION);

Outputs:

After applySet(): Array
(
    [myspace] => Array
        (
            [tree] => cherry
            [rock] => marble
        )

)
After applySet(): Array
(
    [myspace] => Array
        (
            [tree] => apple
            [rock] => marble
            [fruit] => peach
            [flower] => rose
        )

)

As an experimental patch to explore possibilities, consider the following:

Index: Namespace.php
===================================================================
--- Namespace.php	(revision 4225)
+++ Namespace.php	(working copy)
@@ -255,6 +255,48 @@
 
 
     /**
+     * apply() - enables applying user-selected function, such as array_merge() to the namespace
+     * Caveat: ignores members expiring now.
+     *
+     * Example:
+     *   $namespace->apply('array_merge', array('tree' => 'apple', 'fruit' => 'peach'), array('flower' => 'rose'));
+     *   $namespace->apply('count');
+     *
+     * @param string $callback - callback function
+     * @param mixed  OPTIONAL arguments passed to the callback function
+     */
+    public function apply($callback)
+    {
+        $arg_list = func_get_args();
+        $arg_list[0] = $_SESSION[$this->_namespace];
+        return call_user_func_array($callback, $arg_list);
+    }
+
+
+    /**
+     * applySet() - enables applying user-selected function, and sets entire namespace to the result
+     * Result of $callback must be an array. Caveat: ignores members expiring now.
+     *
+     * Example:
+     *   $namespace->applySet('array_merge', array('tree' => 'apple', 'fruit' => 'peach'), array('flower' => 'rose'));
+     *
+     * @param string $callback - callback function
+     * @param mixed  OPTIONAL arguments passed to the callback function
+     */
+    public function applySet($callback)
+    {
+        $arg_list = func_get_args();
+        $arg_list[0] = $_SESSION[$this->_namespace];
+        $result = call_user_func_array($callback, $arg_list);
+        if (!is_array($result)) {
+            throw new Zend_Session_Exception("Result must be an array. Got: " . gettype($result));
+        }
+        $_SESSION[$this->_namespace] = $result;
+        return $result;
+    }
+
+
+    /**
      * __isset() - determine if a variable in this object's namespace is set
      *
      * @param string $name - programmatic name of a key, in a <key,value> pair in the current namespace

Cleanup of metadata associated with session variables dropped by applySet(), if any, is not done in this patch, but should be added (i.e. todo).



 All   Comments   Work Log   Change History   FishEye   Crucible      Sort Order: Ascending order - Click to sort in descending order
Gavin - 26/Mar/07 01:57 PM
experimental patch

Bill Karwin - 17/Apr/07 07:16 PM
Assign to Darby.

Darby Felton - 19/Sep/07 02:19 PM
This patch was already applied to Zend_Session_Namespace, in SVN r4471. Resolving as fixed for 0.9.3.