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