Memory.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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->data[$key] = $value;
  51. }
  52. /**
  53. * @param string $key
  54. * @return mixed
  55. */
  56. public function get(string $key) {
  57. if (!$this->exists($key)) {
  58. return null;
  59. }
  60. return $this->data[$key];
  61. }
  62. /**
  63. * @param string $key
  64. * @return bool
  65. */
  66. public function exists(string $key): bool {
  67. return isset($this->data[$key]);
  68. }
  69. /**
  70. * @param string $key
  71. */
  72. public function remove(string $key) {
  73. unset($this->data[$key]);
  74. }
  75. public function clear() {
  76. $this->data = [];
  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(bool $deleteOldSession = true, bool $updateToken = false) {
  84. }
  85. /**
  86. * Wrapper around session_id
  87. *
  88. * @return string
  89. * @throws SessionNotAvailableException
  90. * @since 9.1.0
  91. */
  92. public function getId(): string {
  93. throw new SessionNotAvailableException('Memory session does not have an ID');
  94. }
  95. /**
  96. * Helper function for PHPUnit execution - don't use in non-test code
  97. */
  98. public function reopen(): bool {
  99. $reopened = $this->sessionClosed;
  100. $this->sessionClosed = false;
  101. return $reopened;
  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. }