4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\Session\Attribute;
15 * This class provides structured storage of session attributes using
16 * a name spacing character in the key.
18 * @author Drak <drak@zikula.org>
20 class NamespacedAttributeBag extends AttributeBag
23 * Namespace character.
27 private $namespaceCharacter;
32 * @param string $storageKey Session storage key
33 * @param string $namespaceCharacter Namespace character to use in keys
35 public function __construct($storageKey = '_sf2_attributes', $namespaceCharacter = '/')
37 $this->namespaceCharacter = $namespaceCharacter;
38 parent::__construct($storageKey);
44 public function has($name)
46 // reference mismatch: if fixed, re-introduced in array_key_exists; keep as it is
47 $attributes = $this->resolveAttributePath($name);
48 $name = $this->resolveKey($name);
50 if (null === $attributes) {
54 return array_key_exists($name, $attributes);
60 public function get($name, $default = null)
62 // reference mismatch: if fixed, re-introduced in array_key_exists; keep as it is
63 $attributes = $this->resolveAttributePath($name);
64 $name = $this->resolveKey($name);
66 if (null === $attributes) {
70 return array_key_exists($name, $attributes) ? $attributes[$name] : $default;
76 public function set($name, $value)
78 $attributes = &$this->resolveAttributePath($name, true);
79 $name = $this->resolveKey($name);
80 $attributes[$name] = $value;
86 public function remove($name)
89 $attributes = &$this->resolveAttributePath($name);
90 $name = $this->resolveKey($name);
91 if (null !== $attributes && array_key_exists($name, $attributes)) {
92 $retval = $attributes[$name];
93 unset($attributes[$name]);
100 * Resolves a path in attributes property and returns it as a reference.
102 * This method allows structured namespacing of session attributes.
104 * @param string $name Key name
105 * @param bool $writeContext Write context, default false
109 protected function &resolveAttributePath($name, $writeContext = false)
111 $array = &$this->attributes;
112 $name = (strpos($name, $this->namespaceCharacter) === 0) ? substr($name, 1) : $name;
114 // Check if there is anything to do, else return
119 $parts = explode($this->namespaceCharacter, $name);
120 if (count($parts) < 2) {
121 if (!$writeContext) {
125 $array[$parts[0]] = array();
130 unset($parts[count($parts) - 1]);
132 foreach ($parts as $part) {
133 if (null !== $array && !array_key_exists($part, $array)) {
134 $array[$part] = $writeContext ? array() : null;
137 $array = &$array[$part];
144 * Resolves the key from the name.
146 * This is the last part in a dot separated string.
148 * @param string $name
152 protected function resolveKey($name)
154 if (false !== $pos = strrpos($name, $this->namespaceCharacter)) {
155 $name = substr($name, $pos + 1);