1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Spiral\Session; |
13
|
|
|
|
14
|
|
|
use Spiral\Core\Container\InjectableInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Represents part of _SESSION array. |
18
|
|
|
*/ |
19
|
|
|
final class SessionSection implements SessionSectionInterface, InjectableInterface |
20
|
|
|
{ |
21
|
|
|
/** @var SessionInterface */ |
22
|
|
|
private $session; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Reference to _SESSION segment. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $name; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param SessionInterface $session |
33
|
|
|
* @param string|null $name |
34
|
|
|
*/ |
35
|
|
|
public function __construct(SessionInterface $session, string $name = null) |
36
|
|
|
{ |
37
|
|
|
$this->session = $session; |
38
|
|
|
$this->name = $name; |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Shortcut for get. |
43
|
|
|
* |
44
|
|
|
* @param string $name |
45
|
|
|
* @return mixed|null |
46
|
|
|
*/ |
47
|
|
|
public function __get(string $name) |
48
|
|
|
{ |
49
|
|
|
return $this->get($name); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $name |
54
|
|
|
* @param mixed $value |
55
|
|
|
*/ |
56
|
|
|
public function __set(string $name, $value): void |
57
|
|
|
{ |
58
|
|
|
$this->set($name, $value); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $name |
63
|
|
|
* |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
public function __isset(string $name) |
67
|
|
|
{ |
68
|
|
|
return $this->has($name); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $name |
73
|
|
|
*/ |
74
|
|
|
public function __unset(string $name): void |
75
|
|
|
{ |
76
|
|
|
$this->delete($name); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritdoc |
81
|
|
|
*/ |
82
|
|
|
public function getName(): string |
83
|
|
|
{ |
84
|
|
|
return $this->name; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritdoc |
89
|
|
|
*/ |
90
|
|
|
public function getIterator() |
91
|
|
|
{ |
92
|
|
|
return new \ArrayIterator($this->getAll()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritdoc |
97
|
|
|
*/ |
98
|
|
|
public function getAll(): array |
99
|
|
|
{ |
100
|
|
|
$this->resumeSection(); |
101
|
|
|
|
102
|
|
|
return $_SESSION[$this->name]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @inheritdoc |
107
|
|
|
*/ |
108
|
|
|
public function set(string $name, $value): void |
109
|
|
|
{ |
110
|
|
|
$this->resumeSection(); |
111
|
|
|
|
112
|
|
|
$_SESSION[$this->name][$name] = $value; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @inheritdoc |
117
|
|
|
*/ |
118
|
|
|
public function has(string $name): bool |
119
|
|
|
{ |
120
|
|
|
$this->resumeSection(); |
121
|
|
|
|
122
|
|
|
return array_key_exists($name, $_SESSION[$this->name]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @inheritdoc |
127
|
|
|
*/ |
128
|
|
|
public function get(string $name, $default = null) |
129
|
|
|
{ |
130
|
|
|
if (!$this->has($name)) { |
131
|
|
|
return $default; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $_SESSION[$this->name][$name]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @inheritdoc |
139
|
|
|
*/ |
140
|
|
|
public function pull(string $name, $default = null) |
141
|
|
|
{ |
142
|
|
|
$value = $this->get($name, $default); |
143
|
|
|
$this->delete($name); |
144
|
|
|
|
145
|
|
|
return $value; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @inheritdoc |
150
|
|
|
*/ |
151
|
|
|
public function delete(string $name): void |
152
|
|
|
{ |
153
|
|
|
$this->resumeSection(); |
154
|
|
|
unset($_SESSION[$this->name][$name]); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @inheritdoc |
159
|
|
|
*/ |
160
|
|
|
public function clear(): void |
161
|
|
|
{ |
162
|
|
|
$this->resumeSection(); |
163
|
|
|
$_SESSION[$this->name] = []; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @inheritdoc |
168
|
|
|
*/ |
169
|
|
|
public function offsetExists($offset): bool |
170
|
|
|
{ |
171
|
|
|
return $this->has($offset); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @inheritdoc |
176
|
|
|
*/ |
177
|
|
|
public function offsetGet($offset) |
178
|
|
|
{ |
179
|
|
|
return $this->get($offset); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @inheritdoc |
184
|
|
|
*/ |
185
|
|
|
public function offsetSet($offset, $value): void |
186
|
|
|
{ |
187
|
|
|
$this->set($offset, $value); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @inheritdoc |
192
|
|
|
*/ |
193
|
|
|
public function offsetUnset($offset): void |
194
|
|
|
{ |
195
|
|
|
$this->delete($offset); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Ensure that session have proper section. |
200
|
|
|
*/ |
201
|
|
|
private function resumeSection(): void |
202
|
|
|
{ |
203
|
|
|
$this->session->resume(); |
204
|
|
|
|
205
|
|
|
if (!isset($_SESSION[$this->name])) { |
206
|
|
|
$_SESSION[$this->name] = []; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.