NamespacedAttributeBag.php 3.7 KB

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