1: <?php
2:
3: namespace Alpha\Util\Http\Session;
4:
5: use Alpha\Util\Logging\Logger;
6:
7: /**
8: * Provides a session handle that stores session data in an array, useful for testing only.
9: *
10: * @since 2.0
11: *
12: * @author John Collins <dev@alphaframework.org>
13: * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
14: * @copyright Copyright (c) 2015, John Collins (founder of Alpha Framework).
15: * All rights reserved.
16: *
17: * <pre>
18: * Redistribution and use in source and binary forms, with or
19: * without modification, are permitted provided that the
20: * following conditions are met:
21: *
22: * * Redistributions of source code must retain the above
23: * copyright notice, this list of conditions and the
24: * following disclaimer.
25: * * Redistributions in binary form must reproduce the above
26: * copyright notice, this list of conditions and the
27: * following disclaimer in the documentation and/or other
28: * materials provided with the distribution.
29: * * Neither the name of the Alpha Framework nor the names
30: * of its contributors may be used to endorse or promote
31: * products derived from this software without specific
32: * prior written permission.
33: *
34: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
35: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
36: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
39: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
44: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
46: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47: * </pre>
48: */
49: class SessionProviderArray implements SessionProviderInterface
50: {
51: /**
52: * Trace logger.
53: *
54: * @var Alpha\Util\Logging\Logger
55: *
56: * @since 2.0
57: */
58: private static $logger = null;
59:
60: /**
61: * The hash array containing the session items.
62: *
63: * @var array
64: *
65: * @since 2.0
66: */
67: public static $sessionArray = array();
68:
69: /**
70: * The current session ID.
71: *
72: * @var string
73: *
74: * @since 2.0
75: */
76: private $ID;
77:
78: /**
79: * Constructor.
80: *
81: * @since 2.0
82: */
83: public function __construct()
84: {
85: self::$logger = new Logger('SessionProviderArray');
86: }
87:
88: /**
89: * {@inheritdoc}
90: */
91: public function init()
92: {
93: $this->ID = uniqid();
94: }
95:
96: /**
97: * {@inheritdoc}
98: */
99: public function destroy()
100: {
101: self::$sessionArray = array();
102: }
103:
104: /**
105: * {@inheritdoc}
106: */
107: public function get($key)
108: {
109: self::$logger->debug('>>get(key=['.$key.'])');
110:
111: self::$logger->debug('Getting value for key ['.$key.']');
112:
113: if (array_key_exists($key, self::$sessionArray)) {
114: return self::$sessionArray[$key];
115: } else {
116: return false;
117: }
118: }
119:
120: /**
121: * {@inheritdoc}
122: */
123: public function set($key, $value)
124: {
125: self::$logger->debug('Setting value for key ['.$key.']');
126:
127: self::$sessionArray[$key] = $value;
128: }
129:
130: /**
131: * {@inheritdoc}
132: */
133: public function delete($key)
134: {
135: self::$logger->debug('Removing value for key ['.$key.']');
136:
137: unset(self::$sessionArray[$key]);
138: }
139:
140: /**
141: * {@inheritdoc}
142: */
143: public function getID()
144: {
145: return $this->ID;
146: }
147: }
148: