SessionInterface.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Session;
  11. use Symfony\Component\HttpFoundation\Session\Storage\MetadataBag;
  12. /**
  13. * Interface for the session.
  14. *
  15. * @author Drak <drak@zikula.org>
  16. */
  17. interface SessionInterface
  18. {
  19. /**
  20. * Starts the session storage.
  21. *
  22. * @throws \RuntimeException if session fails to start
  23. */
  24. public function start(): bool;
  25. /**
  26. * Returns the session ID.
  27. */
  28. public function getId(): string;
  29. /**
  30. * Sets the session ID.
  31. *
  32. * @return void
  33. */
  34. public function setId(string $id);
  35. /**
  36. * Returns the session name.
  37. */
  38. public function getName(): string;
  39. /**
  40. * Sets the session name.
  41. *
  42. * @return void
  43. */
  44. public function setName(string $name);
  45. /**
  46. * Invalidates the current session.
  47. *
  48. * Clears all session attributes and flashes and regenerates the
  49. * session and deletes the old session from persistence.
  50. *
  51. * @param int|null $lifetime Sets the cookie lifetime for the session cookie. A null value
  52. * will leave the system settings unchanged, 0 sets the cookie
  53. * to expire with browser session. Time is in seconds, and is
  54. * not a Unix timestamp.
  55. */
  56. public function invalidate(?int $lifetime = null): bool;
  57. /**
  58. * Migrates the current session to a new session id while maintaining all
  59. * session attributes.
  60. *
  61. * @param bool $destroy Whether to delete the old session or leave it to garbage collection
  62. * @param int|null $lifetime Sets the cookie lifetime for the session cookie. A null value
  63. * will leave the system settings unchanged, 0 sets the cookie
  64. * to expire with browser session. Time is in seconds, and is
  65. * not a Unix timestamp.
  66. */
  67. public function migrate(bool $destroy = false, ?int $lifetime = null): bool;
  68. /**
  69. * Force the session to be saved and closed.
  70. *
  71. * This method is generally not required for real sessions as
  72. * the session will be automatically saved at the end of
  73. * code execution.
  74. *
  75. * @return void
  76. */
  77. public function save();
  78. /**
  79. * Checks if an attribute is defined.
  80. */
  81. public function has(string $name): bool;
  82. /**
  83. * Returns an attribute.
  84. */
  85. public function get(string $name, mixed $default = null): mixed;
  86. /**
  87. * Sets an attribute.
  88. *
  89. * @return void
  90. */
  91. public function set(string $name, mixed $value);
  92. /**
  93. * Returns attributes.
  94. */
  95. public function all(): array;
  96. /**
  97. * Sets attributes.
  98. *
  99. * @return void
  100. */
  101. public function replace(array $attributes);
  102. /**
  103. * Removes an attribute.
  104. *
  105. * @return mixed The removed value or null when it does not exist
  106. */
  107. public function remove(string $name): mixed;
  108. /**
  109. * Clears all attributes.
  110. *
  111. * @return void
  112. */
  113. public function clear();
  114. /**
  115. * Checks if the session was started.
  116. */
  117. public function isStarted(): bool;
  118. /**
  119. * Registers a SessionBagInterface with the session.
  120. *
  121. * @return void
  122. */
  123. public function registerBag(SessionBagInterface $bag);
  124. /**
  125. * Gets a bag instance by name.
  126. */
  127. public function getBag(string $name): SessionBagInterface;
  128. /**
  129. * Gets session meta.
  130. */
  131. public function getMetadataBag(): MetadataBag;
  132. }