You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.0 KiB

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