SessionInterface.php 4.1 KB

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