MetadataBag.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. private string $name = '__metadata';
  25. private string $storageKey;
  26. /**
  27. * @var array
  28. */
  29. protected $meta = [self::CREATED => 0, self::UPDATED => 0, self::LIFETIME => 0];
  30. /**
  31. * Unix timestamp.
  32. */
  33. private int $lastUsed;
  34. private int $updateThreshold;
  35. /**
  36. * @param string $storageKey The key used to store bag in the session
  37. * @param int $updateThreshold The time to wait between two UPDATED updates
  38. */
  39. public function __construct(string $storageKey = '_sf2_meta', int $updateThreshold = 0)
  40. {
  41. $this->storageKey = $storageKey;
  42. $this->updateThreshold = $updateThreshold;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function initialize(array &$array)
  48. {
  49. $this->meta = &$array;
  50. if (isset($array[self::CREATED])) {
  51. $this->lastUsed = $this->meta[self::UPDATED];
  52. $timeStamp = time();
  53. if ($timeStamp - $array[self::UPDATED] >= $this->updateThreshold) {
  54. $this->meta[self::UPDATED] = $timeStamp;
  55. }
  56. } else {
  57. $this->stampCreated();
  58. }
  59. }
  60. /**
  61. * Gets the lifetime that the session cookie was set with.
  62. */
  63. public function getLifetime(): int
  64. {
  65. return $this->meta[self::LIFETIME];
  66. }
  67. /**
  68. * Stamps a new session's metadata.
  69. *
  70. * @param int $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. public function stampNew(int $lifetime = null)
  76. {
  77. $this->stampCreated($lifetime);
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function getStorageKey(): string
  83. {
  84. return $this->storageKey;
  85. }
  86. /**
  87. * Gets the created timestamp metadata.
  88. *
  89. * @return int Unix timestamp
  90. */
  91. public function getCreated(): int
  92. {
  93. return $this->meta[self::CREATED];
  94. }
  95. /**
  96. * Gets the last used metadata.
  97. *
  98. * @return int Unix timestamp
  99. */
  100. public function getLastUsed(): int
  101. {
  102. return $this->lastUsed;
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function clear(): mixed
  108. {
  109. // nothing to do
  110. return null;
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function getName(): string
  116. {
  117. return $this->name;
  118. }
  119. /**
  120. * Sets name.
  121. */
  122. public function setName(string $name)
  123. {
  124. $this->name = $name;
  125. }
  126. private function stampCreated(int $lifetime = null): void
  127. {
  128. $timeStamp = time();
  129. $this->meta[self::CREATED] = $this->meta[self::UPDATED] = $this->lastUsed = $timeStamp;
  130. $this->meta[self::LIFETIME] = $lifetime ?? (int) \ini_get('session.cookie_lifetime');
  131. }
  132. }