Memory.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Christoph Wurst <christoph@owncloud.com>
  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 Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC\Session;
  29. use Exception;
  30. use OCP\Session\Exceptions\SessionNotAvailableException;
  31. /**
  32. * Class Internal
  33. *
  34. * store session data in an in-memory array, not persistent
  35. *
  36. * @package OC\Session
  37. */
  38. class Memory extends Session {
  39. protected $data;
  40. public function __construct(string $name) {
  41. //no need to use $name since all data is already scoped to this instance
  42. $this->data = [];
  43. }
  44. /**
  45. * @param string $key
  46. * @param integer $value
  47. */
  48. public function set(string $key, $value) {
  49. $this->validateSession();
  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. $this->validateSession();
  74. unset($this->data[$key]);
  75. }
  76. public function clear() {
  77. $this->data = [];
  78. }
  79. /**
  80. * Stub since the session ID does not need to get regenerated for the cache
  81. *
  82. * @param bool $deleteOldSession
  83. */
  84. public function regenerateId(bool $deleteOldSession = true, bool $updateToken = false) {}
  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() {
  99. $this->sessionClosed = false;
  100. }
  101. /**
  102. * In case the session has already been locked an exception will be thrown
  103. *
  104. * @throws Exception
  105. */
  106. private function validateSession() {
  107. if ($this->sessionClosed) {
  108. throw new Exception('Session has been closed - no further changes to the session are allowed');
  109. }
  110. }
  111. }