MetadataBag.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. * @return void
  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|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. * @return void
  76. */
  77. public function stampNew(?int $lifetime = null)
  78. {
  79. $this->stampCreated($lifetime);
  80. }
  81. public function getStorageKey(): string
  82. {
  83. return $this->storageKey;
  84. }
  85. /**
  86. * Gets the created timestamp metadata.
  87. *
  88. * @return int Unix timestamp
  89. */
  90. public function getCreated(): int
  91. {
  92. return $this->meta[self::CREATED];
  93. }
  94. /**
  95. * Gets the last used metadata.
  96. *
  97. * @return int Unix timestamp
  98. */
  99. public function getLastUsed(): int
  100. {
  101. return $this->lastUsed;
  102. }
  103. public function clear(): mixed
  104. {
  105. // nothing to do
  106. return null;
  107. }
  108. public function getName(): string
  109. {
  110. return $this->name;
  111. }
  112. /**
  113. * Sets name.
  114. *
  115. * @return void
  116. */
  117. public function setName(string $name)
  118. {
  119. $this->name = $name;
  120. }
  121. private function stampCreated(?int $lifetime = null): void
  122. {
  123. $timeStamp = time();
  124. $this->meta[self::CREATED] = $this->meta[self::UPDATED] = $this->lastUsed = $timeStamp;
  125. $this->meta[self::LIFETIME] = $lifetime ?? (int) \ini_get('session.cookie_lifetime');
  126. }
  127. }