This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
/
ArrayStorage.php
353 lines (318 loc) · 8.82 KB
/
ArrayStorage.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Session
*/
namespace Zend\Session\Storage;
use ArrayObject;
use Zend\Session\Exception;
/**
* Array session storage
*
* Defines an ArrayObject interface for accessing session storage, with options
* for setting metadata, locking, and marking as isImmutable.
*
* @category Zend
* @package Zend_Session
* @subpackage Storage
*/
class ArrayStorage extends ArrayObject implements StorageInterface
{
/**
* Is storage marked isImmutable?
* @var bool
*/
protected $isImmutable = false;
/**
* Constructor
*
* Instantiates storage as an ArrayObject, allowing property access.
* Also sets the initial request access time.
*
* @param array $input
* @param int $flags
* @param string $iteratorClass
*/
public function __construct(
$input = array(),
$flags = ArrayObject::ARRAY_AS_PROPS,
$iteratorClass = '\\ArrayIterator'
) {
parent::__construct($input, $flags, $iteratorClass);
$this->setRequestAccessTime(microtime(true));
}
/**
* Set the request access time
*
* @param float $time
* @return ArrayStorage
*/
protected function setRequestAccessTime($time)
{
$this->setMetadata('_REQUEST_ACCESS_TIME', $time);
return $this;
}
/**
* Retrieve the request access time
*
* @return float
*/
public function getRequestAccessTime()
{
return $this->getMetadata('_REQUEST_ACCESS_TIME');
}
/**
* Set a value in the storage object
*
* If the object is marked as isImmutable, or the object or key is marked as
* locked, raises an exception.
*
* @param string $key
* @param mixed $value
* @return void
*/
/**
* @param mixed $key
* @param mixed $value
* @throws Exception\RuntimeException
*/
public function offsetSet($key, $value)
{
if ($this->isImmutable()) {
throw new Exception\RuntimeException(sprintf(
'Cannot set key "%s" as storage is marked isImmutable', $key
));
}
if ($this->isLocked($key)) {
throw new Exception\RuntimeException(sprintf(
'Cannot set key "%s" due to locking', $key
));
}
parent::offsetSet($key, $value);
}
/**
* Lock this storage instance, or a key within it
*
* @param null|int|string $key
* @return ArrayStorage
*/
public function lock($key = null)
{
if (null === $key) {
$this->setMetadata('_READONLY', true);
return $this;
}
if (isset($this[$key])) {
$this->setMetadata('_LOCKS', array($key => true));
}
return $this;
}
/**
* Is the object or key marked as locked?
*
* @param null|int|string $key
* @return bool
*/
public function isLocked($key = null)
{
if ($this->isImmutable()) {
// isImmutable trumps all
return true;
}
if (null === $key) {
// testing for global lock
return $this->getMetadata('_READONLY');
}
$locks = $this->getMetadata('_LOCKS');
$readOnly = $this->getMetadata('_READONLY');
if ($readOnly && !$locks) {
// global lock in play; all keys are locked
return true;
} elseif ($readOnly && $locks) {
return array_key_exists($key, $locks);
}
// test for individual locks
if (!$locks) {
return false;
}
return array_key_exists($key, $locks);
}
/**
* Unlock an object or key marked as locked
*
* @param null|int|string $key
* @return ArrayStorage
*/
public function unlock($key = null)
{
if (null === $key) {
// Unlock everything
$this->setMetadata('_READONLY', false);
$this->setMetadata('_LOCKS', false);
return $this;
}
$locks = $this->getMetadata('_LOCKS');
if (!$locks) {
if (!$this->getMetadata('_READONLY')) {
return $this;
}
$array = $this->toArray();
$keys = array_keys($array);
$locks = array_flip($keys);
unset($array, $keys);
}
if (array_key_exists($key, $locks)) {
unset($locks[$key]);
$this->setMetadata('_LOCKS', $locks, true);
}
return $this;
}
/**
* Mark the storage container as isImmutable
*
* @return ArrayStorage
*/
public function markImmutable()
{
$this->isImmutable = true;
return $this;
}
/**
* Is the storage container marked as isImmutable?
*
* @return bool
*/
public function isImmutable()
{
return $this->isImmutable;
}
/**
* Set storage metadata
*
* Metadata is used to store information about the data being stored in the
* object. Some example use cases include:
* - Setting expiry data
* - Maintaining access counts
* - localizing session storage
* - etc.
*
* @param string $key
* @param mixed $value
* @param bool $overwriteArray Whether to overwrite or merge array values; by default, merges
* @return ArrayStorage
* @throws Exception\RuntimeException
*/
public function setMetadata($key, $value, $overwriteArray = false)
{
if ($this->isImmutable) {
throw new Exception\RuntimeException(sprintf(
'Cannot set key "%s" as storage is marked isImmutable', $key
));
}
if (!isset($this['__ZF'])) {
$this['__ZF'] = array();
}
if (isset($this['__ZF'][$key]) && is_array($value)) {
if ($overwriteArray) {
$this['__ZF'][$key] = $value;
} else {
$this['__ZF'][$key] = array_replace_recursive($this['__ZF'][$key], $value);
}
} else {
if ((null === $value) && isset($this['__ZF'][$key])) {
// unset($this['__ZF'][$key]) led to "indirect modification...
// has no effect" errors, so explicitly pulling array and
// unsetting key.
$array = $this['__ZF'];
unset($array[$key]);
$this['__ZF'] = $array;
unset($array);
} elseif (null !== $value) {
$this['__ZF'][$key] = $value;
}
}
return $this;
}
/**
* Retrieve metadata for the storage object or a specific metadata key
*
* Returns false if no metadata stored, or no metadata exists for the given
* key.
*
* @param null|int|string $key
* @return mixed
*/
public function getMetadata($key = null)
{
if (!isset($this['__ZF'])) {
return false;
}
if (null === $key) {
return $this['__ZF'];
}
if (!array_key_exists($key, $this['__ZF'])) {
return false;
}
return $this['__ZF'][$key];
}
/**
* Clear the storage object or a subkey of the object
*
* @param null|int|string $key
* @return ArrayStorage
* @throws Exception\RuntimeException
*/
public function clear($key = null)
{
if ($this->isImmutable()) {
throw new Exception\RuntimeException('Cannot clear storage as it is marked immutable');
}
if (null === $key) {
$this->fromArray(array());
return $this;
}
if (!isset($this[$key])) {
return $this;
}
// Clear key data
unset($this[$key]);
// Clear key metadata
$this->setMetadata($key, null)
->unlock($key);
return $this;
}
/**
* Load the storage from another array
*
* Overwrites any data that was previously set.
*
* @param array $array
* @return ArrayStorage
*/
public function fromArray(array $array)
{
$ts = $this->getRequestAccessTime();
$this->exchangeArray($array);
$this->setRequestAccessTime($ts);
return $this;
}
/**
* Cast the object to an array
*
* Returns data only, no metadata.
*
* @return array
*/
public function toArray()
{
$values = $this->getArrayCopy();
if (isset($values['__ZF'])) {
unset($values['__ZF']);
}
return $values;
}
}