Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Session / Storage / ArrayStorage.php
blob4c897fbafd4693a2753f973d00ab381097d3a29d
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Session\Storage;
12 use Zend\Stdlib\ArrayObject;
13 use Zend\Session\Exception;
15 /**
16 * Array session storage
18 * Defines an ArrayObject interface for accessing session storage, with options
19 * for setting metadata, locking, and marking as isImmutable.
21 class ArrayStorage extends ArrayObject implements StorageInterface
23 /**
24 * Is storage marked isImmutable?
25 * @var bool
27 protected $isImmutable = false;
29 /**
30 * Constructor
32 * Instantiates storage as an ArrayObject, allowing property access.
33 * Also sets the initial request access time.
35 * @param array $input
36 * @param int $flags
37 * @param string $iteratorClass
39 public function __construct(
40 $input = array(),
41 $flags = ArrayObject::ARRAY_AS_PROPS,
42 $iteratorClass = '\\ArrayIterator'
43 ) {
44 parent::__construct($input, $flags, $iteratorClass);
45 $this->setRequestAccessTime(microtime(true));
48 /**
49 * Set the request access time
51 * @param float $time
52 * @return ArrayStorage
54 protected function setRequestAccessTime($time)
56 $this->setMetadata('_REQUEST_ACCESS_TIME', $time);
58 return $this;
61 /**
62 * Retrieve the request access time
64 * @return float
66 public function getRequestAccessTime()
68 return $this->getMetadata('_REQUEST_ACCESS_TIME');
71 /**
72 * Set a value in the storage object
74 * If the object is marked as isImmutable, or the object or key is marked as
75 * locked, raises an exception.
77 * @param string $key
78 * @param mixed $value
79 * @return void
82 /**
83 * @param mixed $key
84 * @param mixed $value
85 * @throws Exception\RuntimeException
87 public function offsetSet($key, $value)
89 if ($this->isImmutable()) {
90 throw new Exception\RuntimeException(sprintf(
91 'Cannot set key "%s" as storage is marked isImmutable', $key
92 ));
94 if ($this->isLocked($key)) {
95 throw new Exception\RuntimeException(sprintf(
96 'Cannot set key "%s" due to locking', $key
97 ));
100 parent::offsetSet($key, $value);
104 * Lock this storage instance, or a key within it
106 * @param null|int|string $key
107 * @return ArrayStorage
109 public function lock($key = null)
111 if (null === $key) {
112 $this->setMetadata('_READONLY', true);
114 return $this;
116 if (isset($this[$key])) {
117 $this->setMetadata('_LOCKS', array($key => true));
120 return $this;
124 * Is the object or key marked as locked?
126 * @param null|int|string $key
127 * @return bool
129 public function isLocked($key = null)
131 if ($this->isImmutable()) {
132 // isImmutable trumps all
133 return true;
136 if (null === $key) {
137 // testing for global lock
138 return $this->getMetadata('_READONLY');
141 $locks = $this->getMetadata('_LOCKS');
142 $readOnly = $this->getMetadata('_READONLY');
144 if ($readOnly && !$locks) {
145 // global lock in play; all keys are locked
146 return true;
147 } elseif ($readOnly && $locks) {
148 return array_key_exists($key, $locks);
151 // test for individual locks
152 if (!$locks) {
153 return false;
156 return array_key_exists($key, $locks);
160 * Unlock an object or key marked as locked
162 * @param null|int|string $key
163 * @return ArrayStorage
165 public function unlock($key = null)
167 if (null === $key) {
168 // Unlock everything
169 $this->setMetadata('_READONLY', false);
170 $this->setMetadata('_LOCKS', false);
172 return $this;
175 $locks = $this->getMetadata('_LOCKS');
176 if (!$locks) {
177 if (!$this->getMetadata('_READONLY')) {
178 return $this;
180 $array = $this->toArray();
181 $keys = array_keys($array);
182 $locks = array_flip($keys);
183 unset($array, $keys);
186 if (array_key_exists($key, $locks)) {
187 unset($locks[$key]);
188 $this->setMetadata('_LOCKS', $locks, true);
191 return $this;
195 * Mark the storage container as isImmutable
197 * @return ArrayStorage
199 public function markImmutable()
201 $this->isImmutable = true;
203 return $this;
207 * Is the storage container marked as isImmutable?
209 * @return bool
211 public function isImmutable()
213 return $this->isImmutable;
217 * Set storage metadata
219 * Metadata is used to store information about the data being stored in the
220 * object. Some example use cases include:
221 * - Setting expiry data
222 * - Maintaining access counts
223 * - localizing session storage
224 * - etc.
226 * @param string $key
227 * @param mixed $value
228 * @param bool $overwriteArray Whether to overwrite or merge array values; by default, merges
229 * @return ArrayStorage
230 * @throws Exception\RuntimeException
232 public function setMetadata($key, $value, $overwriteArray = false)
234 if ($this->isImmutable) {
235 throw new Exception\RuntimeException(sprintf(
236 'Cannot set key "%s" as storage is marked isImmutable', $key
240 if (!isset($this['__ZF'])) {
241 $this['__ZF'] = array();
244 if (isset($this['__ZF'][$key]) && is_array($value)) {
245 if ($overwriteArray) {
246 $this['__ZF'][$key] = $value;
247 } else {
248 $this['__ZF'][$key] = array_replace_recursive($this['__ZF'][$key], $value);
250 } else {
251 if ((null === $value) && isset($this['__ZF'][$key])) {
252 // unset($this['__ZF'][$key]) led to "indirect modification...
253 // has no effect" errors, so explicitly pulling array and
254 // unsetting key.
255 $array = $this['__ZF'];
256 unset($array[$key]);
257 $this['__ZF'] = $array;
258 unset($array);
259 } elseif (null !== $value) {
260 $this['__ZF'][$key] = $value;
264 return $this;
268 * Retrieve metadata for the storage object or a specific metadata key
270 * Returns false if no metadata stored, or no metadata exists for the given
271 * key.
273 * @param null|int|string $key
274 * @return mixed
276 public function getMetadata($key = null)
278 if (!isset($this['__ZF'])) {
279 return false;
282 if (null === $key) {
283 return $this['__ZF'];
286 if (!array_key_exists($key, $this['__ZF'])) {
287 return false;
290 return $this['__ZF'][$key];
294 * Clear the storage object or a subkey of the object
296 * @param null|int|string $key
297 * @return ArrayStorage
298 * @throws Exception\RuntimeException
300 public function clear($key = null)
302 if ($this->isImmutable()) {
303 throw new Exception\RuntimeException('Cannot clear storage as it is marked immutable');
305 if (null === $key) {
306 $this->fromArray(array());
308 return $this;
311 if (!isset($this[$key])) {
312 return $this;
315 // Clear key data
316 unset($this[$key]);
318 // Clear key metadata
319 $this->setMetadata($key, null)
320 ->unlock($key);
322 return $this;
326 * Load the storage from another array
328 * Overwrites any data that was previously set.
330 * @param array $array
331 * @return ArrayStorage
333 public function fromArray(array $array)
335 $ts = $this->getRequestAccessTime();
336 $this->exchangeArray($array);
337 $this->setRequestAccessTime($ts);
339 return $this;
343 * Cast the object to an array
345 * @param bool $metaData Whether to include metadata
346 * @return array
348 public function toArray($metaData = false)
350 $values = $this->getArrayCopy();
351 if ($metaData) {
352 return $values;
354 if (isset($values['__ZF'])) {
355 unset($values['__ZF']);
358 return $values;