|
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 |
|
61
|
|
|
* @access protected |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $data; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Init the session |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $name 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
|
|
|
$this->data = unserialize( |
|
104
|
|
|
$this->getHandler()->read($this->getId()) |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@inheritDoc} |
|
112
|
|
|
*/ |
|
113
|
|
|
public function close() |
|
114
|
|
|
{ |
|
115
|
|
|
if (null !== $this->data) { |
|
116
|
|
|
// write to storage |
|
117
|
|
|
$this->getHandler()->write( |
|
118
|
|
|
$this->getId(), |
|
119
|
|
|
$this->serializeData() |
|
120
|
|
|
); |
|
121
|
|
|
|
|
122
|
|
|
// update cookie with id |
|
123
|
|
|
$this->getDriver()->set($this->getName(), $this->getId()); |
|
124
|
|
|
|
|
125
|
|
|
// close |
|
126
|
|
|
$this->data = null; |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
return $this; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* {@inheritDoc} |
|
133
|
|
|
*/ |
|
134
|
|
|
public function abort() |
|
135
|
|
|
{ |
|
136
|
|
|
$this->data = null; |
|
|
|
|
|
|
137
|
|
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* {@inheritDoc} |
|
142
|
|
|
*/ |
|
143
|
|
|
public function reset() |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->abort()->start(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* {@inheritDoc} |
|
150
|
|
|
*/ |
|
151
|
|
|
public function destroy() |
|
152
|
|
|
{ |
|
153
|
|
|
// delete from storage |
|
154
|
|
|
$this->getHandler()->destroy($this->getId()); |
|
155
|
|
|
|
|
156
|
|
|
// delete cookie |
|
157
|
|
|
$this->getDriver()->del($this->getName()); |
|
158
|
|
|
|
|
159
|
|
|
// mark as stopped |
|
160
|
|
|
$this->data = null; |
|
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
return $this; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* {@inheritDoc} |
|
167
|
|
|
*/ |
|
168
|
|
|
public function setName(/*# string */ $sessionName) |
|
169
|
|
|
{ |
|
170
|
|
|
$this->name = $sessionName; |
|
171
|
|
|
return $this; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* {@inheritDoc} |
|
176
|
|
|
*/ |
|
177
|
|
|
public function getName()/*# : string */ |
|
178
|
|
|
{ |
|
179
|
|
|
return $this->name; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* {@inheritDoc} |
|
184
|
|
|
*/ |
|
185
|
|
|
public function setId(/*# string */ $sessionId = '') |
|
186
|
|
|
{ |
|
187
|
|
|
$this->id = $sessionId; |
|
188
|
|
|
return $this; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* {@inheritDoc} |
|
193
|
|
|
*/ |
|
194
|
|
|
public function getId()/*# : string */ |
|
195
|
|
|
{ |
|
196
|
|
|
if (empty($this->id)) { |
|
197
|
|
|
// get from the client |
|
198
|
|
|
$this->id = $this->getDriver()->get($this->getName()); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
if (empty($this->id)) { |
|
202
|
|
|
// generate a new id |
|
203
|
|
|
$this->newId(); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
return $this->id; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* {@inheritDoc} |
|
211
|
|
|
*/ |
|
212
|
|
|
public function newId() |
|
213
|
|
|
{ |
|
214
|
|
|
$generator = $this->getGenerator(); |
|
215
|
|
|
$this->id = call_user_func($generator); |
|
216
|
|
|
return $this; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* {@inheritDoc} |
|
221
|
|
|
*/ |
|
222
|
|
|
public function sessionData(/*# string */ $namespace)/*# : \ArrayObject */ |
|
223
|
|
|
{ |
|
224
|
|
|
$this->checkStopped(); |
|
225
|
|
|
|
|
226
|
|
|
if (!isset($this->data[$namespace])) { |
|
227
|
|
|
$this->data[$namespace] = new \ArrayObject( |
|
228
|
|
|
[], |
|
229
|
|
|
\ArrayObject::STD_PROP_LIST | \ArrayObject::ARRAY_AS_PROPS |
|
230
|
|
|
); |
|
231
|
|
|
} |
|
232
|
|
|
return $this->data[$namespace]; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Throw RuntimeException if stopped |
|
237
|
|
|
* |
|
238
|
|
|
* @throws RuntimeException if session stopped |
|
239
|
|
|
* @access protected |
|
240
|
|
|
*/ |
|
241
|
|
|
protected function checkStopped() |
|
242
|
|
|
{ |
|
243
|
|
|
if (null === $this->data) { |
|
244
|
|
|
throw new RuntimeException( |
|
245
|
|
|
Message::get(Message::SESSION_STOPPED), |
|
246
|
|
|
Message::SESSION_STOPPED |
|
247
|
|
|
); |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
protected function serializeData()/*# : string */ |
|
252
|
|
|
{ |
|
253
|
|
|
return serialize($this->data); |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.