1: <?php
2: /**
3: * A class that stores notifications in the session.
4: *
5: * @category Horde
6: * @package Notification
7: * @author Gunnar Wrobel <wrobel@pardus.de>
8: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
9: * @link http://pear.horde.org/index.php?package=Notification
10: */
11:
12: /**
13: * A class that stores notifications in the session.
14: *
15: * Copyright 2009-2012 Horde LLC (http://www.horde.org/)
16: *
17: * See the enclosed file COPYING for license information (LGPL). If you
18: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
19: *
20: * @category Horde
21: * @package Notification
22: * @author Gunnar Wrobel <wrobel@pardus.de>
23: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
24: * @link http://pear.horde.org/index.php?package=Notification
25: */
26: class Horde_Notification_Storage_Session
27: implements Horde_Notification_Storage_Interface
28: {
29: /**
30: * The stack name.
31: *
32: * @var string
33: */
34: protected $_stack;
35:
36: /**
37: * Constructor.
38: *
39: * @param string $stack The name of the notification stack.
40: */
41: public function __construct($stack)
42: {
43: $this->_stack = $stack;
44:
45: /* Make sure the message stack is registered in the session. */
46: if (!isset($_SESSION[$this->_stack])) {
47: $_SESSION[$this->_stack] = array();
48: }
49: }
50:
51: /**
52: * Return the given stack from the notification store.
53: *
54: * @param string $key The key for the data.
55: *
56: * @return mixed The notification data stored for the given key.
57: */
58: public function get($key)
59: {
60: return $_SESSION[$this->_stack][$key];
61: }
62:
63: /**
64: * Set the given stack in the notification store.
65: *
66: * @param string $key The key for the data.
67: * @param mixed $value The data.
68: */
69: public function set($key, $value)
70: {
71: $_SESSION[$this->_stack][$key] = $value;
72: }
73:
74: /**
75: * Is the given stack present in the notification store?
76: *
77: * @param string $key The key of the data.
78: *
79: * @return boolean True if the element is set, false otherwise.
80: */
81: public function exists($key)
82: {
83: return isset($_SESSION[$this->_stack][$key]);
84: }
85:
86: /**
87: * Unset the given stack in the notification store.
88: *
89: * @param string $key The key of the data.
90: */
91: public function clear($key)
92: {
93: unset($_SESSION[$this->_stack][$key]);
94: }
95:
96: /**
97: * Store a new event for the given listener stack.
98: *
99: * @param string $listener The event will be stored for
100: * this listener.
101: * @param Horde_Notification_Event $event The event to store.
102: */
103: public function push($listener, Horde_Notification_Event $event)
104: {
105: /* No need to serialize() ourselves - PHP's session handling does
106: * this automatically. */
107: $_SESSION[$this->_stack][$listener][] = $event;
108: }
109:
110: }
111: