|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the Symfony package. |
|
5 * |
|
6 * (c) Fabien Potencier <fabien@symfony.com> |
|
7 * |
|
8 * For the full copyright and license information, please view the LICENSE |
|
9 * file that was distributed with this source code. |
|
10 */ |
|
11 |
|
12 namespace Symfony\Component\Config\Definition; |
|
13 |
|
14 use Symfony\Component\Config\Definition\Exception\ForbiddenOverwriteException; |
|
15 |
|
16 use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
|
17 use Symfony\Component\Config\Definition\Exception\DuplicateKeyException; |
|
18 use Symfony\Component\Config\Definition\Exception\InvalidTypeException; |
|
19 use Symfony\Component\Config\Definition\Exception\UnsetKeyException; |
|
20 use Symfony\Component\Config\Definition\Builder\NodeDefinition; |
|
21 |
|
22 /** |
|
23 * Represents an Array node in the config tree. |
|
24 * |
|
25 * @author Johannes M. Schmitt <schmittjoh@gmail.com> |
|
26 */ |
|
27 class ArrayNode extends BaseNode implements PrototypeNodeInterface |
|
28 { |
|
29 protected $xmlRemappings; |
|
30 protected $children; |
|
31 protected $allowFalse; |
|
32 protected $allowNewKeys; |
|
33 protected $addIfNotSet; |
|
34 protected $performDeepMerging; |
|
35 protected $ignoreExtraKeys; |
|
36 |
|
37 /** |
|
38 * Constructor. |
|
39 * |
|
40 * @param string $name The Node's name |
|
41 * @param NodeInterface $parent The node parent |
|
42 */ |
|
43 public function __construct($name, NodeInterface $parent = null) |
|
44 { |
|
45 parent::__construct($name, $parent); |
|
46 |
|
47 $this->children = array(); |
|
48 $this->xmlRemappings = array(); |
|
49 $this->removeKeyAttribute = true; |
|
50 $this->allowFalse = false; |
|
51 $this->addIfNotSet = false; |
|
52 $this->allowNewKeys = true; |
|
53 $this->performDeepMerging = true; |
|
54 } |
|
55 |
|
56 /** |
|
57 * Sets the xml remappings that should be performed. |
|
58 * |
|
59 * @param array $remappings an array of the form array(array(string, string)) |
|
60 */ |
|
61 public function setXmlRemappings(array $remappings) |
|
62 { |
|
63 $this->xmlRemappings = $remappings; |
|
64 } |
|
65 |
|
66 /** |
|
67 * Sets whether to add default values for this array if it has not been |
|
68 * defined in any of the configuration files. |
|
69 * |
|
70 * @param Boolean $boolean |
|
71 */ |
|
72 public function setAddIfNotSet($boolean) |
|
73 { |
|
74 $this->addIfNotSet = (Boolean) $boolean; |
|
75 } |
|
76 |
|
77 /** |
|
78 * Sets whether false is allowed as value indicating that the array should |
|
79 * be unset. |
|
80 * |
|
81 * @param Boolean $allow |
|
82 */ |
|
83 public function setAllowFalse($allow) |
|
84 { |
|
85 $this->allowFalse = (Boolean) $allow; |
|
86 } |
|
87 |
|
88 /** |
|
89 * Sets whether new keys can be defined in subsequent configurations. |
|
90 * |
|
91 * @param Boolean $allow |
|
92 */ |
|
93 public function setAllowNewKeys($allow) |
|
94 { |
|
95 $this->allowNewKeys = (Boolean) $allow; |
|
96 } |
|
97 |
|
98 /** |
|
99 * Sets if deep merging should occur. |
|
100 * |
|
101 * @param Boolean $boolean |
|
102 */ |
|
103 public function setPerformDeepMerging($boolean) |
|
104 { |
|
105 $this->performDeepMerging = (Boolean) $boolean; |
|
106 } |
|
107 |
|
108 /** |
|
109 * Whether extra keys should just be ignore without an exception. |
|
110 * |
|
111 * @param Boolean $boolean To allow extra keys |
|
112 */ |
|
113 public function setIgnoreExtraKeys($boolean) |
|
114 { |
|
115 $this->ignoreExtraKeys = (Boolean) $boolean; |
|
116 } |
|
117 |
|
118 /** |
|
119 * Sets the node Name. |
|
120 * |
|
121 * @param string $name The node's name |
|
122 */ |
|
123 public function setName($name) |
|
124 { |
|
125 $this->name = $name; |
|
126 } |
|
127 |
|
128 /** |
|
129 * Checks if the node has a default value. |
|
130 * |
|
131 * @return Boolean |
|
132 */ |
|
133 public function hasDefaultValue() |
|
134 { |
|
135 return $this->addIfNotSet; |
|
136 } |
|
137 |
|
138 /** |
|
139 * Retrieves the default value. |
|
140 * |
|
141 * @return array The default value |
|
142 * @throws \RuntimeException if the node has no default value |
|
143 */ |
|
144 public function getDefaultValue() |
|
145 { |
|
146 if (!$this->hasDefaultValue()) { |
|
147 throw new \RuntimeException(sprintf('The node at path "%s" has no default value.', $this->getPath())); |
|
148 } |
|
149 |
|
150 $defaults = array(); |
|
151 foreach ($this->children as $name => $child) { |
|
152 if ($child->hasDefaultValue()) { |
|
153 $defaults[$name] = $child->getDefaultValue(); |
|
154 } |
|
155 } |
|
156 |
|
157 return $defaults; |
|
158 } |
|
159 |
|
160 /** |
|
161 * Adds a child node. |
|
162 * |
|
163 * @param NodeInterface $node The child node to add |
|
164 * @throws \InvalidArgumentException when the child node has no name |
|
165 * @throws \InvalidArgumentException when the child node's name is not unique |
|
166 */ |
|
167 public function addChild(NodeInterface $node) |
|
168 { |
|
169 $name = $node->getName(); |
|
170 if (empty($name)) { |
|
171 throw new \InvalidArgumentException('Child nodes must be named.'); |
|
172 } |
|
173 if (isset($this->children[$name])) { |
|
174 throw new \InvalidArgumentException(sprintf('A child node named "%s" already exists.', $name)); |
|
175 } |
|
176 |
|
177 $this->children[$name] = $node; |
|
178 } |
|
179 |
|
180 /** |
|
181 * Finalizes the value of this node. |
|
182 * |
|
183 * @param mixed $value |
|
184 * @return mixed The finalised value |
|
185 * @throws UnsetKeyException |
|
186 * @throws InvalidConfigurationException if the node doesn't have enough children |
|
187 */ |
|
188 protected function finalizeValue($value) |
|
189 { |
|
190 if (false === $value) { |
|
191 $msg = sprintf('Unsetting key for path "%s", value: %s', $this->getPath(), json_encode($value)); |
|
192 throw new UnsetKeyException($msg); |
|
193 } |
|
194 |
|
195 foreach ($this->children as $name => $child) { |
|
196 if (!array_key_exists($name, $value)) { |
|
197 if ($child->isRequired()) { |
|
198 $msg = sprintf('The child node "%s" at path "%s" must be configured.', $name, $this->getPath()); |
|
199 $ex = new InvalidConfigurationException($msg); |
|
200 $ex->setPath($this->getPath()); |
|
201 |
|
202 throw $ex; |
|
203 } |
|
204 |
|
205 if ($child->hasDefaultValue()) { |
|
206 $value[$name] = $child->getDefaultValue(); |
|
207 } |
|
208 |
|
209 continue; |
|
210 } |
|
211 |
|
212 try { |
|
213 $value[$name] = $child->finalize($value[$name]); |
|
214 } catch (UnsetKeyException $unset) { |
|
215 unset($value[$name]); |
|
216 } |
|
217 } |
|
218 |
|
219 return $value; |
|
220 } |
|
221 |
|
222 /** |
|
223 * Validates the type of the value. |
|
224 * |
|
225 * @param mixed $value |
|
226 * @throws InvalidTypeException |
|
227 */ |
|
228 protected function validateType($value) |
|
229 { |
|
230 if (!is_array($value) && (!$this->allowFalse || false !== $value)) { |
|
231 $ex = new InvalidTypeException(sprintf( |
|
232 'Invalid type for path "%s". Expected array, but got %s', |
|
233 $this->getPath(), |
|
234 gettype($value) |
|
235 )); |
|
236 $ex->setPath($this->getPath()); |
|
237 |
|
238 throw $ex; |
|
239 } |
|
240 } |
|
241 |
|
242 /** |
|
243 * Normalizes the value. |
|
244 * |
|
245 * @param mixed $value The value to normalize |
|
246 * @return mixed The normalized value |
|
247 */ |
|
248 protected function normalizeValue($value) |
|
249 { |
|
250 if (false === $value) { |
|
251 return $value; |
|
252 } |
|
253 |
|
254 $value = $this->remapXml($value); |
|
255 |
|
256 $normalized = array(); |
|
257 foreach ($this->children as $name => $child) { |
|
258 if (array_key_exists($name, $value)) { |
|
259 $normalized[$name] = $child->normalize($value[$name]); |
|
260 unset($value[$name]); |
|
261 } |
|
262 } |
|
263 |
|
264 // if extra fields are present, throw exception |
|
265 if (count($value) && !$this->ignoreExtraKeys) { |
|
266 $msg = sprintf('Unrecognized options "%s" under "%s"', implode(', ', array_keys($value)), $this->getPath()); |
|
267 $ex = new InvalidConfigurationException($msg); |
|
268 $ex->setPath($this->getPath().'.'.reset($value)); |
|
269 |
|
270 throw $ex; |
|
271 } |
|
272 |
|
273 return $normalized; |
|
274 } |
|
275 |
|
276 /** |
|
277 * Remap multiple singular values to a single plural value |
|
278 * |
|
279 * @param array $value The source values |
|
280 * @return array The remapped values |
|
281 */ |
|
282 protected function remapXml($value) |
|
283 { |
|
284 foreach ($this->xmlRemappings as $transformation) { |
|
285 list($singular, $plural) = $transformation; |
|
286 |
|
287 if (!isset($value[$singular])) { |
|
288 continue; |
|
289 } |
|
290 |
|
291 $value[$plural] = Processor::normalizeConfig($value, $singular, $plural); |
|
292 unset($value[$singular]); |
|
293 } |
|
294 |
|
295 return $value; |
|
296 } |
|
297 |
|
298 /** |
|
299 * Merges values together. |
|
300 * |
|
301 * @param mixed $leftSide The left side to merge. |
|
302 * @param mixed $rightSide The right side to merge. |
|
303 * @return mixed The merged values |
|
304 * @throws InvalidConfigurationException |
|
305 * @throws \RuntimeException |
|
306 */ |
|
307 protected function mergeValues($leftSide, $rightSide) |
|
308 { |
|
309 if (false === $rightSide) { |
|
310 // if this is still false after the last config has been merged the |
|
311 // finalization pass will take care of removing this key entirely |
|
312 return false; |
|
313 } |
|
314 |
|
315 if (false === $leftSide || !$this->performDeepMerging) { |
|
316 return $rightSide; |
|
317 } |
|
318 |
|
319 foreach ($rightSide as $k => $v) { |
|
320 // no conflict |
|
321 if (!array_key_exists($k, $leftSide)) { |
|
322 if (!$this->allowNewKeys) { |
|
323 $ex = new InvalidConfigurationException(sprintf( |
|
324 'You are not allowed to define new elements for path "%s". ' |
|
325 .'Please define all elements for this path in one config file. ' |
|
326 .'If you are trying to overwrite an element, make sure you redefine it ' |
|
327 .'with the same name.', |
|
328 $this->getPath() |
|
329 )); |
|
330 $ex->setPath($this->getPath()); |
|
331 |
|
332 throw $ex; |
|
333 } |
|
334 |
|
335 $leftSide[$k] = $v; |
|
336 continue; |
|
337 } |
|
338 |
|
339 if (!isset($this->children[$k])) { |
|
340 throw new \RuntimeException('merge() expects a normalized config array.'); |
|
341 } |
|
342 |
|
343 $leftSide[$k] = $this->children[$k]->merge($leftSide[$k], $v); |
|
344 } |
|
345 |
|
346 return $leftSide; |
|
347 } |
|
348 } |