SessionBagProxy.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 SessionBagInterface $bag;
  19. private array $data;
  20. private ?int $usageIndex;
  21. private ?\Closure $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 = null === $usageReporter ? null : $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. public function getName(): string
  49. {
  50. return $this->bag->getName();
  51. }
  52. public function initialize(array &$array): void
  53. {
  54. ++$this->usageIndex;
  55. if ($this->usageReporter && 0 <= $this->usageIndex) {
  56. ($this->usageReporter)();
  57. }
  58. $this->data[$this->bag->getStorageKey()] = &$array;
  59. $this->bag->initialize($array);
  60. }
  61. public function getStorageKey(): string
  62. {
  63. return $this->bag->getStorageKey();
  64. }
  65. public function clear(): mixed
  66. {
  67. return $this->bag->clear();
  68. }
  69. }