Session.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ 8EA+/uBGYPSnOrrVYH+ZcSO0hheeDEEXbE7r4V/0Uyw=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2014 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.0
  11. */
  12. namespace Cube\Authentication\Storage;
  13. use Cube\Controller\Front,
  14. Cube\Session as SessionObject;
  15. class Session implements StorageInterface
  16. {
  17. const SESSION_NAMESPACE = 'CubeAuthentication';
  18. const DEFAULT_KEY = 'AuthStorage';
  19. /**
  20. *
  21. * session namespace
  22. *
  23. * @var string
  24. */
  25. protected $_namespace;
  26. /**
  27. *
  28. * session array key that will hold the auth storage
  29. *
  30. * @var string
  31. */
  32. protected $_key;
  33. /**
  34. *
  35. * session object
  36. *
  37. * @var \Cube\Session
  38. */
  39. protected $_session;
  40. /**
  41. *
  42. * class constructor
  43. *
  44. * @param string $namespace
  45. * @param string $key
  46. * @param \Cube\Session $session
  47. */
  48. public function __construct($namespace = null, $key = null, SessionObject $session = null)
  49. {
  50. $this->setNamespace($namespace)
  51. ->setKey($key)
  52. ->setSession($session);
  53. }
  54. /**
  55. *
  56. * get session namespace
  57. *
  58. * @return string
  59. */
  60. public function getNamespace()
  61. {
  62. return $this->_namespace;
  63. }
  64. /**
  65. *
  66. * set session namespace
  67. *
  68. * @param string $namespace
  69. *
  70. * @return $this
  71. */
  72. public function setNamespace($namespace = null)
  73. {
  74. if ($namespace === null) {
  75. $namespace = self::SESSION_NAMESPACE;
  76. }
  77. $this->_namespace = $namespace;
  78. return $this;
  79. }
  80. /**
  81. *
  82. * get auth storage key
  83. *
  84. * @return string
  85. */
  86. public function getKey()
  87. {
  88. return $this->_key;
  89. }
  90. /**
  91. *
  92. * set auth storage key
  93. *
  94. * @param string $key
  95. *
  96. * @return $this
  97. */
  98. public function setKey($key = null)
  99. {
  100. if ($key === null) {
  101. $key = self::DEFAULT_KEY;
  102. }
  103. $this->_key = $key;
  104. return $this;
  105. }
  106. /**
  107. *
  108. * get session object
  109. *
  110. * @return \Cube\Session
  111. */
  112. public function getSession()
  113. {
  114. if (!($this->_session instanceof Session)) {
  115. $this->setSession();
  116. }
  117. return $this->_session;
  118. }
  119. /**
  120. *
  121. * set session object
  122. *
  123. * @param \Cube\Session $session
  124. *
  125. * @return $this
  126. */
  127. public function setSession(SessionObject $session = null)
  128. {
  129. if ($session === null) {
  130. $session = Front::getInstance()->getBootstrap()->getResource('session');
  131. }
  132. if (!$session instanceof SessionObject) {
  133. $session = new SessionObject();
  134. $session->setNamespace(
  135. $this->getNamespace());
  136. }
  137. $this->_session = $session;
  138. return $this;
  139. }
  140. /**
  141. *
  142. * clear storage
  143. *
  144. * @return $this
  145. */
  146. public function clear()
  147. {
  148. $this->_session->unregister($this->_key);
  149. return $this;
  150. }
  151. /**
  152. *
  153. * check if storage is empty and return true if it is
  154. *
  155. * @return bool
  156. */
  157. public function isEmpty()
  158. {
  159. $data = $this->_session->{$this->_key};
  160. return !isset($data);
  161. }
  162. /**
  163. *
  164. * return storage contents
  165. *
  166. * @return mixed
  167. */
  168. public function read()
  169. {
  170. return $this->_session->{$this->_key};
  171. }
  172. /**
  173. *
  174. * set storage contents
  175. *
  176. * @param mixed $contents
  177. *
  178. * @return $this
  179. */
  180. public function write($contents)
  181. {
  182. $this->_session->{$this->_key} = $contents;
  183. return $this;
  184. }
  185. }