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
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Session\Storage;
use Zend\Stdlib\ArrayObject;
/**
* Session storage in $_SESSION
*
* Replaces the $_SESSION superglobal with an ArrayObject that allows for
* property access, metadata storage, locking, and immutability.
*/
class SessionStorage extends ArrayStorage
{
/**
* Constructor
*
* Sets the $_SESSION superglobal to an ArrayObject, maintaining previous
* values if any discovered.
*
* @param array|null $input
* @param int $flags
* @param string $iteratorClass
*/
public function __construct($input = null, $flags = ArrayObject::ARRAY_AS_PROPS, $iteratorClass = '\\ArrayIterator')
{
$resetSession = true;
if ((null === $input) && isset($_SESSION)) {
$input = $_SESSION;
if (is_object($input) && $_SESSION instanceof ArrayObject) {
$resetSession = false;
} elseif (is_object($input) && ! $_SESSION instanceof ArrayObject) {
$input = (array) $input;
}
} elseif (null === $input) {
$input = [];
}
parent::__construct($input, $flags, $iteratorClass);
if ($resetSession) {
$_SESSION = $this;
}
}
/**
* Destructor
*
* Resets $_SESSION superglobal to an array, by casting object using
* getArrayCopy().
*
* @return void
*/
public function __destruct()
{
$_SESSION = (array) $this->getArrayCopy();
}
/**
* Load session object from an existing array
*
* Ensures $_SESSION is set to an instance of the object when complete.
*
* @param array $array
* @return SessionStorage
*/
public function fromArray(array $array)
{
parent::fromArray($array);
if ($_SESSION !== $this) {
$_SESSION = $this;
}
return $this;
}
/**
* Mark object as isImmutable
*
* @return SessionStorage
*/
public function markImmutable()
{
$this['_IMMUTABLE'] = true;
return $this;
}
/**
* Determine if this object is isImmutable
*
* @return bool
*/
public function isImmutable()
{
return (isset($this['_IMMUTABLE']) && $this['_IMMUTABLE']);
}
}