MetadataBag.php 3.3 KB

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