Memory.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. public function __construct(string $name) {
  20. //no need to use $name since all data is already scoped to this instance
  21. $this->data = [];
  22. }
  23. /**
  24. * @param string $key
  25. * @param integer $value
  26. */
  27. public function set(string $key, $value) {
  28. $this->data[$key] = $value;
  29. }
  30. /**
  31. * @param string $key
  32. * @return mixed
  33. */
  34. public function get(string $key) {
  35. if (!$this->exists($key)) {
  36. return null;
  37. }
  38. return $this->data[$key];
  39. }
  40. /**
  41. * @param string $key
  42. * @return bool
  43. */
  44. public function exists(string $key): bool {
  45. return isset($this->data[$key]);
  46. }
  47. /**
  48. * @param string $key
  49. */
  50. public function remove(string $key) {
  51. unset($this->data[$key]);
  52. }
  53. public function clear() {
  54. $this->data = [];
  55. }
  56. /**
  57. * Stub since the session ID does not need to get regenerated for the cache
  58. *
  59. * @param bool $deleteOldSession
  60. */
  61. public function regenerateId(bool $deleteOldSession = true, bool $updateToken = false) {
  62. }
  63. /**
  64. * Wrapper around session_id
  65. *
  66. * @return string
  67. * @throws SessionNotAvailableException
  68. * @since 9.1.0
  69. */
  70. public function getId(): string {
  71. throw new SessionNotAvailableException('Memory session does not have an ID');
  72. }
  73. /**
  74. * Helper function for PHPUnit execution - don't use in non-test code
  75. */
  76. public function reopen(): bool {
  77. $reopened = $this->sessionClosed;
  78. $this->sessionClosed = false;
  79. return $reopened;
  80. }
  81. }