SessionStorageInterface.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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\Storage;
  11. use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
  12. /**
  13. * StorageInterface.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. * @author Drak <drak@zikula.org>
  17. */
  18. interface SessionStorageInterface
  19. {
  20. /**
  21. * Starts the session.
  22. *
  23. * @throws \RuntimeException if something goes wrong starting the session
  24. */
  25. public function start(): bool;
  26. /**
  27. * Checks if the session is started.
  28. */
  29. public function isStarted(): bool;
  30. /**
  31. * Returns the session ID.
  32. */
  33. public function getId(): string;
  34. /**
  35. * Sets the session ID.
  36. *
  37. * @return void
  38. */
  39. public function setId(string $id);
  40. /**
  41. * Returns the session name.
  42. */
  43. public function getName(): string;
  44. /**
  45. * Sets the session name.
  46. *
  47. * @return void
  48. */
  49. public function setName(string $name);
  50. /**
  51. * Regenerates id that represents this storage.
  52. *
  53. * This method must invoke session_regenerate_id($destroy) unless
  54. * this interface is used for a storage object designed for unit
  55. * or functional testing where a real PHP session would interfere
  56. * with testing.
  57. *
  58. * Note regenerate+destroy should not clear the session data in memory
  59. * only delete the session data from persistent storage.
  60. *
  61. * Care: When regenerating the session ID no locking is involved in PHP's
  62. * session design. See https://bugs.php.net/61470 for a discussion.
  63. * So you must make sure the regenerated session is saved BEFORE sending the
  64. * headers with the new ID. Symfony's HttpKernel offers a listener for this.
  65. * See Symfony\Component\HttpKernel\EventListener\SaveSessionListener.
  66. * Otherwise session data could get lost again for concurrent requests with the
  67. * new ID. One result could be that you get logged out after just logging in.
  68. *
  69. * @param bool $destroy Destroy session when regenerating?
  70. * @param int|null $lifetime Sets the cookie lifetime for the session cookie. A null value
  71. * will leave the system settings unchanged, 0 sets the cookie
  72. * to expire with browser session. Time is in seconds, and is
  73. * not a Unix timestamp.
  74. *
  75. * @throws \RuntimeException If an error occurs while regenerating this storage
  76. */
  77. public function regenerate(bool $destroy = false, ?int $lifetime = null): bool;
  78. /**
  79. * Force the session to be saved and closed.
  80. *
  81. * This method must invoke session_write_close() unless this interface is
  82. * used for a storage object design for unit or functional testing where
  83. * a real PHP session would interfere with testing, in which case
  84. * it should actually persist the session data if required.
  85. *
  86. * @return void
  87. *
  88. * @throws \RuntimeException if the session is saved without being started, or if the session
  89. * is already closed
  90. */
  91. public function save();
  92. /**
  93. * Clear all session data in memory.
  94. *
  95. * @return void
  96. */
  97. public function clear();
  98. /**
  99. * Gets a SessionBagInterface by name.
  100. *
  101. * @throws \InvalidArgumentException If the bag does not exist
  102. */
  103. public function getBag(string $name): SessionBagInterface;
  104. /**
  105. * Registers a SessionBagInterface for use.
  106. *
  107. * @return void
  108. */
  109. public function registerBag(SessionBagInterface $bag);
  110. public function getMetadataBag(): MetadataBag;
  111. }