1
|
|
|
<?php |
2
|
|
|
namespace Wandu\Http\Parameters; |
3
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
use SessionHandlerInterface; |
6
|
|
|
use Wandu\Http\Contracts\CookieJarInterface; |
7
|
|
|
use Wandu\Http\Contracts\ParameterInterface; |
8
|
|
|
use Wandu\Http\Contracts\SessionInterface; |
9
|
|
|
use Wandu\Http\Session\Configuration; |
10
|
|
|
|
11
|
|
|
class Session extends Parameter implements SessionInterface |
12
|
|
|
{ |
13
|
|
|
/** @var \SessionHandlerInterface */ |
14
|
|
|
protected $handler; |
15
|
|
|
|
16
|
|
|
/** @var \Wandu\Http\Session\Configuration */ |
17
|
|
|
protected $config; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
protected $id; |
21
|
|
|
|
22
|
27 |
|
public function __construct( |
23
|
|
|
CookieJarInterface $cookieJar, |
24
|
|
|
SessionHandlerInterface $handler, |
25
|
|
|
Configuration $config = null, |
26
|
|
|
ParameterInterface $fallback = null |
27
|
|
|
) { |
28
|
27 |
|
$this->handler = $handler; |
29
|
27 |
|
$this->config = $config ?: new Configuration(); |
30
|
|
|
|
31
|
27 |
|
$sessionName = $this->config->getName(); |
32
|
|
|
|
33
|
27 |
|
$this->id = $cookieJar->has($sessionName) |
34
|
26 |
|
? $cookieJar->get($sessionName) |
35
|
1 |
|
: $this->config->getUniqueId(); |
36
|
|
|
|
37
|
27 |
|
$params = @unserialize($handler->read($this->id)); |
38
|
27 |
|
parent::__construct(isset($params) && is_array($params) ? $params : [], $fallback); |
39
|
27 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
6 |
|
public function toArray() |
45
|
|
|
{ |
46
|
6 |
|
$arrayToReturn = parent::toArray(); |
47
|
6 |
|
if (isset($arrayToReturn['__flash__'])) { |
48
|
1 |
|
$arrayToReturn = $arrayToReturn + $arrayToReturn['__flash__']; |
49
|
1 |
|
unset($arrayToReturn['__flash__'], $this->params['__flash__']); |
50
|
|
|
} |
51
|
6 |
|
return $arrayToReturn; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
14 |
|
public function get($name, $default = null, $isStrict = false) |
58
|
|
|
{ |
59
|
14 |
|
$this->validNameArgument($name); |
60
|
14 |
|
if (isset($this->params['__flash__'][$name]) && ($isStrict || !$isStrict && $this->params['__flash__'][$name])) { |
61
|
2 |
|
$resultToReturn = $this->params['__flash__'][$name]; |
62
|
2 |
|
unset($this->params['__flash__'][$name]); |
63
|
2 |
|
return $resultToReturn; |
64
|
|
|
} |
65
|
14 |
|
return parent::get($name, $default, $isStrict); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
5 |
|
public function set($name, $value) |
72
|
|
|
{ |
73
|
5 |
|
$this->validNameArgument($name); |
74
|
4 |
|
$this->params[$name] = $value; |
75
|
4 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
3 |
|
public function flash($name, $value) |
82
|
|
|
{ |
83
|
3 |
|
unset($this->params[$name]); |
84
|
3 |
|
if (!isset($this->params['__flash__'])) { |
85
|
3 |
|
$this->params['__flash__'] = []; |
86
|
|
|
} |
87
|
3 |
|
$this->params['__flash__'][$name] = $value; |
88
|
3 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
10 |
|
public function has($name) |
95
|
|
|
{ |
96
|
10 |
|
$this->validNameArgument($name); |
97
|
10 |
|
return parent::has($name) || // or, __flash__ exists check. |
98
|
10 |
|
(isset($this->params['__flash__']) && array_key_exists($name, $this->params['__flash__'])); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
3 |
|
public function remove($name) |
105
|
|
|
{ |
106
|
3 |
|
$this->validNameArgument($name); |
107
|
3 |
|
unset($this->params[$name]); |
108
|
3 |
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param mixed $name |
113
|
|
|
*/ |
114
|
22 |
|
protected function validNameArgument($name) |
115
|
|
|
{ |
116
|
22 |
|
if (!is_string($name)) { |
117
|
1 |
|
throw new InvalidArgumentException(sprintf('The session name must be string; "%s"', $name)); |
118
|
|
|
} |
119
|
22 |
|
if ($name === '') { |
120
|
1 |
|
throw new InvalidArgumentException('The session name cannot be empty.'); |
121
|
|
|
} |
122
|
21 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
1 |
|
public function offsetSet($offset, $value) |
128
|
|
|
{ |
129
|
1 |
|
$this->set($offset, $value); |
130
|
1 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritdoc} |
134
|
|
|
*/ |
135
|
1 |
|
public function offsetUnset($offset) |
136
|
|
|
{ |
137
|
1 |
|
$this->remove($offset); |
138
|
1 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param \Wandu\Http\Contracts\CookieJarInterface $cookieJar |
142
|
|
|
*/ |
143
|
1 |
|
public function applyToCookieJar(CookieJarInterface $cookieJar) |
144
|
|
|
{ |
145
|
1 |
|
$sessionName = $this->config->getName(); |
146
|
|
|
|
147
|
|
|
// save to handler |
148
|
1 |
|
$this->handler->write($this->id, serialize($this->params)); |
149
|
|
|
|
150
|
|
|
// apply to cookie-jar |
151
|
1 |
|
$cookieJar->set( |
152
|
|
|
$sessionName, |
153
|
1 |
|
$this->id, |
154
|
1 |
|
(new \DateTime())->setTimestamp(time() + $this->config->getTimeout()) |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
// garbage collection |
158
|
1 |
|
$pick = rand(1, max(1, $this->config->getGcFrequency())); |
159
|
1 |
|
if ($pick === 1) { |
160
|
|
|
$this->handler->gc($this->config->getTimeout()); |
161
|
|
|
} |
162
|
1 |
|
} |
163
|
|
|
} |
164
|
|
|
|