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 / AbstractSessionArrayStorage.php
blob984bd49f960af6a8f91586590a7118d9876e8d42
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 ArrayIterator;
13 use IteratorAggregate;
14 use Zend\Session\Exception;
16 /**
17 * Session storage in $_SESSION
19 * Replaces the $_SESSION superglobal with an ArrayObject that allows for
20 * property access, metadata storage, locking, and immutability.
22 abstract class AbstractSessionArrayStorage implements
23 IteratorAggregate,
24 StorageInterface,
25 StorageInitializationInterface
27 /**
28 * Constructor
30 * @param array|null $input
32 public function __construct($input = null)
34 // this is here for B.C.
35 $this->init($input);
39 /**
40 * Initialize Storage
42 * @param array $input
43 * @return void
45 public function init($input = null)
47 if ((null === $input) && isset($_SESSION)) {
48 $input = $_SESSION;
49 if (is_object($input) && !$_SESSION instanceof \ArrayObject) {
50 $input = (array) $input;
52 } elseif (null === $input) {
53 $input = array();
55 $_SESSION = $input;
56 $this->setRequestAccessTime(microtime(true));
59 /**
60 * Get Offset
62 * @param mixed $key
63 * @return mixed
65 public function __get($key)
67 return $this->offsetGet($key);
70 /**
71 * Set Offset
73 * @param mixed $key
74 * @param mixed $value
75 * @return void
77 public function __set($key, $value)
79 return $this->offsetSet($key, $value);
82 /**
83 * Isset Offset
85 * @param mixed $key
86 * @return bool
88 public function __isset($key)
90 return $this->offsetExists($key);
93 /**
94 * Unset Offset
96 * @param mixed $key
97 * @return void
99 public function __unset($key)
101 return $this->offsetUnset($key);
105 * Destructor
107 * @return void
109 public function __destruct()
111 return ;
115 * Offset Exists
117 * @param mixed $key
118 * @return bool
120 public function offsetExists($key)
122 return isset($_SESSION[$key]);
126 * Offset Get
128 * @param mixed $key
129 * @return mixed
131 public function offsetGet($key)
133 if (isset($_SESSION[$key])) {
134 return $_SESSION[$key];
137 return null;
141 * Offset Set
143 * @param mixed $key
144 * @param mixed $value
145 * @return void
147 public function offsetSet($key, $value)
149 $_SESSION[$key] = $value;
153 * Offset Unset
155 * @param mixed $key
156 * @return void
158 public function offsetUnset($key)
160 unset($_SESSION[$key]);
164 * Count
166 * @return int
168 public function count()
170 return count($_SESSION);
174 * Seralize
176 * @return string
178 public function serialize()
180 return serialize($_SESSION);
184 * Unserialize
186 * @param string $session
187 * @return mixed
189 public function unserialize($session)
191 return unserialize($session);
195 * Get Iterator
197 * @return ArrayIterator
199 public function getIterator()
201 return new ArrayIterator($_SESSION);
205 * Load session object from an existing array
207 * Ensures $_SESSION is set to an instance of the object when complete.
209 * @param array $array
210 * @return SessionStorage
212 public function fromArray(array $array)
214 $ts = $this->getRequestAccessTime();
215 $_SESSION = $array;
216 $this->setRequestAccessTime($ts);
218 return $this;
222 * Mark object as isImmutable
224 * @return SessionStorage
226 public function markImmutable()
228 $_SESSION['_IMMUTABLE'] = true;
230 return $this;
234 * Determine if this object is isImmutable
236 * @return bool
238 public function isImmutable()
240 return (isset($_SESSION['_IMMUTABLE']) && $_SESSION['_IMMUTABLE']);
244 * Lock this storage instance, or a key within it
246 * @param null|int|string $key
247 * @return ArrayStorage
249 public function lock($key = null)
251 if (null === $key) {
252 $this->setMetadata('_READONLY', true);
254 return $this;
256 if (isset($_SESSION[$key])) {
257 $this->setMetadata('_LOCKS', array($key => true));
260 return $this;
264 * Is the object or key marked as locked?
266 * @param null|int|string $key
267 * @return bool
269 public function isLocked($key = null)
271 if ($this->isImmutable()) {
272 // isImmutable trumps all
273 return true;
276 if (null === $key) {
277 // testing for global lock
278 return $this->getMetadata('_READONLY');
281 $locks = $this->getMetadata('_LOCKS');
282 $readOnly = $this->getMetadata('_READONLY');
284 if ($readOnly && !$locks) {
285 // global lock in play; all keys are locked
286 return true;
288 if ($readOnly && $locks) {
289 return array_key_exists($key, $locks);
292 // test for individual locks
293 if (!$locks) {
294 return false;
297 return array_key_exists($key, $locks);
301 * Unlock an object or key marked as locked
303 * @param null|int|string $key
304 * @return ArrayStorage
306 public function unlock($key = null)
308 if (null === $key) {
309 // Unlock everything
310 $this->setMetadata('_READONLY', false);
311 $this->setMetadata('_LOCKS', false);
313 return $this;
316 $locks = $this->getMetadata('_LOCKS');
317 if (!$locks) {
318 if (!$this->getMetadata('_READONLY')) {
319 return $this;
321 $array = $this->toArray();
322 $keys = array_keys($array);
323 $locks = array_flip($keys);
324 unset($array, $keys);
327 if (array_key_exists($key, $locks)) {
328 unset($locks[$key]);
329 $this->setMetadata('_LOCKS', $locks, true);
332 return $this;
336 * Set storage metadata
338 * Metadata is used to store information about the data being stored in the
339 * object. Some example use cases include:
340 * - Setting expiry data
341 * - Maintaining access counts
342 * - localizing session storage
343 * - etc.
345 * @param string $key
346 * @param mixed $value
347 * @param bool $overwriteArray Whether to overwrite or merge array values; by default, merges
348 * @return ArrayStorage
349 * @throws Exception\RuntimeException
351 public function setMetadata($key, $value, $overwriteArray = false)
353 if ($this->isImmutable()) {
354 throw new Exception\RuntimeException(sprintf(
355 'Cannot set key "%s" as storage is marked isImmutable', $key
359 if (!isset($_SESSION['__ZF'])) {
360 $_SESSION['__ZF'] = array();
362 if (isset($_SESSION['__ZF'][$key]) && is_array($value)) {
363 if ($overwriteArray) {
364 $_SESSION['__ZF'][$key] = $value;
365 } else {
366 $_SESSION['__ZF'][$key] = array_replace_recursive($_SESSION['__ZF'][$key], $value);
368 } else {
369 if ((null === $value) && isset($_SESSION['__ZF'][$key])) {
370 $array = $_SESSION['__ZF'];
371 unset($array[$key]);
372 $_SESSION['__ZF'] = $array;
373 unset($array);
374 } elseif (null !== $value) {
375 $_SESSION['__ZF'][$key] = $value;
379 return $this;
383 * Retrieve metadata for the storage object or a specific metadata key
385 * Returns false if no metadata stored, or no metadata exists for the given
386 * key.
388 * @param null|int|string $key
389 * @return mixed
391 public function getMetadata($key = null)
393 if (!isset($_SESSION['__ZF'])) {
394 return false;
397 if (null === $key) {
398 return $_SESSION['__ZF'];
401 if (!array_key_exists($key, $_SESSION['__ZF'])) {
402 return false;
405 return $_SESSION['__ZF'][$key];
409 * Clear the storage object or a subkey of the object
411 * @param null|int|string $key
412 * @return ArrayStorage
413 * @throws Exception\RuntimeException
415 public function clear($key = null)
417 if ($this->isImmutable()) {
418 throw new Exception\RuntimeException('Cannot clear storage as it is marked immutable');
420 if (null === $key) {
421 $this->fromArray(array());
423 return $this;
426 if (!isset($_SESSION[$key])) {
427 return $this;
430 // Clear key data
431 unset($_SESSION[$key]);
433 // Clear key metadata
434 $this->setMetadata($key, null)
435 ->unlock($key);
437 return $this;
441 * Retrieve the request access time
443 * @return float
445 public function getRequestAccessTime()
447 return $this->getMetadata('_REQUEST_ACCESS_TIME');
451 * Set the request access time
453 * @param float $time
454 * @return ArrayStorage
456 protected function setRequestAccessTime($time)
458 $this->setMetadata('_REQUEST_ACCESS_TIME', $time);
460 return $this;
464 * Cast the object to an array
466 * @param bool $metaData Whether to include metadata
467 * @return array
469 public function toArray($metaData = false)
471 if (isset($_SESSION)) {
472 $values = $_SESSION;
473 } else {
474 $values = array();
477 if ($metaData) {
478 return $values;
481 if (isset($values['__ZF'])) {
482 unset($values['__ZF']);
485 return $values;