Splitted off the Horde_Notification_Storage interface and added the
authorGunnar Wrobel <p@rdus.de>
Tue, 27 Oct 2009 17:51:09 +0000 (18:51 +0100)
committerGunnar Wrobel <p@rdus.de>
Tue, 27 Oct 2009 17:51:09 +0000 (18:51 +0100)
Horde_Notification_Storage_Session class implementing this interface.

framework/Notification/lib/Horde/Notification.php
framework/Notification/lib/Horde/Notification/Storage.php [new file with mode: 0644]
framework/Notification/lib/Horde/Notification/Storage/Session.php [new file with mode: 0644]
framework/Notification/package.xml
framework/Notification/test/Horde/Notification/Class/NotificationTest.php

index 0ff9e2b..2ab002d 100644 (file)
@@ -29,11 +29,11 @@ class Horde_Notification
     protected $_listeners = array();
 
     /**
-     * The name of the session variable where we store the messages.
+     * The storage location where we store the messages.
      *
-     * @var string
+     * @var Horde_Notification_Storage
      */
-    protected $_stack;
+    protected $_storage;
 
     /**
      * A Horde_Alarm instance.
@@ -56,7 +56,8 @@ class Horde_Notification
     static public function singleton($stack = 'horde_notification_stacks')
     {
         if (!isset(self::$_instances[$stack])) {
-            self::$_instances[$stack] = new Horde_Notification($stack);
+            $storage = new Horde_Notification_Storage_Session($stack);
+            self::$_instances[$stack] = new Horde_Notification($storage);
         }
 
         return self::$_instances[$stack];
@@ -66,17 +67,11 @@ class Horde_Notification
      * Initialize the notification system, set up any needed session
      * variables, etc.
      *
-     * @param string $stack  The name of the message stack to use.
+     * @param Horde_Notification_Storage $storage The storage location to use.
      */
-    protected function __construct($stack)
+    protected function __construct(Horde_Notification_Storage $storage)
     {
-        $this->_stack = $stack;
-
-        /* Make sure the message stack is registered in the session,
-         * and obtain a global-scope reference to it. */
-        if (!isset($_SESSION[$this->_stack])) {
-            $_SESSION[$this->_stack] = array();
-        }
+        $this->_storage = $storage;
 
         if (!empty($GLOBALS['conf']['alarms']['driver'])) {
             $this->_alarm = Horde_Alarm::factory();
@@ -116,8 +111,8 @@ class Horde_Notification
 
         if (class_exists($class)) {
             $this->_listeners[$listener] = new $class($params);
-            if (!isset($_SESSION[$this->_stack][$listener])) {
-                $_SESSION[$this->_stack][$listener] = array();
+            if (!isset($this->_storage[$listener])) {
+                $this->_storage[$listener] = array();
             }
             return $this->_listeners[$listener];
         }
@@ -141,7 +136,7 @@ class Horde_Notification
         }
 
         $list = $this->_listeners[$listener];
-        unset($this->_listeners[$listener], $_SESSION[$this->_stack][$list->getName()]);
+        unset($this->_listeners[$listener], $this->_storage[$list->getName()]);
     }
 
     /**
@@ -198,11 +193,14 @@ class Horde_Notification
 
         foreach ($this->_listeners as $listener) {
             if ($listener->handles($type)) {
-                $_SESSION[$this->_stack][$listener->getName()][] = array(
-                    'class' => get_class($event),
-                    'event' => serialize($event),
-                    'flags' => serialize($flags),
-                    'type' => $type
+                $this->_storage->push(
+                    $listener->getName(),
+                    array(
+                        'class' => get_class($event),
+                        'event' => serialize($event),
+                        'flags' => serialize($flags),
+                        'type' => $type
+                    )
                 );
             }
         }
@@ -231,7 +229,8 @@ class Horde_Notification
 
         foreach ($options['listeners'] as $listener) {
             if (isset($this->_listeners[$listener])) {
-                $this->_listeners[$listener]->notify($_SESSION[$this->_stack][$this->_listeners[$listener]->getName()], $options);
+                $instance = $this->_storage[$this->_listeners[$listener]->getName()];
+                $this->_listeners[$listener]->notify($instance, $options);
             }
         }
     }
@@ -250,13 +249,13 @@ class Horde_Notification
         if (is_null($my_listener)) {
             $count = 0;
             foreach ($this->_listeners as $listener) {
-                if (isset($_SESSION[$this->_stack][$listener->getName()])) {
-                    $count += count($_SESSION[$this->_stack][$listener->getName()]);
+                if (isset($this->_storage[$listener->getName()])) {
+                    $count += count($this->_storage[$listener->getName()]);
                 }
             }
             return $count;
         } else {
-            return @count($_SESSION[$this->_stack][$this->_listeners[strtolower($my_listener)]->getName()]);
+            return @count($this->_storage[$this->_listeners[strtolower($my_listener)]->getName()]);
         }
     }
 
diff --git a/framework/Notification/lib/Horde/Notification/Storage.php b/framework/Notification/lib/Horde/Notification/Storage.php
new file mode 100644 (file)
index 0000000..ca35e7a
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+/**
+ * An interface describing a storage location for notification messages.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Notification
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Notification
+ */
+
+/**
+ * An interface describing a storage location for notification messages.
+ *
+ * Copyright 2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Horde
+ * @package  Notification
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Notification
+ */
+interface Horde_Notification_Storage extends ArrayAccess
+{
+    /**
+     * Store a new event.
+     *
+     * @param string $listener The event will be stored for this listener.
+     * @param array  $event    The event to store.
+     *
+     * @return NULL
+     */
+    public function push($listener, array $event);
+}
\ No newline at end of file
diff --git a/framework/Notification/lib/Horde/Notification/Storage/Session.php b/framework/Notification/lib/Horde/Notification/Storage/Session.php
new file mode 100644 (file)
index 0000000..0232fcb
--- /dev/null
@@ -0,0 +1,113 @@
+<?php
+/**
+ * A class that stores notifications in the session.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Notification
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Notification
+ */
+
+/**
+ * A class that stores notifications in the session.
+ *
+ * Copyright 2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Horde
+ * @package  Notification
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Notification
+ */
+class Horde_Notification_Storage_Session implements Horde_Notification_Storage
+{
+    /**
+     * The stack name.
+     *
+     * @var string
+     */
+    private $_stack;
+
+    /**
+     * Constructor.
+     *
+     * @param string $stack The name of the notification stack.
+     */
+    public function __construct($stack)
+    {
+        $this->_stack = $stack;
+
+        /* Make sure the message stack is registered in the session. */
+        if (!isset($_SESSION[$this->_stack])) {
+            $_SESSION[$this->_stack] = array();
+        }
+    }
+
+    /**
+     * Return the given value from the notification store.
+     *
+     * @param string $key The key for the data.
+     *
+     * @return mixed The notification data stored for the given key.
+     */
+    public function offsetGet($key)
+    {
+        return $_SESSION[$this->_stack][$key];
+    }
+
+    /**
+     * Set the given value in the notification store.
+     *
+     * @param string $key   The key for the data.
+     * @param mixed  $value The data.
+     *
+     * @return NULL
+     */
+    public function offsetSet($key, $value)
+    {
+        $_SESSION[$this->_stack][$key] = $value;
+    }
+
+    /**
+     * Is the given key set in the notification store?
+     *
+     * @param string $key The key of the data.
+     *
+     * @return boolean True if the element is set, false otherwise.
+     */
+    public function offsetExists($key)
+    {
+        return isset($_SESSION[$this->_stack][$key]);
+    }
+
+    /**
+     * Unset the given key set in the notification store.
+     *
+     * @param string $key The key of the data.
+     *
+     * @return NULL
+     */
+    public function offsetUnset($key)
+    {
+        unset($_SESSION[$this->_stack][$key]);
+    }
+
+    /**
+     * Store a new event for the given listener name.
+     *
+     * @param string $listener The event will be stored for this listener.
+     * @param array  $event    The event to store.
+     *
+     * @return NULL
+     */
+    public function push($listener, array $event)
+    {
+        $_SESSION[$this->_stack][$listener][] = $event;
+    }
+}
\ No newline at end of file
index 6522780..8a695b4 100644 (file)
@@ -41,6 +41,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
       </dir> <!-- /lib/Horde/Notification/Listener -->
       <file name="Event.php" role="php" />
       <file name="Listener.php" role="php" />
+      <file name="Storage.php" role="php" />
+      <dir name="Storage">
+       <file name="Session.php" role="php" />
+      </dir> <!-- /lib/Horde/Notification/Storage -->
      </dir> <!-- /lib/Horde/Notification -->
      <file name="Notification.php" role="php" />
     </dir> <!-- /lib/Horde -->
@@ -100,6 +104,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
    <install name="lib/Horde/Notification/Listener/Status.php" as="Horde/Notification/Listener/Status.php" />
    <install name="lib/Horde/Notification/Event.php" as="Horde/Notification/Event.php" />
    <install name="lib/Horde/Notification/Listener.php" as="Horde/Notification/Listener.php" />
+   <install name="lib/Horde/Notification/Storage.php" as="Horde/Notification/Storage.php" />
+   <install name="lib/Horde/Notification/Storage/Session.php" as="Horde/Notification/Storage/Session.php" />
    <install name="lib/Horde/Notification.php" as="Horde/Notification.php" />
    <install name="test/Horde/Notification/AllTests.php" as="Horde/Notification/AllTests.php" />
    <install name="test/Horde/Notification/Autoload.php" as="Horde/Notification/Autoload.php" />
index f8d9fef..346269b 100644 (file)
@@ -221,7 +221,8 @@ class Horde_Notification_Instance extends Horde_Notification
 {
     static public function newInstance($stack)
     {
-        $instance = new Horde_Notification($stack);
+        $storage = new Horde_Notification_Storage_Session($stack);
+        $instance = new Horde_Notification($storage);
         return $instance;
     }
 }