Memory.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\storage\session\adapter;
  9. use lithium\util\String;
  10. /**
  11. * Simple memory session storage engine. Used for testing.
  12. */
  13. class Memory extends \lithium\core\Object {
  14. /**
  15. * Holds the array that corresponds to session keys & values.
  16. *
  17. * @var array "Session" data.
  18. */
  19. public $_session = array();
  20. /**
  21. * Obtain the session key.
  22. *
  23. * For this adapter, it is a UUID.
  24. *
  25. * @return string UUID.
  26. */
  27. public static function key() {
  28. return String::uuid();
  29. }
  30. /**
  31. * The memory adapter session is always "on".
  32. *
  33. * @return boolean True.
  34. */
  35. public function isStarted() {
  36. return true;
  37. }
  38. /**
  39. * Checks if a value has been set in the session.
  40. *
  41. * @param string $key Key of the entry to be checked.
  42. * @param array $options Options array. Not used for this adapter method.
  43. * @return closure Function returning boolean `true` if the key exists, `false` otherwise.
  44. */
  45. public function check($key, array $options = array()) {
  46. $session =& $this->_session;
  47. return function($self, $params) use (&$session) {
  48. return isset($session[$params['key']]);
  49. };
  50. }
  51. /**
  52. * Read a value from the session.
  53. *
  54. * @param null|string $key Key of the entry to be read. If no key is passed, all
  55. * current session data is returned.
  56. * @param array $options Options array. Not used for this adapter method.
  57. * @return closure Function returning data in the session if successful, `false` otherwise.
  58. */
  59. public function read($key = null, array $options = array()) {
  60. $session = $this->_session;
  61. return function($self, $params) use ($session) {
  62. extract($params);
  63. if (!$key) {
  64. return $session;
  65. }
  66. return isset($session[$key]) ? $session[$key] : null;
  67. };
  68. }
  69. /**
  70. * Write a value to the session.
  71. *
  72. * @param string $key Key of the item to be stored.
  73. * @param mixed $value The value to be stored.
  74. * @param array $options Options array. Not used for this adapter method.
  75. * @return closure Function returning boolean `true` on successful write, `false` otherwise.
  76. */
  77. public function write($key, $value, array $options = array()) {
  78. $session =& $this->_session;
  79. return function($self, $params) use (&$session) {
  80. extract($params);
  81. return (boolean) ($session[$key] = $value);
  82. };
  83. }
  84. /**
  85. * Delete value from the session
  86. *
  87. * @param string $key The key to be deleted
  88. * @param array $options Options array. Not used for this adapter method.
  89. * @return closure Function returning boolean `true` on successful delete, `false` otherwise
  90. */
  91. public function delete($key, array $options = array()) {
  92. $session =& $this->_session;
  93. return function($self, $params) use (&$session) {
  94. extract($params);
  95. unset($session[$key]);
  96. return !isset($session[$key]);
  97. };
  98. }
  99. /**
  100. * Clears all keys from the session.
  101. *
  102. * @param array $options Options array. Not used for this adapter method.
  103. * @return closure Function that clears the session
  104. */
  105. public function clear(array $options = array()) {
  106. $session =& $this->_session;
  107. return function($self, $params) use (&$session) {
  108. $session = array();
  109. };
  110. }
  111. /**
  112. * This adapter is always enabled, as it has no external dependencies.
  113. *
  114. * @return boolean True
  115. */
  116. public static function enabled() {
  117. return true;
  118. }
  119. }
  120. ?>