memory.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. * @author Robin Appelman <icewind@owncloud.com>
  6. * @author Thomas Müller <thomas.mueller@tmit.eu>
  7. *
  8. * @copyright Copyright (c) 2015, ownCloud, Inc.
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Session;
  25. /**
  26. * Class Internal
  27. *
  28. * store session data in an in-memory array, not persistent
  29. *
  30. * @package OC\Session
  31. */
  32. class Memory extends Session {
  33. protected $data;
  34. public function __construct($name) {
  35. //no need to use $name since all data is already scoped to this instance
  36. $this->data = array();
  37. }
  38. /**
  39. * @param string $key
  40. * @param integer $value
  41. */
  42. public function set($key, $value) {
  43. $this->validateSession();
  44. $this->data[$key] = $value;
  45. }
  46. /**
  47. * @param string $key
  48. * @return mixed
  49. */
  50. public function get($key) {
  51. if (!$this->exists($key)) {
  52. return null;
  53. }
  54. return $this->data[$key];
  55. }
  56. /**
  57. * @param string $key
  58. * @return bool
  59. */
  60. public function exists($key) {
  61. return isset($this->data[$key]);
  62. }
  63. /**
  64. * @param string $key
  65. */
  66. public function remove($key) {
  67. $this->validateSession();
  68. unset($this->data[$key]);
  69. }
  70. public function clear() {
  71. $this->data = array();
  72. }
  73. /**
  74. * Helper function for PHPUnit execution - don't use in non-test code
  75. */
  76. public function reopen() {
  77. $this->sessionClosed = false;
  78. }
  79. /**
  80. * In case the session has already been locked an exception will be thrown
  81. *
  82. * @throws \Exception
  83. */
  84. private function validateSession() {
  85. if ($this->sessionClosed) {
  86. throw new \Exception('Session has been closed - no further changes to the session as allowed');
  87. }
  88. }
  89. }