PageRenderTime 42ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Classes/TYPO3/FLOW3/Session/TransientSession.php

https://github.com/christianjul/FLOW3-Composer
PHP | 178 lines | 53 code | 20 blank | 105 comment | 6 complexity | a31f42a5f48f6ea5dc525d9b676ad47f MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. namespace TYPO3\FLOW3\Session;
  3. /* *
  4. * This script belongs to the FLOW3 framework. *
  5. * *
  6. * It is free software; you can redistribute it and/or modify it under *
  7. * the terms of the GNU Lesser General Public License, either version 3 *
  8. * of the License, or (at your option) any later version. *
  9. * *
  10. * The TYPO3 project - inspiring people to share! *
  11. * */
  12. use TYPO3\FLOW3\Annotations as FLOW3;
  13. /**
  14. * Implementation of a transient session.
  15. *
  16. * This session behaves like any other session except that it only stores the
  17. * data during one request.
  18. *
  19. * @FLOW3\Scope("singleton")
  20. */
  21. class TransientSession implements \TYPO3\FLOW3\Session\SessionInterface {
  22. /**
  23. * The session Id
  24. *
  25. * @var string
  26. */
  27. protected $sessionId;
  28. /**
  29. * If this session has been started
  30. *
  31. * @var boolean
  32. */
  33. protected $started = FALSE;
  34. /**
  35. * The session data
  36. *
  37. * @var array
  38. */
  39. protected $data = array();
  40. /**
  41. * Tells if the session has been started already.
  42. *
  43. * @return boolean
  44. */
  45. public function isStarted() {
  46. return $this->started;
  47. }
  48. /**
  49. * Starts the session, if it has not been already started
  50. *
  51. * @return void
  52. */
  53. public function start() {
  54. $this->sessionId = uniqid();
  55. $this->started = TRUE;
  56. }
  57. /**
  58. * Returns TRUE if there is a session that can be resumed. FALSE otherwise
  59. *
  60. * @return boolean
  61. */
  62. public function canBeResumed() {
  63. return TRUE;
  64. }
  65. /**
  66. * Resumes an existing session, if any.
  67. *
  68. * @return void
  69. */
  70. public function resume() {
  71. if ($this->started === FALSE) {
  72. $this->start();
  73. }
  74. }
  75. /**
  76. * Generates and propagates a new session ID and transfers all existing data
  77. * to the new session.
  78. *
  79. * @return string The new session ID
  80. */
  81. public function renewId() {
  82. $this->sessionId = uniqid();
  83. return $this->sessionId;
  84. }
  85. /**
  86. * Returns the current session ID.
  87. *
  88. * @return string The current session ID
  89. * @throws \TYPO3\FLOW3\Session\Exception\SessionNotStartedException
  90. */
  91. public function getId() {
  92. if ($this->started !== TRUE) throw new \TYPO3\FLOW3\Session\Exception\SessionNotStartedException('The session has not been started yet.', 1218034659);
  93. return $this->sessionId;
  94. }
  95. /**
  96. * Returns the data associated with the given key.
  97. *
  98. * @param string $key An identifier for the content stored in the session.
  99. * @return mixed The data associated with the given key or NULL
  100. * @throws \TYPO3\FLOW3\Session\Exception\SessionNotStartedException
  101. */
  102. public function getData($key) {
  103. if ($this->started !== TRUE) throw new \TYPO3\FLOW3\Session\Exception\SessionNotStartedException('The session has not been started yet.', 1218034660);
  104. return (array_key_exists($key, $this->data)) ? $this->data[$key] : NULL;
  105. }
  106. /**
  107. * Returns TRUE if $key is available.
  108. *
  109. * @param string $key
  110. * @return boolean
  111. */
  112. public function hasKey($key) {
  113. return array_key_exists($key, $this->data);
  114. }
  115. /**
  116. * Stores the given data under the given key in the session
  117. *
  118. * @param string $key The key under which the data should be stored
  119. * @param object $data The data to be stored
  120. * @return void
  121. * @throws \TYPO3\FLOW3\Session\Exception\SessionNotStartedException
  122. */
  123. public function putData($key, $data) {
  124. if ($this->started !== TRUE) throw new \TYPO3\FLOW3\Session\Exception\SessionNotStartedException('The session has not been started yet.', 1218034661);
  125. $this->data[$key] = $data;
  126. }
  127. /**
  128. * Closes the session
  129. *
  130. * @return void
  131. * @throws \TYPO3\FLOW3\Session\Exception\SessionNotStartedException
  132. */
  133. public function close() {
  134. if ($this->started !== TRUE) throw new \TYPO3\FLOW3\Session\Exception\SessionNotStartedException('The session has not been started yet.', 1218034662);
  135. $this->started = FALSE;
  136. }
  137. /**
  138. * Explicitly destroys all session data
  139. *
  140. * @param string $reason A reason for destroying the session – used by the LoggingAspect
  141. * @return void
  142. * @throws \TYPO3\FLOW3\Session\Exception
  143. * @throws \TYPO3\FLOW3\Session\Exception\SessionNotStartedException
  144. */
  145. public function destroy($reason = NULL) {
  146. if ($this->started !== TRUE) throw new \TYPO3\FLOW3\Session\Exception\SessionNotStartedException('The session has not been started yet.', 1218034663);
  147. $this->data = array();
  148. $this->started = FALSE;
  149. }
  150. /**
  151. * No operation for transient session.
  152. *
  153. * @param \TYPO3\FLOW3\Core\Bootstrap $bootstrap
  154. * @return void
  155. */
  156. static public function destroyAll(\TYPO3\FLOW3\Core\Bootstrap $bootstrap) {}
  157. }
  158. ?>