Horde_Notification_Storage_Session class implementing this interface.
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.
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];
* 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();
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];
}
}
$list = $this->_listeners[$listener];
- unset($this->_listeners[$listener], $_SESSION[$this->_stack][$list->getName()]);
+ unset($this->_listeners[$listener], $this->_storage[$list->getName()]);
}
/**
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
+ )
);
}
}
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);
}
}
}
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()]);
}
}
--- /dev/null
+<?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
--- /dev/null
+<?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
</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 -->
<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" />
{
static public function newInstance($stack)
{
- $instance = new Horde_Notification($stack);
+ $storage = new Horde_Notification_Storage_Session($stack);
+ $instance = new Horde_Notification($storage);
return $instance;
}
}