|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the MIT license. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
declare(strict_types=1); |
|
20
|
|
|
|
|
21
|
|
|
namespace PSR7Session\Storageless\Session; |
|
22
|
|
|
|
|
23
|
|
|
final class LazySession implements SessionInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @internal do not access directly: use {@see LazySession::getRealSession} instead |
|
27
|
|
|
* |
|
28
|
|
|
* @var SessionInterface|null |
|
29
|
|
|
*/ |
|
30
|
|
|
private $realSession; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var callable |
|
34
|
|
|
*/ |
|
35
|
|
|
private $sessionLoader; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Instantiation via __construct is not allowed, use {@see LazySession::fromContainerBuildingCallback} instead |
|
39
|
|
|
*/ |
|
40
|
50 |
|
private function __construct() |
|
41
|
|
|
{ |
|
42
|
50 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param callable $sessionLoader |
|
46
|
|
|
* |
|
47
|
|
|
* @return self |
|
48
|
|
|
*/ |
|
49
|
50 |
|
public static function fromContainerBuildingCallback(callable $sessionLoader) : self |
|
50
|
|
|
{ |
|
51
|
50 |
|
$instance = new self(); |
|
52
|
|
|
|
|
53
|
50 |
|
$instance->sessionLoader = $sessionLoader; |
|
54
|
|
|
|
|
55
|
50 |
|
return $instance; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritDoc} |
|
60
|
|
|
*/ |
|
61
|
17 |
|
public function set(string $key, $value) |
|
62
|
|
|
{ |
|
63
|
17 |
|
$this->getRealSession()->set($key, $value); |
|
64
|
17 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritDoc} |
|
68
|
|
|
*/ |
|
69
|
4 |
|
public function get(string $key, $default = null) |
|
70
|
|
|
{ |
|
71
|
4 |
|
return $this->getRealSession()->get($key, $default); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritDoc} |
|
76
|
|
|
*/ |
|
77
|
2 |
|
public function remove(string $key) |
|
78
|
|
|
{ |
|
79
|
2 |
|
$this->getRealSession()->remove($key); |
|
80
|
2 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritDoc} |
|
84
|
|
|
*/ |
|
85
|
4 |
|
public function clear() |
|
86
|
|
|
{ |
|
87
|
4 |
|
$this->getRealSession()->clear(); |
|
88
|
4 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritDoc} |
|
92
|
|
|
*/ |
|
93
|
1 |
|
public function has(string $key): bool |
|
94
|
|
|
{ |
|
95
|
1 |
|
return $this->getRealSession()->has($key); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritDoc} |
|
100
|
|
|
*/ |
|
101
|
42 |
|
public function hasChanged() : bool |
|
102
|
|
|
{ |
|
103
|
42 |
|
return $this->realSession && $this->realSession->hasChanged(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritDoc} |
|
108
|
|
|
*/ |
|
109
|
39 |
|
public function isEmpty() : bool |
|
110
|
|
|
{ |
|
111
|
39 |
|
return $this->getRealSession()->isEmpty(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* {@inheritDoc} |
|
116
|
|
|
*/ |
|
117
|
18 |
|
public function jsonSerialize() |
|
118
|
|
|
{ |
|
119
|
18 |
|
return $this->getRealSession()->jsonSerialize(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get or initialize the session |
|
124
|
|
|
* |
|
125
|
|
|
* @return SessionInterface |
|
126
|
|
|
*/ |
|
127
|
46 |
|
private function getRealSession() : SessionInterface |
|
128
|
|
|
{ |
|
129
|
46 |
|
return $this->realSession ?? $this->realSession = $this->loadSession(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Type-safe wrapper that ensures that the given callback returns the expected type of object, when called |
|
134
|
|
|
* |
|
135
|
|
|
* @return SessionInterface |
|
136
|
|
|
*/ |
|
137
|
46 |
|
private function loadSession() : SessionInterface |
|
138
|
|
|
{ |
|
139
|
46 |
|
$sessionLoader = $this->sessionLoader; |
|
140
|
|
|
|
|
141
|
46 |
|
return $sessionLoader(); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|