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