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