Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\HttpFoundation\Session\Attribute;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * This class provides structured storage of session attributes using
|
Chris@0
|
16 * a name spacing character in the key.
|
Chris@0
|
17 *
|
Chris@0
|
18 * @author Drak <drak@zikula.org>
|
Chris@0
|
19 */
|
Chris@0
|
20 class NamespacedAttributeBag extends AttributeBag
|
Chris@0
|
21 {
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Namespace character.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @var string
|
Chris@0
|
26 */
|
Chris@0
|
27 private $namespaceCharacter;
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * Constructor.
|
Chris@0
|
31 *
|
Chris@0
|
32 * @param string $storageKey Session storage key
|
Chris@0
|
33 * @param string $namespaceCharacter Namespace character to use in keys
|
Chris@0
|
34 */
|
Chris@0
|
35 public function __construct($storageKey = '_sf2_attributes', $namespaceCharacter = '/')
|
Chris@0
|
36 {
|
Chris@0
|
37 $this->namespaceCharacter = $namespaceCharacter;
|
Chris@0
|
38 parent::__construct($storageKey);
|
Chris@0
|
39 }
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * {@inheritdoc}
|
Chris@0
|
43 */
|
Chris@0
|
44 public function has($name)
|
Chris@0
|
45 {
|
Chris@0
|
46 // reference mismatch: if fixed, re-introduced in array_key_exists; keep as it is
|
Chris@0
|
47 $attributes = $this->resolveAttributePath($name);
|
Chris@0
|
48 $name = $this->resolveKey($name);
|
Chris@0
|
49
|
Chris@0
|
50 if (null === $attributes) {
|
Chris@0
|
51 return false;
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 return array_key_exists($name, $attributes);
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * {@inheritdoc}
|
Chris@0
|
59 */
|
Chris@0
|
60 public function get($name, $default = null)
|
Chris@0
|
61 {
|
Chris@0
|
62 // reference mismatch: if fixed, re-introduced in array_key_exists; keep as it is
|
Chris@0
|
63 $attributes = $this->resolveAttributePath($name);
|
Chris@0
|
64 $name = $this->resolveKey($name);
|
Chris@0
|
65
|
Chris@0
|
66 if (null === $attributes) {
|
Chris@0
|
67 return $default;
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 return array_key_exists($name, $attributes) ? $attributes[$name] : $default;
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 /**
|
Chris@0
|
74 * {@inheritdoc}
|
Chris@0
|
75 */
|
Chris@0
|
76 public function set($name, $value)
|
Chris@0
|
77 {
|
Chris@0
|
78 $attributes = &$this->resolveAttributePath($name, true);
|
Chris@0
|
79 $name = $this->resolveKey($name);
|
Chris@0
|
80 $attributes[$name] = $value;
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 /**
|
Chris@0
|
84 * {@inheritdoc}
|
Chris@0
|
85 */
|
Chris@0
|
86 public function remove($name)
|
Chris@0
|
87 {
|
Chris@0
|
88 $retval = null;
|
Chris@0
|
89 $attributes = &$this->resolveAttributePath($name);
|
Chris@0
|
90 $name = $this->resolveKey($name);
|
Chris@0
|
91 if (null !== $attributes && array_key_exists($name, $attributes)) {
|
Chris@0
|
92 $retval = $attributes[$name];
|
Chris@0
|
93 unset($attributes[$name]);
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@0
|
96 return $retval;
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 /**
|
Chris@0
|
100 * Resolves a path in attributes property and returns it as a reference.
|
Chris@0
|
101 *
|
Chris@0
|
102 * This method allows structured namespacing of session attributes.
|
Chris@0
|
103 *
|
Chris@0
|
104 * @param string $name Key name
|
Chris@0
|
105 * @param bool $writeContext Write context, default false
|
Chris@0
|
106 *
|
Chris@0
|
107 * @return array
|
Chris@0
|
108 */
|
Chris@0
|
109 protected function &resolveAttributePath($name, $writeContext = false)
|
Chris@0
|
110 {
|
Chris@0
|
111 $array = &$this->attributes;
|
Chris@0
|
112 $name = (strpos($name, $this->namespaceCharacter) === 0) ? substr($name, 1) : $name;
|
Chris@0
|
113
|
Chris@0
|
114 // Check if there is anything to do, else return
|
Chris@0
|
115 if (!$name) {
|
Chris@0
|
116 return $array;
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 $parts = explode($this->namespaceCharacter, $name);
|
Chris@0
|
120 if (count($parts) < 2) {
|
Chris@0
|
121 if (!$writeContext) {
|
Chris@0
|
122 return $array;
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 $array[$parts[0]] = array();
|
Chris@0
|
126
|
Chris@0
|
127 return $array;
|
Chris@0
|
128 }
|
Chris@0
|
129
|
Chris@0
|
130 unset($parts[count($parts) - 1]);
|
Chris@0
|
131
|
Chris@0
|
132 foreach ($parts as $part) {
|
Chris@0
|
133 if (null !== $array && !array_key_exists($part, $array)) {
|
Chris@0
|
134 $array[$part] = $writeContext ? array() : null;
|
Chris@0
|
135 }
|
Chris@0
|
136
|
Chris@0
|
137 $array = &$array[$part];
|
Chris@0
|
138 }
|
Chris@0
|
139
|
Chris@0
|
140 return $array;
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@0
|
143 /**
|
Chris@0
|
144 * Resolves the key from the name.
|
Chris@0
|
145 *
|
Chris@0
|
146 * This is the last part in a dot separated string.
|
Chris@0
|
147 *
|
Chris@0
|
148 * @param string $name
|
Chris@0
|
149 *
|
Chris@0
|
150 * @return string
|
Chris@0
|
151 */
|
Chris@0
|
152 protected function resolveKey($name)
|
Chris@0
|
153 {
|
Chris@0
|
154 if (false !== $pos = strrpos($name, $this->namespaceCharacter)) {
|
Chris@0
|
155 $name = substr($name, $pos + 1);
|
Chris@0
|
156 }
|
Chris@0
|
157
|
Chris@0
|
158 return $name;
|
Chris@0
|
159 }
|
Chris@0
|
160 }
|