MetadataBag.php 3.6 KB

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