You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.0 KiB

3 years ago
  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;
  11. /**
  12. * @author Nicolas Grekas <p@tchwork.com>
  13. *
  14. * @internal
  15. */
  16. final class SessionBagProxy implements SessionBagInterface
  17. {
  18. private $bag;
  19. private $data;
  20. private $usageIndex;
  21. private $usageReporter;
  22. public function __construct(SessionBagInterface $bag, array &$data, ?int &$usageIndex, ?callable $usageReporter)
  23. {
  24. $this->bag = $bag;
  25. $this->data = &$data;
  26. $this->usageIndex = &$usageIndex;
  27. $this->usageReporter = $usageReporter;
  28. }
  29. public function getBag(): SessionBagInterface
  30. {
  31. ++$this->usageIndex;
  32. if ($this->usageReporter && 0 <= $this->usageIndex) {
  33. ($this->usageReporter)();
  34. }
  35. return $this->bag;
  36. }
  37. public function isEmpty(): bool
  38. {
  39. if (!isset($this->data[$this->bag->getStorageKey()])) {
  40. return true;
  41. }
  42. ++$this->usageIndex;
  43. if ($this->usageReporter && 0 <= $this->usageIndex) {
  44. ($this->usageReporter)();
  45. }
  46. return empty($this->data[$this->bag->getStorageKey()]);
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function getName(): string
  52. {
  53. return $this->bag->getName();
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function initialize(array &$array): void
  59. {
  60. ++$this->usageIndex;
  61. if ($this->usageReporter && 0 <= $this->usageIndex) {
  62. ($this->usageReporter)();
  63. }
  64. $this->data[$this->bag->getStorageKey()] = &$array;
  65. $this->bag->initialize($array);
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function getStorageKey(): string
  71. {
  72. return $this->bag->getStorageKey();
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function clear()
  78. {
  79. return $this->bag->clear();
  80. }
  81. }