SessionInterface.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. * @return bool
  23. *
  24. * @throws \RuntimeException if session fails to start
  25. */
  26. public function start();
  27. /**
  28. * Returns the session ID.
  29. *
  30. * @return string
  31. */
  32. public function getId();
  33. /**
  34. * Sets the session ID.
  35. */
  36. public function setId(string $id);
  37. /**
  38. * Returns the session name.
  39. *
  40. * @return string
  41. */
  42. public function getName();
  43. /**
  44. * Sets the session name.
  45. */
  46. public function setName(string $name);
  47. /**
  48. * Invalidates the current session.
  49. *
  50. * Clears all session attributes and flashes and regenerates the
  51. * session and deletes the old session from persistence.
  52. *
  53. * @param int|null $lifetime Sets the cookie lifetime for the session cookie. A null value
  54. * will leave the system settings unchanged, 0 sets the cookie
  55. * to expire with browser session. Time is in seconds, and is
  56. * not a Unix timestamp.
  57. *
  58. * @return bool
  59. */
  60. public function invalidate(?int $lifetime = null);
  61. /**
  62. * Migrates the current session to a new session id while maintaining all
  63. * session attributes.
  64. *
  65. * @param bool $destroy Whether to delete the old session or leave it to garbage collection
  66. * @param int|null $lifetime Sets the cookie lifetime for the session cookie. A null value
  67. * will leave the system settings unchanged, 0 sets the cookie
  68. * to expire with browser session. Time is in seconds, and is
  69. * not a Unix timestamp.
  70. *
  71. * @return bool
  72. */
  73. public function migrate(bool $destroy = false, ?int $lifetime = null);
  74. /**
  75. * Force the session to be saved and closed.
  76. *
  77. * This method is generally not required for real sessions as
  78. * the session will be automatically saved at the end of
  79. * code execution.
  80. */
  81. public function save();
  82. /**
  83. * Checks if an attribute is defined.
  84. *
  85. * @return bool
  86. */
  87. public function has(string $name);
  88. /**
  89. * Returns an attribute.
  90. *
  91. * @param mixed $default The default value if not found
  92. *
  93. * @return mixed
  94. */
  95. public function get(string $name, $default = null);
  96. /**
  97. * Sets an attribute.
  98. *
  99. * @param mixed $value
  100. */
  101. public function set(string $name, $value);
  102. /**
  103. * Returns attributes.
  104. *
  105. * @return array
  106. */
  107. public function all();
  108. /**
  109. * Sets attributes.
  110. */
  111. public function replace(array $attributes);
  112. /**
  113. * Removes an attribute.
  114. *
  115. * @return mixed The removed value or null when it does not exist
  116. */
  117. public function remove(string $name);
  118. /**
  119. * Clears all attributes.
  120. */
  121. public function clear();
  122. /**
  123. * Checks if the session was started.
  124. *
  125. * @return bool
  126. */
  127. public function isStarted();
  128. /**
  129. * Registers a SessionBagInterface with the session.
  130. */
  131. public function registerBag(SessionBagInterface $bag);
  132. /**
  133. * Gets a bag instance by name.
  134. *
  135. * @return SessionBagInterface
  136. */
  137. public function getBag(string $name);
  138. /**
  139. * Gets session meta.
  140. *
  141. * @return MetadataBag
  142. */
  143. public function getMetadataBag();
  144. }