1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PSR7Session\Session; |
4
|
|
|
|
5
|
|
|
use PSR7Session\Id\SessionIdInterface; |
6
|
|
|
use PSR7Session\Id\UuidSessionId; |
7
|
|
|
use PSR7Session\Storage\StorageInterface; |
8
|
|
|
|
9
|
|
|
class StorableSession implements StorableSessionInterface |
10
|
|
|
{ |
11
|
|
|
/** @var SessionIdInterface */ |
12
|
|
|
private $id; |
13
|
|
|
/** @var SessionInterface */ |
14
|
|
|
private $wrappedSession; |
15
|
|
|
/** @var StorageInterface */ |
16
|
|
|
private $storage; |
17
|
|
|
|
18
|
|
|
public static function create(StorageInterface $storage):StorableSession |
19
|
|
|
{ |
20
|
|
|
return new self(DefaultSessionData::newEmptySession(), $storage); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @internal Should only be called by a storage |
25
|
|
|
*/ |
26
|
|
|
public static function fromId( |
27
|
|
|
SessionInterface $wrappedSession, |
28
|
|
|
StorageInterface $storage, |
29
|
|
|
SessionIdInterface $id |
30
|
|
|
):StorableSession |
31
|
|
|
{ |
32
|
|
|
$session = new self($wrappedSession, $storage); |
33
|
|
|
$session->id = $id; |
34
|
|
|
return $session; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function __construct(SessionInterface $wrappedSession, StorageInterface $storage) |
38
|
|
|
{ |
39
|
|
|
$this->id = new UuidSessionId; |
40
|
|
|
$this->wrappedSession = $wrappedSession; |
41
|
|
|
$this->storage = $storage; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function getId() : SessionIdInterface |
48
|
|
|
{ |
49
|
|
|
return $this->id; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function set(string $key, $value) |
56
|
|
|
{ |
57
|
|
|
$this->wrappedSession->set($key, $value); |
58
|
|
|
$this->save(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function get(string $key, $default = null) |
65
|
|
|
{ |
66
|
|
|
return $this->wrappedSession->get($key, $default); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function remove(string $key) |
73
|
|
|
{ |
74
|
|
|
$this->wrappedSession->remove($key); |
75
|
|
|
$this->save(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function clear() |
82
|
|
|
{ |
83
|
|
|
$this->wrappedSession->clear(); |
84
|
|
|
$this->save(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
|
|
public function has(string $key) : bool |
91
|
|
|
{ |
92
|
|
|
return $this->wrappedSession->has($key); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
|
|
public function hasChanged() : bool |
99
|
|
|
{ |
100
|
|
|
return $this->wrappedSession->hasChanged(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
|
|
public function isEmpty() : bool |
107
|
|
|
{ |
108
|
|
|
return $this->wrappedSession->isEmpty(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
|
|
public function jsonSerialize() |
115
|
|
|
{ |
116
|
|
|
return $this->wrappedSession->jsonSerialize(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function save() |
120
|
|
|
{ |
121
|
|
|
$this->storage->save($this); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|