From: Gunnar Wrobel Date: Tue, 27 Oct 2009 17:51:09 +0000 (+0100) Subject: Splitted off the Horde_Notification_Storage interface and added the X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=d000d80252fb52aa5fcac0c718cbbcec08f5762f;p=horde.git Splitted off the Horde_Notification_Storage interface and added the Horde_Notification_Storage_Session class implementing this interface. --- diff --git a/framework/Notification/lib/Horde/Notification.php b/framework/Notification/lib/Horde/Notification.php index 0ff9e2b4e..2ab002d00 100644 --- a/framework/Notification/lib/Horde/Notification.php +++ b/framework/Notification/lib/Horde/Notification.php @@ -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 index 000000000..ca35e7a9b --- /dev/null +++ b/framework/Notification/lib/Horde/Notification/Storage.php @@ -0,0 +1,39 @@ + + * @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 + * @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 index 000000000..0232fcb28 --- /dev/null +++ b/framework/Notification/lib/Horde/Notification/Storage/Session.php @@ -0,0 +1,113 @@ + + * @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 + * @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 diff --git a/framework/Notification/package.xml b/framework/Notification/package.xml index 65227808b..8a695b43d 100644 --- a/framework/Notification/package.xml +++ b/framework/Notification/package.xml @@ -41,6 +41,10 @@ http://pear.php.net/dtd/package-2.0.xsd"> + + + + @@ -100,6 +104,8 @@ http://pear.php.net/dtd/package-2.0.xsd"> + + diff --git a/framework/Notification/test/Horde/Notification/Class/NotificationTest.php b/framework/Notification/test/Horde/Notification/Class/NotificationTest.php index f8d9fefd3..346269b88 100644 --- a/framework/Notification/test/Horde/Notification/Class/NotificationTest.php +++ b/framework/Notification/test/Horde/Notification/Class/NotificationTest.php @@ -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; } }