SessionBagProxy.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 array $data;
  19. private ?int $usageIndex;
  20. private ?\Closure $usageReporter;
  21. public function __construct(
  22. private SessionBagInterface $bag,
  23. array &$data,
  24. ?int &$usageIndex,
  25. ?callable $usageReporter,
  26. ) {
  27. $this->bag = $bag;
  28. $this->data = &$data;
  29. $this->usageIndex = &$usageIndex;
  30. $this->usageReporter = null === $usageReporter ? null : $usageReporter(...);
  31. }
  32. public function getBag(): SessionBagInterface
  33. {
  34. ++$this->usageIndex;
  35. if ($this->usageReporter && 0 <= $this->usageIndex) {
  36. ($this->usageReporter)();
  37. }
  38. return $this->bag;
  39. }
  40. public function isEmpty(): bool
  41. {
  42. if (!isset($this->data[$this->bag->getStorageKey()])) {
  43. return true;
  44. }
  45. ++$this->usageIndex;
  46. if ($this->usageReporter && 0 <= $this->usageIndex) {
  47. ($this->usageReporter)();
  48. }
  49. return empty($this->data[$this->bag->getStorageKey()]);
  50. }
  51. public function getName(): string
  52. {
  53. return $this->bag->getName();
  54. }
  55. public function initialize(array &$array): void
  56. {
  57. ++$this->usageIndex;
  58. if ($this->usageReporter && 0 <= $this->usageIndex) {
  59. ($this->usageReporter)();
  60. }
  61. $this->data[$this->bag->getStorageKey()] = &$array;
  62. $this->bag->initialize($array);
  63. }
  64. public function getStorageKey(): string
  65. {
  66. return $this->bag->getStorageKey();
  67. }
  68. public function clear(): mixed
  69. {
  70. return $this->bag->clear();
  71. }
  72. }