PageRenderTime 35ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/session/Session.php

https://gitlab.com/igorbabko/mindk
PHP | 207 lines | 123 code | 18 blank | 66 comment | 13 complexity | c67a20dfd7440ecd59ece152dc99abf8 MD5 | raw file
  1. <?php
  2. /**
  3. * File /framework/session/Session.php contains Session class to easily manipulate
  4. * with session variables.
  5. *
  6. * PHP version 5
  7. *
  8. * @package Framework\Session
  9. * @author Igor Babko <i.i.babko@gmail.com>
  10. */
  11. namespace Framework\Session;
  12. use Framework\Exception\SessionException;
  13. /**
  14. * Class Session represents objects to work with sessions.
  15. * Default implementation of {@link SessionInterface}.
  16. *
  17. * @package Framework\Session
  18. * @author Igor Babko <i.i.babko@gmail.com>
  19. */
  20. class Session implements SessionInterface
  21. {
  22. /**
  23. * @static
  24. * @var Session|null $_instance Session instance
  25. */
  26. private static $_instance = null;
  27. /**
  28. * @var string $_meta Session variable that keeps general information of current session
  29. */
  30. private $_meta = '__meta';
  31. /**
  32. * @var bool $_started Is session started?
  33. */
  34. private $_started = false;
  35. /**
  36. * Session constructor.
  37. *
  38. * @return \Framework\Session\Session Session object.
  39. */
  40. private function __construct()
  41. {
  42. }
  43. /**
  44. * Method to clone objects of its class.
  45. *
  46. * @return \Framework\Session\Session Session instance.
  47. */
  48. private function __clone()
  49. {
  50. }
  51. /**
  52. * Method returns Session instance creating it if it's not been instantiated before
  53. * otherwise existed Session object will be returned.
  54. *
  55. * @return \Framework\Session\Session Session instance.
  56. */
  57. public static function getInstance()
  58. {
  59. if (null === self::$_instance) {
  60. self::$_instance = new self();
  61. }
  62. return self::$_instance;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function isStarted()
  68. {
  69. return $this->_started;
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function start()
  75. {
  76. if ($this->_started === true) {
  77. return;
  78. }
  79. session_start();
  80. if (!isset($_SESSION[$this->_meta])) {
  81. $this->init();
  82. } else {
  83. $_SESSION[$this->_meta]['activity'] = $_SERVER['REQUEST_TIME'];
  84. }
  85. $this->_started = true;
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function destroy()
  91. {
  92. $_SESSION = array();
  93. if (ini_get("session.use_cookies")) {
  94. $params = session_get_cookie_params();
  95. setcookie(
  96. session_name(),
  97. '',
  98. time() - 42000,
  99. $params["path"],
  100. $params["domain"],
  101. $params["secure"],
  102. $params["httponly"]
  103. );
  104. }
  105. session_destroy();
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function getName()
  111. {
  112. return session_name();
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function init()
  118. {
  119. $_SESSION[$this->_meta] = array(
  120. 'ip' => $_SERVER['REMOTE_ADDR'],
  121. 'name' => session_name(),
  122. 'created' => $_SERVER['REQUEST_TIME'],
  123. 'activity' => $_SERVER['REQUEST_TIME'],
  124. );
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function exists($name)
  130. {
  131. if ($this->_started === true) {
  132. return isset($_SESSION[$name]);
  133. } else {
  134. throw new SessionException(500, "<strong>Internal server error:</strong> session isn't started.");
  135. }
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. public function get($name)
  141. {
  142. if ($this->_started === true) {
  143. return isset($_SESSION[$name])?$_SESSION[$name]:null;
  144. } else {
  145. throw new SessionException(500, "<strong>Internal server error:</strong> session isn't started.");
  146. }
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. public function add($name, $value)
  152. {
  153. if ($this->_started === true) {
  154. $_SESSION[$name] = $value;
  155. return $value;
  156. } else {
  157. throw new SessionException(500, "<strong>Internal server error:</strong> session isn't started.");
  158. }
  159. }
  160. /**
  161. * {@inheritdoc}
  162. */
  163. public function remove($name)
  164. {
  165. if ($this->_started !== true) {
  166. throw new SessionException(500, "<strong>Internal server error:</strong> session isn't started.");
  167. } else {
  168. unset($_SESSION[$name]);
  169. }
  170. }
  171. /**
  172. * {@inheritdoc}
  173. */
  174. public function flash($name, $value)
  175. {
  176. if (!is_string($value)) {
  177. $parameterType = gettype($value);
  178. throw new SessionException(
  179. 500, "<strong>Internal server error:</strong> second parameter for Session::flash method must be 'string', '$parameterType' is given"
  180. );
  181. } else {
  182. $flashMsgs = $this->exists('flashMsgs')?$this->get('flashMsgs'):array();
  183. $flashMsgs[$name] = $value;
  184. $this->add('flashMsgs', $flashMsgs);
  185. }
  186. }
  187. }