1
0

Memory.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OC\Session;
  9. use OCP\Session\Exceptions\SessionNotAvailableException;
  10. /**
  11. * Class Internal
  12. *
  13. * store session data in an in-memory array, not persistent
  14. *
  15. * @package OC\Session
  16. */
  17. class Memory extends Session {
  18. protected $data;
  19. /**
  20. * @param string $key
  21. * @param integer $value
  22. */
  23. public function set(string $key, $value) {
  24. $this->data[$key] = $value;
  25. }
  26. /**
  27. * @param string $key
  28. * @return mixed
  29. */
  30. public function get(string $key) {
  31. if (!$this->exists($key)) {
  32. return null;
  33. }
  34. return $this->data[$key];
  35. }
  36. /**
  37. * @param string $key
  38. * @return bool
  39. */
  40. public function exists(string $key): bool {
  41. return isset($this->data[$key]);
  42. }
  43. /**
  44. * @param string $key
  45. */
  46. public function remove(string $key) {
  47. unset($this->data[$key]);
  48. }
  49. public function clear() {
  50. $this->data = [];
  51. }
  52. /**
  53. * Stub since the session ID does not need to get regenerated for the cache
  54. *
  55. * @param bool $deleteOldSession
  56. */
  57. public function regenerateId(bool $deleteOldSession = true, bool $updateToken = false) {
  58. }
  59. /**
  60. * Wrapper around session_id
  61. *
  62. * @return string
  63. * @throws SessionNotAvailableException
  64. * @since 9.1.0
  65. */
  66. public function getId(): string {
  67. throw new SessionNotAvailableException('Memory session does not have an ID');
  68. }
  69. /**
  70. * Helper function for PHPUnit execution - don't use in non-test code
  71. */
  72. public function reopen(): bool {
  73. $reopened = $this->sessionClosed;
  74. $this->sessionClosed = false;
  75. return $reopened;
  76. }
  77. }