Verzeichnisstruktur phpBB-3.1.0
- Veröffentlicht
- 27.10.2014
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
BaseNode.php
001 <?php
002
003 /*
004 * This file is part of the Symfony package.
005 *
006 * (c) Fabien Potencier <fabien@symfony.com>
007 *
008 * For the full copyright and license information, please view the LICENSE
009 * file that was distributed with this source code.
010 */
011
012 namespace Symfony\Component\Config\Definition;
013
014 use Symfony\Component\Config\Definition\Exception\Exception;
015 use Symfony\Component\Config\Definition\Exception\ForbiddenOverwriteException;
016 use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
017
018 /**
019 * The base node class
020 *
021 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
022 */
023 abstract class BaseNode implements NodeInterface
024 {
025 protected $name;
026 protected $parent;
027 protected $normalizationClosures;
028 protected $finalValidationClosures;
029 protected $allowOverwrite;
030 protected $required;
031 protected $equivalentValues;
032 protected $attributes = array();
033
034 /**
035 * Constructor.
036 *
037 * @param string $name The name of the node
038 * @param NodeInterface $parent The parent of this node
039 *
040 * @throws \InvalidArgumentException if the name contains a period.
041 */
042 public function __construct($name, NodeInterface $parent = null)
043 {
044 if (false !== strpos($name, '.')) {
045 throw new \InvalidArgumentException('The name must not contain ".".');
046 }
047
048 $this->name = $name;
049 $this->parent = $parent;
050 $this->normalizationClosures = array();
051 $this->finalValidationClosures = array();
052 $this->allowOverwrite = true;
053 $this->required = false;
054 $this->equivalentValues = array();
055 }
056
057 public function setAttribute($key, $value)
058 {
059 $this->attributes[$key] = $value;
060 }
061
062 public function getAttribute($key, $default = null)
063 {
064 return isset($this->attributes[$key]) ? $this->attributes[$key] : $default;
065 }
066
067 public function hasAttribute($key)
068 {
069 return isset($this->attributes[$key]);
070 }
071
072 public function getAttributes()
073 {
074 return $this->attributes;
075 }
076
077 public function setAttributes(array $attributes)
078 {
079 $this->attributes = $attributes;
080 }
081
082 public function removeAttribute($key)
083 {
084 unset($this->attributes[$key]);
085 }
086
087 /**
088 * Sets an info message.
089 *
090 * @param string $info
091 */
092 public function setInfo($info)
093 {
094 $this->setAttribute('info', $info);
095 }
096
097 /**
098 * Returns info message.
099 *
100 * @return string The info text
101 */
102 public function getInfo()
103 {
104 return $this->getAttribute('info');
105 }
106
107 /**
108 * Sets the example configuration for this node.
109 *
110 * @param string|array $example
111 */
112 public function setExample($example)
113 {
114 $this->setAttribute('example', $example);
115 }
116
117 /**
118 * Retrieves the example configuration for this node.
119 *
120 * @return string|array The example
121 */
122 public function getExample()
123 {
124 return $this->getAttribute('example');
125 }
126
127 /**
128 * Adds an equivalent value.
129 *
130 * @param mixed $originalValue
131 * @param mixed $equivalentValue
132 */
133 public function addEquivalentValue($originalValue, $equivalentValue)
134 {
135 $this->equivalentValues[] = array($originalValue, $equivalentValue);
136 }
137
138 /**
139 * Set this node as required.
140 *
141 * @param bool $boolean Required node
142 */
143 public function setRequired($boolean)
144 {
145 $this->required = (bool) $boolean;
146 }
147
148 /**
149 * Sets if this node can be overridden.
150 *
151 * @param bool $allow
152 */
153 public function setAllowOverwrite($allow)
154 {
155 $this->allowOverwrite = (bool) $allow;
156 }
157
158 /**
159 * Sets the closures used for normalization.
160 *
161 * @param \Closure[] $closures An array of Closures used for normalization
162 */
163 public function setNormalizationClosures(array $closures)
164 {
165 $this->normalizationClosures = $closures;
166 }
167
168 /**
169 * Sets the closures used for final validation.
170 *
171 * @param \Closure[] $closures An array of Closures used for final validation
172 */
173 public function setFinalValidationClosures(array $closures)
174 {
175 $this->finalValidationClosures = $closures;
176 }
177
178 /**
179 * Checks if this node is required.
180 *
181 * @return bool
182 */
183 public function isRequired()
184 {
185 return $this->required;
186 }
187
188 /**
189 * Returns the name of this node
190 *
191 * @return string The Node's name.
192 */
193 public function getName()
194 {
195 return $this->name;
196 }
197
198 /**
199 * Retrieves the path of this node.
200 *
201 * @return string The Node's path
202 */
203 public function getPath()
204 {
205 $path = $this->name;
206
207 if (null !== $this->parent) {
208 $path = $this->parent->getPath().'.'.$path;
209 }
210
211 return $path;
212 }
213
214 /**
215 * Merges two values together.
216 *
217 * @param mixed $leftSide
218 * @param mixed $rightSide
219 *
220 * @return mixed The merged value
221 *
222 * @throws ForbiddenOverwriteException
223 */
224 final public function merge($leftSide, $rightSide)
225 {
226 if (!$this->allowOverwrite) {
227 throw new ForbiddenOverwriteException(sprintf(
228 'Configuration path "%s" cannot be overwritten. You have to '
229 .'define all options for this path, and any of its sub-paths in '
230 .'one configuration section.',
231 $this->getPath()
232 ));
233 }
234
235 $this->validateType($leftSide);
236 $this->validateType($rightSide);
237
238 return $this->mergeValues($leftSide, $rightSide);
239 }
240
241 /**
242 * Normalizes a value, applying all normalization closures.
243 *
244 * @param mixed $value Value to normalize.
245 *
246 * @return mixed The normalized value.
247 */
248 final public function normalize($value)
249 {
250 $value = $this->preNormalize($value);
251
252 // run custom normalization closures
253 foreach ($this->normalizationClosures as $closure) {
254 $value = $closure($value);
255 }
256
257 // replace value with their equivalent
258 foreach ($this->equivalentValues as $data) {
259 if ($data[0] === $value) {
260 $value = $data[1];
261 }
262 }
263
264 // validate type
265 $this->validateType($value);
266
267 // normalize value
268 return $this->normalizeValue($value);
269 }
270
271 /**
272 * Normalizes the value before any other normalization is applied.
273 *
274 * @param $value
275 *
276 * @return $value The normalized array value
277 */
278 protected function preNormalize($value)
279 {
280 return $value;
281 }
282
283 /**
284 * Finalizes a value, applying all finalization closures.
285 *
286 * @param mixed $value The value to finalize
287 *
288 * @return mixed The finalized value
289 *
290 * @throws Exception
291 * @throws InvalidConfigurationException
292 */
293 final public function finalize($value)
294 {
295 $this->validateType($value);
296
297 $value = $this->finalizeValue($value);
298
299 // Perform validation on the final value if a closure has been set.
300 // The closure is also allowed to return another value.
301 foreach ($this->finalValidationClosures as $closure) {
302 try {
303 $value = $closure($value);
304 } catch (Exception $correctEx) {
305 throw $correctEx;
306 } catch (\Exception $invalid) {
307 throw new InvalidConfigurationException(sprintf(
308 'Invalid configuration for path "%s": %s',
309 $this->getPath(),
310 $invalid->getMessage()
311 ), $invalid->getCode(), $invalid);
312 }
313 }
314
315 return $value;
316 }
317
318 /**
319 * Validates the type of a Node.
320 *
321 * @param mixed $value The value to validate
322 *
323 * @throws InvalidTypeException when the value is invalid
324 */
325 abstract protected function validateType($value);
326
327 /**
328 * Normalizes the value.
329 *
330 * @param mixed $value The value to normalize.
331 *
332 * @return mixed The normalized value
333 */
334 abstract protected function normalizeValue($value);
335
336 /**
337 * Merges two values together.
338 *
339 * @param mixed $leftSide
340 * @param mixed $rightSide
341 *
342 * @return mixed The merged value
343 */
344 abstract protected function mergeValues($leftSide, $rightSide);
345
346 /**
347 * Finalizes a value.
348 *
349 * @param mixed $value The value to finalize
350 *
351 * @return mixed The finalized value
352 */
353 abstract protected function finalizeValue($value);
354 }
355