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 PSR7Sessions\Storageless\Session; |
22
|
|
|
|
23
|
|
|
final class LazySession implements SessionInterface |
24
|
|
|
{ |
25
|
|
|
/** @internal do not access directly: use {@see LazySession::getRealSession} instead */ |
26
|
|
|
private ?SessionInterface $realSession = null; |
|
|
|
|
27
|
|
|
|
28
|
|
|
/** @var callable */ |
29
|
|
|
private $sessionLoader; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Instantiation via __construct is not allowed, use {@see LazySession::fromContainerBuildingCallback} instead |
33
|
|
|
*/ |
34
|
58 |
|
private function __construct() |
35
|
|
|
{ |
36
|
58 |
|
} |
37
|
|
|
|
38
|
58 |
|
public static function fromContainerBuildingCallback(callable $sessionLoader) : self |
39
|
|
|
{ |
40
|
58 |
|
$instance = new self(); |
41
|
|
|
|
42
|
58 |
|
$instance->sessionLoader = $sessionLoader; |
43
|
|
|
|
44
|
58 |
|
return $instance; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritDoc} |
49
|
|
|
*/ |
50
|
22 |
|
public function set(string $key, $value) : void |
51
|
|
|
{ |
52
|
22 |
|
$this->getRealSession()->set($key, $value); |
53
|
22 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritDoc} |
57
|
|
|
*/ |
58
|
7 |
|
public function get(string $key, $default = null) |
59
|
|
|
{ |
60
|
7 |
|
return $this->getRealSession()->get($key, $default); |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
public function remove(string $key) : void |
64
|
|
|
{ |
65
|
2 |
|
$this->getRealSession()->remove($key); |
66
|
2 |
|
} |
67
|
|
|
|
68
|
4 |
|
public function clear() : void |
69
|
|
|
{ |
70
|
4 |
|
$this->getRealSession()->clear(); |
71
|
4 |
|
} |
72
|
|
|
|
73
|
1 |
|
public function has(string $key) : bool |
74
|
|
|
{ |
75
|
1 |
|
return $this->getRealSession()->has($key); |
76
|
|
|
} |
77
|
|
|
|
78
|
51 |
|
public function hasChanged() : bool |
79
|
|
|
{ |
80
|
51 |
|
return $this->realSession && $this->realSession->hasChanged(); |
81
|
|
|
} |
82
|
|
|
|
83
|
45 |
|
public function isEmpty() : bool |
84
|
|
|
{ |
85
|
45 |
|
return $this->getRealSession()->isEmpty(); |
86
|
|
|
} |
87
|
|
|
|
88
|
27 |
|
public function jsonSerialize() : object |
89
|
|
|
{ |
90
|
27 |
|
return $this->getRealSession()->jsonSerialize(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get or initialize the session |
95
|
|
|
*/ |
96
|
52 |
|
private function getRealSession() : SessionInterface |
97
|
|
|
{ |
98
|
52 |
|
return $this->realSession ?? $this->realSession = $this->loadSession(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Type-safe wrapper that ensures that the given callback returns the expected type of object, when called |
103
|
|
|
*/ |
104
|
52 |
|
private function loadSession() : SessionInterface |
105
|
|
|
{ |
106
|
52 |
|
$sessionLoader = $this->sessionLoader; |
107
|
|
|
|
108
|
52 |
|
return $sessionLoader(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|