SessionNamespace.php
4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* Created by PhpStorm.
* User: Zip
* Date: 14/12/25
* Time: 下午4:44
*/
namespace Hood\Core\Session;
use Hood\Debug\DebugException;
class SessionNamespace
{
protected $_namespace = "session_default";
protected static $_namespaceLocks = array();
protected static $_writable = false;
public function __construct($namespace = 'session_default', $singleInstance = false)
{
if ($namespace === '') {
throw new DebugException('Session Namespace 不能为空.');
}
if ($namespace[0] == "_") {
throw new DebugException('Session Namespace 不能已_开头.');
}
$this->_namespace = $namespace;
}
/**
* 锁定Session NameSpace
*/
public function lock()
{
self::$_namespaceLocks[$this->_namespace] = true;
}
/**
* 解锁Session NameSpace
*/
public function unlock()
{
unset(self::$_namespaceLocks[$this->_namespace]);
}
/**
* 解锁全部Session NameSpace
*/
public static function unlockAll()
{
self::$_namespaceLocks = array();
}
/**
* 检查Session NameSpace是否有锁
* @return bool
*/
public function isLocked()
{
return isset(self::$_namespaceLocks[$this->_namespace]);
}
/**
* 释放所有session
*/
public function unsetAll()
{
return $this->_namespaceUnset($this->_namespace);
}
/**
* 释放所有session
* @param $namespace
* @param null $name
*/
private function _namespaceUnset($namespace, $name = null)
{
$name = (string)$name;
if ($name === '') {
unset($_SESSION[$namespace]);
} else {
unset($_SESSION[$namespace][$name]);
}
}
/**
* 存入一个Session
* @param $name
* @param $value
* @throws Q_Exception
*/
public function __set($name, $value)
{
if (isset(self::$_namespaceLocks[$this->_namespace])) {
throw new DebugException('This session/namespace has been marked as read-only.');
}
if ($name === '') {
throw new DebugException("The '$name' key must be a non-empty string");
}
$name = (string)$name;
$_SESSION[$this->_namespace][$name] = $value;
}
/**
* 获取一个Session
* @param $name
* @return mixed
* @throws Q_Exception
*/
public function __get($name)
{
if (isset(self::$_namespaceLocks[$this->_namespace])) {
throw new DebugException('This session/namespace has been marked as read-only.');
}
if ($name === '') {
throw new DebugException("The '$name' key must be a non-empty string");
}
return $this->_namespaceGet($this->_namespace, $name);
}
/**
* 注销session
* @param $name
* @throws Q_Exception
*/
public function __unset($name)
{
if ($name === '') {
throw new DebugException("The '$name' key must be a non-empty string");
}
return $this->_namespaceUnset($this->_namespace, $name);
}
/**
* @param $namespace
* @param null $name
* @return mixed
*/
private function _namespaceGet($namespace, $name = null)
{
if ($name === null) {
return empty($_SESSION[$namespace]) ? NULL : $_SESSION[$namespace];
}
return empty($_SESSION[$namespace][$name]) ? NULL : $_SESSION[$namespace][$name];
}
/**
* 判断Session是否存在
* @param $name
* @return bool
* @throws Q_Exception
*/
public function __isset($name)
{
if ($name === '') {
throw new DebugException("The '$name' key must be a non-empty string");
}
return $this->_namespaceIsset($this->_namespace, $name);
}
/**
* @param $namespace
* @param null $name
* @return bool
* @throws Q_Exception
*/
private function _namespaceIsset($namespace, $name = null)
{
if ($name === null) {
return isset($_SESSION[$namespace]);
}
return isset($_SESSION[$namespace][$name]);
}
}