MetadataBag.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. * Metadata container.
  14. *
  15. * Adds metadata to the session.
  16. *
  17. * @author Drak <drak@zikula.org>
  18. */
  19. class MetadataBag implements SessionBagInterface
  20. {
  21. public const CREATED = 'c';
  22. public const UPDATED = 'u';
  23. public const LIFETIME = 'l';
  24. protected array $meta = [self::CREATED => 0, self::UPDATED => 0, self::LIFETIME => 0];
  25. private string $name = '__metadata';
  26. private int $lastUsed;
  27. /**
  28. * @param string $storageKey The key used to store bag in the session
  29. * @param int $updateThreshold The time to wait between two UPDATED updates
  30. */
  31. public function __construct(
  32. private string $storageKey = '_sf2_meta',
  33. private int $updateThreshold = 0,
  34. ) {
  35. }
  36. public function initialize(array &$array): void
  37. {
  38. $this->meta = &$array;
  39. if (isset($array[self::CREATED])) {
  40. $this->lastUsed = $this->meta[self::UPDATED];
  41. $timeStamp = time();
  42. if ($timeStamp - $array[self::UPDATED] >= $this->updateThreshold) {
  43. $this->meta[self::UPDATED] = $timeStamp;
  44. }
  45. } else {
  46. $this->stampCreated();
  47. }
  48. }
  49. /**
  50. * Gets the lifetime that the session cookie was set with.
  51. */
  52. public function getLifetime(): int
  53. {
  54. return $this->meta[self::LIFETIME];
  55. }
  56. /**
  57. * Stamps a new session's metadata.
  58. *
  59. * @param int|null $lifetime Sets the cookie lifetime for the session cookie. A null value
  60. * will leave the system settings unchanged, 0 sets the cookie
  61. * to expire with browser session. Time is in seconds, and is
  62. * not a Unix timestamp.
  63. */
  64. public function stampNew(?int $lifetime = null): void
  65. {
  66. $this->stampCreated($lifetime);
  67. }
  68. public function getStorageKey(): string
  69. {
  70. return $this->storageKey;
  71. }
  72. /**
  73. * Gets the created timestamp metadata.
  74. *
  75. * @return int Unix timestamp
  76. */
  77. public function getCreated(): int
  78. {
  79. return $this->meta[self::CREATED];
  80. }
  81. /**
  82. * Gets the last used metadata.
  83. *
  84. * @return int Unix timestamp
  85. */
  86. public function getLastUsed(): int
  87. {
  88. return $this->lastUsed;
  89. }
  90. public function clear(): mixed
  91. {
  92. // nothing to do
  93. return null;
  94. }
  95. public function getName(): string
  96. {
  97. return $this->name;
  98. }
  99. /**
  100. * Sets name.
  101. */
  102. public function setName(string $name): void
  103. {
  104. $this->name = $name;
  105. }
  106. private function stampCreated(?int $lifetime = null): void
  107. {
  108. $timeStamp = time();
  109. $this->meta[self::CREATED] = $this->meta[self::UPDATED] = $this->lastUsed = $timeStamp;
  110. $this->meta[self::LIFETIME] = $lifetime ?? (int) \ini_get('session.cookie_lifetime');
  111. }
  112. }