This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
/
Session.php
171 lines (148 loc) · 3.9 KB
/
Session.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* @see https://github.com/zendframework/zend-expressive-session for the canonical source repository
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-expressive-session/blob/master/LICENSE.md New BSD License
*/
declare(strict_types=1);
namespace Zend\Expressive\Session;
class Session implements
SessionCookiePersistenceInterface,
SessionIdentifierAwareInterface,
SessionInterface
{
/**
* Current data within the session.
*
* @var array
*/
private $data;
/**
* The session identifier, if any.
*
* This is present in the session to allow the session persistence
* implementation to be stateless. When present here, we can query for it
* when it is time to persist the session, instead of relying on state in
* the persistence instance (which may be shared between multiple
* requests).
*
* @var string
*/
private $id;
/**
* @var bool
*/
private $isRegenerated = false;
/**
* Original data provided to the constructor.
*
* @var array
*/
private $originalData;
/**
* Lifetime of the session cookie.
*
* @var int
*/
private $sessionLifetime = 0;
public function __construct(array $data, string $id = '')
{
$this->data = $this->originalData = $data;
$this->id = $id;
if (isset($data[SessionCookiePersistenceInterface::SESSION_LIFETIME_KEY])) {
$this->sessionLifetime = $data[SessionCookiePersistenceInterface::SESSION_LIFETIME_KEY];
}
}
/**
* Convert a value to a JSON-serializable value.
*
* This value should be used by `set()` operations to ensure that the values
* within a session are serializable across any session adapter.
*
* @param mixed $value
* @return null|bool|int|float|string|array|\stdClass
*/
public static function extractSerializableValue($value)
{
return json_decode(json_encode($value, \JSON_PRESERVE_ZERO_FRACTION), true);
}
/**
* Retrieve all data for purposes of persistence.
*/
public function toArray() : array
{
return $this->data;
}
/**
* @param mixed $default Default value to return if $name does not exist.
* @return mixed
*/
public function get(string $name, $default = null)
{
return $this->data[$name] ?? $default;
}
public function has(string $name) : bool
{
return array_key_exists($name, $this->data);
}
/**
* @param mixed $value
*/
public function set(string $name, $value) : void
{
$this->data[$name] = self::extractSerializableValue($value);
}
public function unset(string $name) : void
{
unset($this->data[$name]);
}
public function clear() : void
{
$this->data = [];
}
public function hasChanged() : bool
{
if ($this->isRegenerated) {
return true;
}
return $this->data !== $this->originalData;
}
public function regenerate() : SessionInterface
{
$session = clone $this;
$session->isRegenerated = true;
return $session;
}
public function isRegenerated() : bool
{
return $this->isRegenerated;
}
/**
* {@inheritDoc}
*
* @since 1.1.0
*/
public function getId() : string
{
return $this->id;
}
/**
* {@inheritDoc}
*
* @since 1.2.0
*/
public function persistSessionFor(int $duration) : void
{
$this->sessionLifetime = $duration;
$this->set(SessionCookiePersistenceInterface::SESSION_LIFETIME_KEY, $duration);
}
/**
* {@inheritDoc}
*
* @since 1.2.0
*/
public function getSessionLifetime() : int
{
return $this->sessionLifetime;
}
}