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\Zend; |
22
|
|
|
|
23
|
|
|
use Lcobucci\Clock\Clock; |
24
|
|
|
use Lcobucci\Clock\SystemClock; |
25
|
|
|
use PSR7Sessions\Storageless\Session\SessionInterface; |
26
|
|
|
use Zend\Expressive\Session\SessionInterface as ZendSessionInterface; |
27
|
|
|
|
28
|
|
|
final class SessionAdapter implements ZendSessionInterface |
29
|
|
|
{ |
30
|
|
|
private const SESSION_REGENERATED_NAME = '_regenerated'; |
31
|
|
|
|
32
|
|
|
/** @var SessionInterface */ |
33
|
|
|
private $session; |
34
|
|
|
|
35
|
|
|
/** @var Clock */ |
36
|
|
|
private $clock; |
37
|
|
|
|
38
|
|
|
public function __construct(SessionInterface $session, ?Clock $clock = null) |
39
|
|
|
{ |
40
|
|
|
$this->session = $session; |
41
|
|
|
$this->clock = $clock ?? new SystemClock(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
public function toArray() : array |
48
|
|
|
{ |
49
|
|
|
return (array) $this->session->jsonSerialize(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** {@inheritDoc} */ |
53
|
|
|
public function get(string $name, $default = null) |
54
|
|
|
{ |
55
|
|
|
return $this->session->get($name, $default); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function has(string $name) : bool |
59
|
|
|
{ |
60
|
|
|
return $this->session->has($name); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** {@inheritDoc} */ |
64
|
|
|
public function set(string $name, $value) : void |
65
|
|
|
{ |
66
|
|
|
$this->session->set($name, $value); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function unset(string $name) : void |
70
|
|
|
{ |
71
|
|
|
$this->session->remove($name); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function clear() : void |
75
|
|
|
{ |
76
|
|
|
$this->session->clear(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function hasChanged() : bool |
80
|
|
|
{ |
81
|
|
|
return $this->session->hasChanged(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function regenerate() : ZendSessionInterface |
85
|
|
|
{ |
86
|
|
|
$this->session->set(self::SESSION_REGENERATED_NAME, $this->timestamp()); |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function isRegenerated() : bool |
92
|
|
|
{ |
93
|
|
|
return $this->session->has(self::SESSION_REGENERATED_NAME); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function timestamp() : int |
97
|
|
|
{ |
98
|
|
|
return $this->clock->now()->getTimestamp(); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|