SessionStorageInterface.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. public function setId(string $id);
  38. /**
  39. * Returns the session name.
  40. */
  41. public function getName(): string;
  42. /**
  43. * Sets the session name.
  44. */
  45. public function setName(string $name);
  46. /**
  47. * Regenerates id that represents this storage.
  48. *
  49. * This method must invoke session_regenerate_id($destroy) unless
  50. * this interface is used for a storage object designed for unit
  51. * or functional testing where a real PHP session would interfere
  52. * with testing.
  53. *
  54. * Note regenerate+destroy should not clear the session data in memory
  55. * only delete the session data from persistent storage.
  56. *
  57. * Care: When regenerating the session ID no locking is involved in PHP's
  58. * session design. See https://bugs.php.net/61470 for a discussion.
  59. * So you must make sure the regenerated session is saved BEFORE sending the
  60. * headers with the new ID. Symfony's HttpKernel offers a listener for this.
  61. * See Symfony\Component\HttpKernel\EventListener\SaveSessionListener.
  62. * Otherwise session data could get lost again for concurrent requests with the
  63. * new ID. One result could be that you get logged out after just logging in.
  64. *
  65. * @param bool $destroy Destroy session when regenerating?
  66. * @param int $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. * @throws \RuntimeException If an error occurs while regenerating this storage
  72. */
  73. public function regenerate(bool $destroy = false, int $lifetime = null): bool;
  74. /**
  75. * Force the session to be saved and closed.
  76. *
  77. * This method must invoke session_write_close() unless this interface is
  78. * used for a storage object design for unit or functional testing where
  79. * a real PHP session would interfere with testing, in which case
  80. * it should actually persist the session data if required.
  81. *
  82. * @throws \RuntimeException if the session is saved without being started, or if the session
  83. * is already closed
  84. */
  85. public function save();
  86. /**
  87. * Clear all session data in memory.
  88. */
  89. public function clear();
  90. /**
  91. * Gets a SessionBagInterface by name.
  92. *
  93. * @throws \InvalidArgumentException If the bag does not exist
  94. */
  95. public function getBag(string $name): SessionBagInterface;
  96. /**
  97. * Registers a SessionBagInterface for use.
  98. */
  99. public function registerBag(SessionBagInterface $bag);
  100. public function getMetadataBag(): MetadataBag;
  101. }