1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Session |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Session; |
16
|
|
|
|
17
|
|
|
use SessionHandlerInterface; |
18
|
|
|
use Phossa2\Session\Message\Message; |
19
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
20
|
|
|
use Phossa2\Session\Traits\DriverAwareTrait; |
21
|
|
|
use Phossa2\Session\Traits\HandlerAwareTrait; |
22
|
|
|
use Phossa2\Session\Interfaces\DriverInterface; |
23
|
|
|
use Phossa2\Session\Traits\ValidatorAwareTrait; |
24
|
|
|
use Phossa2\Session\Traits\GeneratorAwareTrait; |
25
|
|
|
use Phossa2\Session\Exception\RuntimeException; |
26
|
|
|
use Phossa2\Session\Interfaces\SessionInterface; |
27
|
|
|
use Phossa2\Session\Interfaces\GeneratorInterface; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Session |
31
|
|
|
* |
32
|
|
|
* @package Phossa2\Session |
33
|
|
|
* @author Hong Zhang <[email protected]> |
34
|
|
|
* @version 2.1.0 |
35
|
|
|
* @since 2.1.0 added |
36
|
|
|
*/ |
37
|
|
|
class Session extends ObjectAbstract implements SessionInterface |
38
|
|
|
{ |
39
|
|
|
use DriverAwareTrait, HandlerAwareTrait, ValidatorAwareTrait, GeneratorAwareTrait; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Session id |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
* @access protected |
46
|
|
|
*/ |
47
|
|
|
protected $id; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Session name |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
* @access protected |
54
|
|
|
*/ |
55
|
|
|
protected $name; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* session data |
59
|
|
|
* |
60
|
|
|
* @var array|null |
61
|
|
|
* @access protected |
62
|
|
|
*/ |
63
|
|
|
protected $data; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Init the session |
67
|
|
|
* |
68
|
|
|
* @param string $sessionName session name |
69
|
|
|
* @param SessionHandlerInterface $handler session save handler |
70
|
|
|
* @param DriverInterface $driver communication driver |
71
|
|
|
* @param GeneratorInterface $generator id generator |
72
|
|
|
* @access public |
73
|
|
|
*/ |
74
|
|
|
public function __construct( |
75
|
|
|
/*# string */ $sessionName = 'session', |
76
|
|
|
SessionHandlerInterface $handler = null, |
77
|
|
|
DriverInterface $driver = null, |
78
|
|
|
GeneratorInterface $generator = null |
79
|
|
|
) { |
80
|
|
|
$this |
81
|
|
|
->setName($sessionName) |
82
|
|
|
->setHandler($handler) |
83
|
|
|
->setDriver($driver) |
84
|
|
|
->setGenerator($generator); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Write & close |
89
|
|
|
* |
90
|
|
|
* @access public |
91
|
|
|
*/ |
92
|
|
|
public function __destruct() |
93
|
|
|
{ |
94
|
|
|
$this->close(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritDoc} |
99
|
|
|
*/ |
100
|
|
|
public function start() |
101
|
|
|
{ |
102
|
|
|
if (null === $this->data) { |
103
|
|
|
// read data |
104
|
|
|
$this->data = unserialize( |
105
|
|
|
$this->getHandler()->read($this->getId()) |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
// validate |
109
|
|
|
if (!$this->isValid()) { |
110
|
|
|
throw new RuntimeException( |
111
|
|
|
Message::get(Message::SESSION_INVALID, $this->getName()), |
112
|
|
|
Message::SESSION_INVALID |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritDoc} |
121
|
|
|
*/ |
122
|
|
|
public function close() |
123
|
|
|
{ |
124
|
|
|
if (null !== $this->data) { |
125
|
|
|
// write to storage |
126
|
|
|
$this->getHandler()->write( |
127
|
|
|
$this->getId(), |
128
|
|
|
$this->serializeData() |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
// update cookie with id |
132
|
|
|
$this->getDriver()->set($this->getName(), $this->getId()); |
133
|
|
|
|
134
|
|
|
// close |
135
|
|
|
$this->data = null; |
136
|
|
|
} |
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritDoc} |
142
|
|
|
*/ |
143
|
|
|
public function abort() |
144
|
|
|
{ |
145
|
|
|
$this->data = null; |
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritDoc} |
151
|
|
|
*/ |
152
|
|
|
public function reset() |
153
|
|
|
{ |
154
|
|
|
return $this->abort()->start(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritDoc} |
159
|
|
|
*/ |
160
|
|
|
public function destroy() |
161
|
|
|
{ |
162
|
|
|
// delete from storage |
163
|
|
|
$this->getHandler()->destroy($this->getId()); |
164
|
|
|
|
165
|
|
|
// delete cookie |
166
|
|
|
$this->getDriver()->del($this->getName()); |
167
|
|
|
|
168
|
|
|
// mark as stopped |
169
|
|
|
$this->data = null; |
170
|
|
|
|
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* {@inheritDoc} |
176
|
|
|
*/ |
177
|
|
|
public function setName(/*# string */ $sessionName) |
178
|
|
|
{ |
179
|
|
|
$this->name = $sessionName; |
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* {@inheritDoc} |
185
|
|
|
*/ |
186
|
|
|
public function getName()/*# : string */ |
187
|
|
|
{ |
188
|
|
|
return $this->name; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* {@inheritDoc} |
193
|
|
|
*/ |
194
|
|
|
public function setId(/*# string */ $sessionId = '') |
195
|
|
|
{ |
196
|
|
|
$this->id = $sessionId; |
197
|
|
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* {@inheritDoc} |
202
|
|
|
*/ |
203
|
|
|
public function getId()/*# : string */ |
204
|
|
|
{ |
205
|
|
|
if (empty($this->id)) { |
206
|
|
|
// get from the client |
207
|
|
|
$this->id = $this->getDriver()->get($this->getName()); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
if (empty($this->id)) { |
211
|
|
|
// generate a new id |
212
|
|
|
$this->newId(); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return $this->id; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* {@inheritDoc} |
220
|
|
|
*/ |
221
|
|
|
public function newId() |
222
|
|
|
{ |
223
|
|
|
$generator = $this->getGenerator(); |
224
|
|
|
$this->id = call_user_func($generator); |
225
|
|
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* {@inheritDoc} |
230
|
|
|
*/ |
231
|
|
|
public function sessionData(/*# string */ $namespace)/*# : \ArrayObject */ |
232
|
|
|
{ |
233
|
|
|
$this->checkStopped(); |
234
|
|
|
|
235
|
|
|
if (!isset($this->data[$namespace])) { |
236
|
|
|
$this->data[$namespace] = new \ArrayObject( |
237
|
|
|
[], |
238
|
|
|
\ArrayObject::STD_PROP_LIST | \ArrayObject::ARRAY_AS_PROPS |
239
|
|
|
); |
240
|
|
|
} |
241
|
|
|
return $this->data[$namespace]; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Throw RuntimeException if stopped |
246
|
|
|
* |
247
|
|
|
* @throws RuntimeException if session stopped |
248
|
|
|
* @access protected |
249
|
|
|
*/ |
250
|
|
|
protected function checkStopped() |
251
|
|
|
{ |
252
|
|
|
if (null === $this->data) { |
253
|
|
|
throw new RuntimeException( |
254
|
|
|
Message::get(Message::SESSION_STOPPED), |
255
|
|
|
Message::SESSION_STOPPED |
256
|
|
|
); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
protected function serializeData()/*# : string */ |
261
|
|
|
{ |
262
|
|
|
return serialize($this->data); |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|