Memory.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 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. /**
  41. * @param string $key
  42. * @param integer $value
  43. */
  44. public function set(string $key, $value) {
  45. $this->data[$key] = $value;
  46. }
  47. /**
  48. * @param string $key
  49. * @return mixed
  50. */
  51. public function get(string $key) {
  52. if (!$this->exists($key)) {
  53. return null;
  54. }
  55. return $this->data[$key];
  56. }
  57. /**
  58. * @param string $key
  59. * @return bool
  60. */
  61. public function exists(string $key): bool {
  62. return isset($this->data[$key]);
  63. }
  64. /**
  65. * @param string $key
  66. */
  67. public function remove(string $key) {
  68. unset($this->data[$key]);
  69. }
  70. public function clear() {
  71. $this->data = [];
  72. }
  73. /**
  74. * Stub since the session ID does not need to get regenerated for the cache
  75. *
  76. * @param bool $deleteOldSession
  77. */
  78. public function regenerateId(bool $deleteOldSession = true, bool $updateToken = false) {
  79. }
  80. /**
  81. * Wrapper around session_id
  82. *
  83. * @return string
  84. * @throws SessionNotAvailableException
  85. * @since 9.1.0
  86. */
  87. public function getId(): string {
  88. throw new SessionNotAvailableException('Memory session does not have an ID');
  89. }
  90. /**
  91. * Helper function for PHPUnit execution - don't use in non-test code
  92. */
  93. public function reopen(): bool {
  94. $reopened = $this->sessionClosed;
  95. $this->sessionClosed = false;
  96. return $reopened;
  97. }
  98. }