SessionInterface.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Contract;
  12. use RuntimeException;
  13. interface SessionInterface
  14. {
  15. /**
  16. * Starts the session storage.
  17. *
  18. * @return bool True if session started
  19. * @throws RuntimeException if session fails to start
  20. */
  21. public function start(): bool;
  22. /**
  23. * Returns the session ID.
  24. *
  25. * @return string The session ID
  26. */
  27. public function getId(): string;
  28. /**
  29. * Sets the session ID.
  30. */
  31. public function setId(string $id);
  32. /**
  33. * Returns the session name.
  34. */
  35. public function getName(): string;
  36. /**
  37. * Sets the session name.
  38. */
  39. public function setName(string $name);
  40. /**
  41. * Invalidates the current session.
  42. *
  43. * Clears all session attributes and flashes and regenerates the
  44. * session and deletes the old session from persistence.
  45. *
  46. * @param int $lifetime Sets the cookie lifetime for the session cookie. A null value
  47. * will leave the system settings unchanged, 0 sets the cookie
  48. * to expire with browser session. Time is in seconds, and is
  49. * not a Unix timestamp.
  50. *
  51. * @return bool True if session invalidated, false if error
  52. */
  53. public function invalidate(?int $lifetime = null): bool;
  54. /**
  55. * Migrates the current session to a new session id while maintaining all
  56. * session attributes.
  57. *
  58. * @param bool $destroy Whether to delete the old session or leave it to garbage collection
  59. * @param int $lifetime Sets the cookie lifetime for the session cookie. A null value
  60. * will leave the system settings unchanged, 0 sets the cookie
  61. * to expire with browser session. Time is in seconds, and is
  62. * not a Unix timestamp.
  63. *
  64. * @return bool True if session migrated, false if error
  65. */
  66. public function migrate(bool $destroy = false, ?int $lifetime = null): bool;
  67. /**
  68. * Force the session to be saved and closed.
  69. *
  70. * This method is generally not required for real sessions as
  71. * the session will be automatically saved at the end of
  72. * code execution.
  73. */
  74. public function save(): void;
  75. /**
  76. * Checks if an attribute is defined.
  77. *
  78. * @param string $name The attribute name
  79. *
  80. * @return bool true if the attribute is defined, false otherwise
  81. */
  82. public function has(string $name): bool;
  83. /**
  84. * Returns an attribute.
  85. *
  86. * @param string $name The attribute name
  87. * @param mixed $default The default value if not found
  88. */
  89. public function get(string $name, $default = null);
  90. /**
  91. * Sets an attribute.
  92. * @param mixed $value
  93. */
  94. public function set(string $name, $value): void;
  95. /**
  96. * Put a key / value pair or array of key / value pairs in the session.
  97. *
  98. * @param array|string $key
  99. * @param null|mixed $value
  100. */
  101. public function put($key, $value = null): void;
  102. /**
  103. * Returns attributes.
  104. */
  105. public function all(): array;
  106. /**
  107. * Sets attributes.
  108. */
  109. public function replace(array $attributes): void;
  110. /**
  111. * Removes an attribute, returning its value.
  112. *
  113. * @return mixed The removed value or null when it does not exist
  114. */
  115. public function remove(string $name);
  116. /**
  117. * Remove one or many items from the session.
  118. *
  119. * @param array|string $keys
  120. */
  121. public function forget($keys): void;
  122. /**
  123. * Clears all attributes.
  124. */
  125. public function clear(): void;
  126. /**
  127. * Checks if the session was started.
  128. */
  129. public function isStarted(): bool;
  130. /**
  131. * Get the previous URL from the session.
  132. */
  133. public function previousUrl(): ?string;
  134. /**
  135. * Set the "previous" URL in the session.
  136. */
  137. public function setPreviousUrl(string $url): void;
  138. }