Memory.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@owncloud.com>
  6. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Session;
  28. use Exception;
  29. use OCP\Session\Exceptions\SessionNotAvailableException;
  30. /**
  31. * Class Internal
  32. *
  33. * store session data in an in-memory array, not persistent
  34. *
  35. * @package OC\Session
  36. */
  37. class Memory extends Session {
  38. protected $data;
  39. public function __construct($name) {
  40. //no need to use $name since all data is already scoped to this instance
  41. $this->data = array();
  42. }
  43. /**
  44. * @param string $key
  45. * @param integer $value
  46. */
  47. public function set($key, $value) {
  48. $this->validateSession();
  49. $this->data[$key] = $value;
  50. }
  51. /**
  52. * @param string $key
  53. * @return mixed
  54. */
  55. public function get($key) {
  56. if (!$this->exists($key)) {
  57. return null;
  58. }
  59. return $this->data[$key];
  60. }
  61. /**
  62. * @param string $key
  63. * @return bool
  64. */
  65. public function exists($key) {
  66. return isset($this->data[$key]);
  67. }
  68. /**
  69. * @param string $key
  70. */
  71. public function remove($key) {
  72. $this->validateSession();
  73. unset($this->data[$key]);
  74. }
  75. public function clear() {
  76. $this->data = array();
  77. }
  78. /**
  79. * Stub since the session ID does not need to get regenerated for the cache
  80. *
  81. * @param bool $deleteOldSession
  82. */
  83. public function regenerateId($deleteOldSession = true) {}
  84. /**
  85. * Wrapper around session_id
  86. *
  87. * @return string
  88. * @throws SessionNotAvailableException
  89. * @since 9.1.0
  90. */
  91. public function getId() {
  92. throw new SessionNotAvailableException('Memory session does not have an ID');
  93. }
  94. /**
  95. * Helper function for PHPUnit execution - don't use in non-test code
  96. */
  97. public function reopen() {
  98. $this->sessionClosed = false;
  99. }
  100. /**
  101. * In case the session has already been locked an exception will be thrown
  102. *
  103. * @throws Exception
  104. */
  105. private function validateSession() {
  106. if ($this->sessionClosed) {
  107. throw new Exception('Session has been closed - no further changes to the session are allowed');
  108. }
  109. }
  110. }