NamespacedAttributeBag.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. $attributes = $this->resolveAttributePath($name);
  42. $name = $this->resolveKey($name);
  43. if (null === $attributes) {
  44. return false;
  45. }
  46. return array_key_exists($name, $attributes);
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function get($name, $default = null)
  52. {
  53. $attributes = $this->resolveAttributePath($name);
  54. $name = $this->resolveKey($name);
  55. if (null === $attributes) {
  56. return $default;
  57. }
  58. return array_key_exists($name, $attributes) ? $attributes[$name] : $default;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function set($name, $value)
  64. {
  65. $attributes = & $this->resolveAttributePath($name, true);
  66. $name = $this->resolveKey($name);
  67. $attributes[$name] = $value;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function remove($name)
  73. {
  74. $retval = null;
  75. $attributes = & $this->resolveAttributePath($name);
  76. $name = $this->resolveKey($name);
  77. if (null !== $attributes && array_key_exists($name, $attributes)) {
  78. $retval = $attributes[$name];
  79. unset($attributes[$name]);
  80. }
  81. return $retval;
  82. }
  83. /**
  84. * Resolves a path in attributes property and returns it as a reference.
  85. *
  86. * This method allows structured namespacing of session attributes.
  87. *
  88. * @param string $name Key name
  89. * @param boolean $writeContext Write context, default false
  90. *
  91. * @return array
  92. */
  93. protected function &resolveAttributePath($name, $writeContext = false)
  94. {
  95. $array = & $this->attributes;
  96. $name = (strpos($name, $this->namespaceCharacter) === 0) ? substr($name, 1) : $name;
  97. // Check if there is anything to do, else return
  98. if (!$name) {
  99. return $array;
  100. }
  101. $parts = explode($this->namespaceCharacter, $name);
  102. if (count($parts) < 2) {
  103. if (!$writeContext) {
  104. return $array;
  105. }
  106. $array[$parts[0]] = array();
  107. return $array;
  108. }
  109. unset($parts[count($parts)-1]);
  110. foreach ($parts as $part) {
  111. if (null !== $array && !array_key_exists($part, $array)) {
  112. $array[$part] = $writeContext ? array() : null;
  113. }
  114. $array = & $array[$part];
  115. }
  116. return $array;
  117. }
  118. /**
  119. * Resolves the key from the name.
  120. *
  121. * This is the last part in a dot separated string.
  122. *
  123. * @param string $name
  124. *
  125. * @return string
  126. */
  127. protected function resolveKey($name)
  128. {
  129. if (strpos($name, $this->namespaceCharacter) !== false) {
  130. $name = substr($name, strrpos($name, $this->namespaceCharacter)+1, strlen($name));
  131. }
  132. return $name;
  133. }
  134. }