PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zendframework/library/Zend/Session/Storage/AbstractSessionArrayStorage.php

https://bitbucket.org/zbahij/eprojets_app
PHP | 484 lines | 233 code | 58 blank | 193 comment | 38 complexity | 84ccabf874cf3c539e5197869d2b17ea MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  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. */
  9. namespace Zend\Session\Storage;
  10. use ArrayIterator;
  11. use IteratorAggregate;
  12. use Zend\Session\Exception;
  13. /**
  14. * Session storage in $_SESSION
  15. *
  16. * Replaces the $_SESSION superglobal with an ArrayObject that allows for
  17. * property access, metadata storage, locking, and immutability.
  18. */
  19. abstract class AbstractSessionArrayStorage implements IteratorAggregate, StorageInterface, StorageInitializationInterface
  20. {
  21. /**
  22. * Constructor
  23. *
  24. * @param array|null $input
  25. */
  26. public function __construct($input = null)
  27. {
  28. // this is here for B.C.
  29. $this->init($input);
  30. }
  31. /**
  32. * Initialize Storage
  33. *
  34. * @param array $input
  35. * @return void
  36. */
  37. public function init($input = null)
  38. {
  39. if ((null === $input) && isset($_SESSION)) {
  40. $input = $_SESSION;
  41. if (is_object($input) && !$_SESSION instanceof \ArrayObject) {
  42. $input = (array) $input;
  43. }
  44. } elseif (null === $input) {
  45. $input = array();
  46. }
  47. $_SESSION = $input;
  48. $this->setRequestAccessTime(microtime(true));
  49. }
  50. /**
  51. * Get Offset
  52. *
  53. * @param mixed $key
  54. * @return mixed
  55. */
  56. public function __get($key)
  57. {
  58. return $this->offsetGet($key);
  59. }
  60. /**
  61. * Set Offset
  62. *
  63. * @param mixed $key
  64. * @param mixed $value
  65. * @return void
  66. */
  67. public function __set($key, $value)
  68. {
  69. return $this->offsetSet($key, $value);
  70. }
  71. /**
  72. * Isset Offset
  73. *
  74. * @param mixed $key
  75. * @return bool
  76. */
  77. public function __isset($key)
  78. {
  79. return $this->offsetExists($key);
  80. }
  81. /**
  82. * Unset Offset
  83. *
  84. * @param mixed $key
  85. * @return void
  86. */
  87. public function __unset($key)
  88. {
  89. return $this->offsetUnset($key);
  90. }
  91. /**
  92. * Destructor
  93. *
  94. * @return void
  95. */
  96. public function __destruct()
  97. {
  98. return ;
  99. }
  100. /**
  101. * Offset Exists
  102. *
  103. * @param mixed $key
  104. * @return bool
  105. */
  106. public function offsetExists($key)
  107. {
  108. return isset($_SESSION[$key]);
  109. }
  110. /**
  111. * Offset Get
  112. *
  113. * @param mixed $key
  114. * @return mixed
  115. */
  116. public function offsetGet($key)
  117. {
  118. if (isset($_SESSION[$key])) {
  119. return $_SESSION[$key];
  120. }
  121. return null;
  122. }
  123. /**
  124. * Offset Set
  125. *
  126. * @param mixed $key
  127. * @param mixed $value
  128. * @return void
  129. */
  130. public function offsetSet($key, $value)
  131. {
  132. $_SESSION[$key] = $value;
  133. }
  134. /**
  135. * Offset Unset
  136. *
  137. * @param mixed $key
  138. * @return void
  139. */
  140. public function offsetUnset($key)
  141. {
  142. unset($_SESSION[$key]);
  143. }
  144. /**
  145. * Count
  146. *
  147. * @return int
  148. */
  149. public function count()
  150. {
  151. return count($_SESSION);
  152. }
  153. /**
  154. * Seralize
  155. *
  156. * @return string
  157. */
  158. public function serialize()
  159. {
  160. return serialize($_SESSION);
  161. }
  162. /**
  163. * Unserialize
  164. *
  165. * @param string $session
  166. * @return mixed
  167. */
  168. public function unserialize($session)
  169. {
  170. return unserialize($session);
  171. }
  172. /**
  173. * Get Iterator
  174. *
  175. * @return ArrayIterator
  176. */
  177. public function getIterator()
  178. {
  179. return new ArrayIterator($_SESSION);
  180. }
  181. /**
  182. * Load session object from an existing array
  183. *
  184. * Ensures $_SESSION is set to an instance of the object when complete.
  185. *
  186. * @param array $array
  187. * @return SessionStorage
  188. */
  189. public function fromArray(array $array)
  190. {
  191. $ts = $this->getRequestAccessTime();
  192. $_SESSION = $array;
  193. $this->setRequestAccessTime($ts);
  194. return $this;
  195. }
  196. /**
  197. * Mark object as isImmutable
  198. *
  199. * @return SessionStorage
  200. */
  201. public function markImmutable()
  202. {
  203. $_SESSION['_IMMUTABLE'] = true;
  204. return $this;
  205. }
  206. /**
  207. * Determine if this object is isImmutable
  208. *
  209. * @return bool
  210. */
  211. public function isImmutable()
  212. {
  213. return (isset($_SESSION['_IMMUTABLE']) && $_SESSION['_IMMUTABLE']);
  214. }
  215. /**
  216. * Lock this storage instance, or a key within it
  217. *
  218. * @param null|int|string $key
  219. * @return ArrayStorage
  220. */
  221. public function lock($key = null)
  222. {
  223. if (null === $key) {
  224. $this->setMetadata('_READONLY', true);
  225. return $this;
  226. }
  227. if (isset($_SESSION[$key])) {
  228. $this->setMetadata('_LOCKS', array($key => true));
  229. }
  230. return $this;
  231. }
  232. /**
  233. * Is the object or key marked as locked?
  234. *
  235. * @param null|int|string $key
  236. * @return bool
  237. */
  238. public function isLocked($key = null)
  239. {
  240. if ($this->isImmutable()) {
  241. // isImmutable trumps all
  242. return true;
  243. }
  244. if (null === $key) {
  245. // testing for global lock
  246. return $this->getMetadata('_READONLY');
  247. }
  248. $locks = $this->getMetadata('_LOCKS');
  249. $readOnly = $this->getMetadata('_READONLY');
  250. if ($readOnly && !$locks) {
  251. // global lock in play; all keys are locked
  252. return true;
  253. }
  254. if ($readOnly && $locks) {
  255. return array_key_exists($key, $locks);
  256. }
  257. // test for individual locks
  258. if (!$locks) {
  259. return false;
  260. }
  261. return array_key_exists($key, $locks);
  262. }
  263. /**
  264. * Unlock an object or key marked as locked
  265. *
  266. * @param null|int|string $key
  267. * @return ArrayStorage
  268. */
  269. public function unlock($key = null)
  270. {
  271. if (null === $key) {
  272. // Unlock everything
  273. $this->setMetadata('_READONLY', false);
  274. $this->setMetadata('_LOCKS', false);
  275. return $this;
  276. }
  277. $locks = $this->getMetadata('_LOCKS');
  278. if (!$locks) {
  279. if (!$this->getMetadata('_READONLY')) {
  280. return $this;
  281. }
  282. $array = $this->toArray();
  283. $keys = array_keys($array);
  284. $locks = array_flip($keys);
  285. unset($array, $keys);
  286. }
  287. if (array_key_exists($key, $locks)) {
  288. unset($locks[$key]);
  289. $this->setMetadata('_LOCKS', $locks, true);
  290. }
  291. return $this;
  292. }
  293. /**
  294. * Set storage metadata
  295. *
  296. * Metadata is used to store information about the data being stored in the
  297. * object. Some example use cases include:
  298. * - Setting expiry data
  299. * - Maintaining access counts
  300. * - localizing session storage
  301. * - etc.
  302. *
  303. * @param string $key
  304. * @param mixed $value
  305. * @param bool $overwriteArray Whether to overwrite or merge array values; by default, merges
  306. * @return ArrayStorage
  307. * @throws Exception\RuntimeException
  308. */
  309. public function setMetadata($key, $value, $overwriteArray = false)
  310. {
  311. if ($this->isImmutable()) {
  312. throw new Exception\RuntimeException(sprintf(
  313. 'Cannot set key "%s" as storage is marked isImmutable', $key
  314. ));
  315. }
  316. if (!isset($_SESSION['__ZF'])) {
  317. $_SESSION['__ZF'] = array();
  318. }
  319. if (isset($_SESSION['__ZF'][$key]) && is_array($value)) {
  320. if ($overwriteArray) {
  321. $_SESSION['__ZF'][$key] = $value;
  322. } else {
  323. $_SESSION['__ZF'][$key] = array_replace_recursive($_SESSION['__ZF'][$key], $value);
  324. }
  325. } else {
  326. if ((null === $value) && isset($_SESSION['__ZF'][$key])) {
  327. $array = $_SESSION['__ZF'];
  328. unset($array[$key]);
  329. $_SESSION['__ZF'] = $array;
  330. unset($array);
  331. } elseif (null !== $value) {
  332. $_SESSION['__ZF'][$key] = $value;
  333. }
  334. }
  335. return $this;
  336. }
  337. /**
  338. * Retrieve metadata for the storage object or a specific metadata key
  339. *
  340. * Returns false if no metadata stored, or no metadata exists for the given
  341. * key.
  342. *
  343. * @param null|int|string $key
  344. * @return mixed
  345. */
  346. public function getMetadata($key = null)
  347. {
  348. if (!isset($_SESSION['__ZF'])) {
  349. return false;
  350. }
  351. if (null === $key) {
  352. return $_SESSION['__ZF'];
  353. }
  354. if (!array_key_exists($key, $_SESSION['__ZF'])) {
  355. return false;
  356. }
  357. return $_SESSION['__ZF'][$key];
  358. }
  359. /**
  360. * Clear the storage object or a subkey of the object
  361. *
  362. * @param null|int|string $key
  363. * @return ArrayStorage
  364. * @throws Exception\RuntimeException
  365. */
  366. public function clear($key = null)
  367. {
  368. if ($this->isImmutable()) {
  369. throw new Exception\RuntimeException('Cannot clear storage as it is marked immutable');
  370. }
  371. if (null === $key) {
  372. $this->fromArray(array());
  373. return $this;
  374. }
  375. if (!isset($_SESSION[$key])) {
  376. return $this;
  377. }
  378. // Clear key data
  379. unset($_SESSION[$key]);
  380. // Clear key metadata
  381. $this->setMetadata($key, null)
  382. ->unlock($key);
  383. return $this;
  384. }
  385. /**
  386. * Retrieve the request access time
  387. *
  388. * @return float
  389. */
  390. public function getRequestAccessTime()
  391. {
  392. return $this->getMetadata('_REQUEST_ACCESS_TIME');
  393. }
  394. /**
  395. * Set the request access time
  396. *
  397. * @param float $time
  398. * @return ArrayStorage
  399. */
  400. protected function setRequestAccessTime($time)
  401. {
  402. $this->setMetadata('_REQUEST_ACCESS_TIME', $time);
  403. return $this;
  404. }
  405. /**
  406. * Cast the object to an array
  407. *
  408. * @param bool $metaData Whether to include metadata
  409. * @return array
  410. */
  411. public function toArray($metaData = false)
  412. {
  413. if (isset($_SESSION)) {
  414. $values = $_SESSION;
  415. } else {
  416. $values = array();
  417. }
  418. if ($metaData) {
  419. return $values;
  420. }
  421. if (isset($values['__ZF'])) {
  422. unset($values['__ZF']);
  423. }
  424. return $values;
  425. }
  426. }