NamespacedAttributeBag.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\Attribute;
  11. trigger_deprecation('symfony/http-foundation', '5.3', 'The "%s" class is deprecated.', NamespacedAttributeBag::class);
  12. /**
  13. * This class provides structured storage of session attributes using
  14. * a name spacing character in the key.
  15. *
  16. * @author Drak <drak@zikula.org>
  17. *
  18. * @deprecated since Symfony 5.3
  19. */
  20. class NamespacedAttributeBag extends AttributeBag
  21. {
  22. private $namespaceCharacter;
  23. /**
  24. * @param string $storageKey Session storage key
  25. * @param string $namespaceCharacter Namespace character to use in keys
  26. */
  27. public function __construct(string $storageKey = '_sf2_attributes', string $namespaceCharacter = '/')
  28. {
  29. $this->namespaceCharacter = $namespaceCharacter;
  30. parent::__construct($storageKey);
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function has(string $name)
  36. {
  37. // reference mismatch: if fixed, re-introduced in array_key_exists; keep as it is
  38. $attributes = $this->resolveAttributePath($name);
  39. $name = $this->resolveKey($name);
  40. if (null === $attributes) {
  41. return false;
  42. }
  43. return \array_key_exists($name, $attributes);
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function get(string $name, $default = null)
  49. {
  50. // reference mismatch: if fixed, re-introduced in array_key_exists; keep as it is
  51. $attributes = $this->resolveAttributePath($name);
  52. $name = $this->resolveKey($name);
  53. if (null === $attributes) {
  54. return $default;
  55. }
  56. return \array_key_exists($name, $attributes) ? $attributes[$name] : $default;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function set(string $name, $value)
  62. {
  63. $attributes = &$this->resolveAttributePath($name, true);
  64. $name = $this->resolveKey($name);
  65. $attributes[$name] = $value;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function remove(string $name)
  71. {
  72. $retval = null;
  73. $attributes = &$this->resolveAttributePath($name);
  74. $name = $this->resolveKey($name);
  75. if (null !== $attributes && \array_key_exists($name, $attributes)) {
  76. $retval = $attributes[$name];
  77. unset($attributes[$name]);
  78. }
  79. return $retval;
  80. }
  81. /**
  82. * Resolves a path in attributes property and returns it as a reference.
  83. *
  84. * This method allows structured namespacing of session attributes.
  85. *
  86. * @param string $name Key name
  87. * @param bool $writeContext Write context, default false
  88. *
  89. * @return array|null
  90. */
  91. protected function &resolveAttributePath(string $name, bool $writeContext = false)
  92. {
  93. $array = &$this->attributes;
  94. $name = (str_starts_with($name, $this->namespaceCharacter)) ? substr($name, 1) : $name;
  95. // Check if there is anything to do, else return
  96. if (!$name) {
  97. return $array;
  98. }
  99. $parts = explode($this->namespaceCharacter, $name);
  100. if (\count($parts) < 2) {
  101. if (!$writeContext) {
  102. return $array;
  103. }
  104. $array[$parts[0]] = [];
  105. return $array;
  106. }
  107. unset($parts[\count($parts) - 1]);
  108. foreach ($parts as $part) {
  109. if (null !== $array && !\array_key_exists($part, $array)) {
  110. if (!$writeContext) {
  111. $null = null;
  112. return $null;
  113. }
  114. $array[$part] = [];
  115. }
  116. $array = &$array[$part];
  117. }
  118. return $array;
  119. }
  120. /**
  121. * Resolves the key from the name.
  122. *
  123. * This is the last part in a dot separated string.
  124. *
  125. * @return string
  126. */
  127. protected function resolveKey(string $name)
  128. {
  129. if (false !== $pos = strrpos($name, $this->namespaceCharacter)) {
  130. $name = substr($name, $pos + 1);
  131. }
  132. return $name;
  133. }
  134. }