|
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
|
|
|
public 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
|
|
|
/** |
|
53
|
|
|
* @param mixed $default |
|
54
|
|
|
* @return mixed |
|
55
|
|
|
*/ |
|
56
|
|
|
public function get(string $name, $default = null) |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->session->get($name, $default); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function has(string $name) : bool |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->session->has($name); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param mixed $value |
|
68
|
|
|
*/ |
|
69
|
|
|
public function set(string $name, $value) : void |
|
70
|
|
|
{ |
|
71
|
|
|
$this->session->set($name, $value); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function unset(string $name) : void |
|
75
|
|
|
{ |
|
76
|
|
|
$this->session->remove($name); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function clear() : void |
|
80
|
|
|
{ |
|
81
|
|
|
$this->session->clear(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function hasChanged() : bool |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->session->hasChanged(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function regenerate() : ZendSessionInterface |
|
90
|
|
|
{ |
|
91
|
|
|
$this->session->set(self::SESSION_REGENERATED_NAME, $this->timestamp()); |
|
92
|
|
|
|
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function isRegenerated() : bool |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->session->has(self::SESSION_REGENERATED_NAME); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
private function timestamp() : int |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->clock->now()->getTimestamp(); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|