SessionInterface.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. public function setId(string $id);
  33. /**
  34. * Returns the session name.
  35. */
  36. public function getName(): string;
  37. /**
  38. * Sets the session name.
  39. */
  40. public function setName(string $name);
  41. /**
  42. * Invalidates the current session.
  43. *
  44. * Clears all session attributes and flashes and regenerates the
  45. * session and deletes the old session from persistence.
  46. *
  47. * @param int $lifetime Sets the cookie lifetime for the session cookie. A null value
  48. * will leave the system settings unchanged, 0 sets the cookie
  49. * to expire with browser session. Time is in seconds, and is
  50. * not a Unix timestamp.
  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. public function migrate(bool $destroy = false, int $lifetime = null): bool;
  64. /**
  65. * Force the session to be saved and closed.
  66. *
  67. * This method is generally not required for real sessions as
  68. * the session will be automatically saved at the end of
  69. * code execution.
  70. */
  71. public function save();
  72. /**
  73. * Checks if an attribute is defined.
  74. */
  75. public function has(string $name): bool;
  76. /**
  77. * Returns an attribute.
  78. */
  79. public function get(string $name, mixed $default = null): mixed;
  80. /**
  81. * Sets an attribute.
  82. */
  83. public function set(string $name, mixed $value);
  84. /**
  85. * Returns attributes.
  86. */
  87. public function all(): array;
  88. /**
  89. * Sets attributes.
  90. */
  91. public function replace(array $attributes);
  92. /**
  93. * Removes an attribute.
  94. *
  95. * @return mixed The removed value or null when it does not exist
  96. */
  97. public function remove(string $name): mixed;
  98. /**
  99. * Clears all attributes.
  100. */
  101. public function clear();
  102. /**
  103. * Checks if the session was started.
  104. */
  105. public function isStarted(): bool;
  106. /**
  107. * Registers a SessionBagInterface with the session.
  108. */
  109. public function registerBag(SessionBagInterface $bag);
  110. /**
  111. * Gets a bag instance by name.
  112. */
  113. public function getBag(string $name): SessionBagInterface;
  114. /**
  115. * Gets session meta.
  116. */
  117. public function getMetadataBag(): MetadataBag;
  118. }