Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
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 |
ParameterBag.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\DependencyInjection\ParameterBag;
013
014 use Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException;
015 use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
016 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
017
018 /**
019 * Holds parameters.
020 *
021 * @author Fabien Potencier <fabien@symfony.com>
022 */
023 class ParameterBag implements ParameterBagInterface
024 {
025 protected $parameters = [];
026 protected $resolved = false;
027
028 private $normalizedNames = [];
029
030 /**
031 * @param array $parameters An array of parameters
032 */
033 public function __construct(array $parameters = [])
034 {
035 $this->add($parameters);
036 }
037
038 /**
039 * Clears all parameters.
040 */
041 public function clear()
042 {
043 $this->parameters = [];
044 }
045
046 /**
047 * Adds parameters to the service container parameters.
048 *
049 * @param array $parameters An array of parameters
050 */
051 public function add(array $parameters)
052 {
053 foreach ($parameters as $key => $value) {
054 $this->set($key, $value);
055 }
056 }
057
058 /**
059 * {@inheritdoc}
060 */
061 public function all()
062 {
063 return $this->parameters;
064 }
065
066 /**
067 * {@inheritdoc}
068 */
069 public function get($name)
070 {
071 $name = $this->normalizeName($name);
072
073 if (!\array_key_exists($name, $this->parameters)) {
074 if (!$name) {
075 throw new ParameterNotFoundException($name);
076 }
077
078 $alternatives = [];
079 foreach ($this->parameters as $key => $parameterValue) {
080 $lev = levenshtein($name, $key);
081 if ($lev <= \strlen($name) / 3 || false !== strpos($key, $name)) {
082 $alternatives[] = $key;
083 }
084 }
085
086 $nonNestedAlternative = null;
087 if (!\count($alternatives) && false !== strpos($name, '.')) {
088 $namePartsLength = array_map('strlen', explode('.', $name));
089 $key = substr($name, 0, -1 * (1 + array_pop($namePartsLength)));
090 while (\count($namePartsLength)) {
091 if ($this->has($key)) {
092 if (\is_array($this->get($key))) {
093 $nonNestedAlternative = $key;
094 }
095 break;
096 }
097
098 $key = substr($key, 0, -1 * (1 + array_pop($namePartsLength)));
099 }
100 }
101
102 throw new ParameterNotFoundException($name, null, null, null, $alternatives, $nonNestedAlternative);
103 }
104
105 return $this->parameters[$name];
106 }
107
108 /**
109 * Sets a service container parameter.
110 *
111 * @param string $name The parameter name
112 * @param mixed $value The parameter value
113 */
114 public function set($name, $value)
115 {
116 $this->parameters[$this->normalizeName($name)] = $value;
117 }
118
119 /**
120 * {@inheritdoc}
121 */
122 public function has($name)
123 {
124 return \array_key_exists($this->normalizeName($name), $this->parameters);
125 }
126
127 /**
128 * Removes a parameter.
129 *
130 * @param string $name The parameter name
131 */
132 public function remove($name)
133 {
134 unset($this->parameters[$this->normalizeName($name)]);
135 }
136
137 /**
138 * {@inheritdoc}
139 */
140 public function resolve()
141 {
142 if ($this->resolved) {
143 return;
144 }
145
146 $parameters = [];
147 foreach ($this->parameters as $key => $value) {
148 try {
149 $value = $this->resolveValue($value);
150 $parameters[$key] = $this->unescapeValue($value);
151 } catch (ParameterNotFoundException $e) {
152 $e->setSourceKey($key);
153
154 throw $e;
155 }
156 }
157
158 $this->parameters = $parameters;
159 $this->resolved = true;
160 }
161
162 /**
163 * Replaces parameter placeholders (%name%) by their values.
164 *
165 * @param mixed $value A value
166 * @param array $resolving An array of keys that are being resolved (used internally to detect circular references)
167 *
168 * @return mixed The resolved value
169 *
170 * @throws ParameterNotFoundException if a placeholder references a parameter that does not exist
171 * @throws ParameterCircularReferenceException if a circular reference if detected
172 * @throws RuntimeException when a given parameter has a type problem
173 */
174 public function resolveValue($value, array $resolving = [])
175 {
176 if (\is_array($value)) {
177 $args = [];
178 foreach ($value as $k => $v) {
179 $args[\is_string($k) ? $this->resolveValue($k, $resolving) : $k] = $this->resolveValue($v, $resolving);
180 }
181
182 return $args;
183 }
184
185 if (!\is_string($value) || 2 > \strlen($value)) {
186 return $value;
187 }
188
189 return $this->resolveString($value, $resolving);
190 }
191
192 /**
193 * Resolves parameters inside a string.
194 *
195 * @param string $value The string to resolve
196 * @param array $resolving An array of keys that are being resolved (used internally to detect circular references)
197 *
198 * @return mixed The resolved string
199 *
200 * @throws ParameterNotFoundException if a placeholder references a parameter that does not exist
201 * @throws ParameterCircularReferenceException if a circular reference if detected
202 * @throws RuntimeException when a given parameter has a type problem
203 */
204 public function resolveString($value, array $resolving = [])
205 {
206 // we do this to deal with non string values (Boolean, integer, ...)
207 // as the preg_replace_callback throw an exception when trying
208 // a non-string in a parameter value
209 if (preg_match('/^%([^%\s]+)%$/', $value, $match)) {
210 $key = $match[1];
211 $lcKey = strtolower($key); // strtolower() to be removed in 4.0
212
213 if (isset($resolving[$lcKey])) {
214 throw new ParameterCircularReferenceException(array_keys($resolving));
215 }
216
217 $resolving[$lcKey] = true;
218
219 return $this->resolved ? $this->get($key) : $this->resolveValue($this->get($key), $resolving);
220 }
221
222 return preg_replace_callback('/%%|%([^%\s]+)%/', function ($match) use ($resolving, $value) {
223 // skip %%
224 if (!isset($match[1])) {
225 return '%%';
226 }
227
228 $key = $match[1];
229 $lcKey = strtolower($key); // strtolower() to be removed in 4.0
230 if (isset($resolving[$lcKey])) {
231 throw new ParameterCircularReferenceException(array_keys($resolving));
232 }
233
234 $resolved = $this->get($key);
235
236 if (!\is_string($resolved) && !is_numeric($resolved)) {
237 throw new RuntimeException(sprintf('A string value must be composed of strings and/or numbers, but found parameter "%s" of type "%s" inside string value "%s".', $key, \gettype($resolved), $value));
238 }
239
240 $resolved = (string) $resolved;
241 $resolving[$lcKey] = true;
242
243 return $this->isResolved() ? $resolved : $this->resolveString($resolved, $resolving);
244 }, $value);
245 }
246
247 public function isResolved()
248 {
249 return $this->resolved;
250 }
251
252 /**
253 * {@inheritdoc}
254 */
255 public function escapeValue($value)
256 {
257 if (\is_string($value)) {
258 return str_replace('%', '%%', $value);
259 }
260
261 if (\is_array($value)) {
262 $result = [];
263 foreach ($value as $k => $v) {
264 $result[$k] = $this->escapeValue($v);
265 }
266
267 return $result;
268 }
269
270 return $value;
271 }
272
273 /**
274 * {@inheritdoc}
275 */
276 public function unescapeValue($value)
277 {
278 if (\is_string($value)) {
279 return str_replace('%%', '%', $value);
280 }
281
282 if (\is_array($value)) {
283 $result = [];
284 foreach ($value as $k => $v) {
285 $result[$k] = $this->unescapeValue($v);
286 }
287
288 return $result;
289 }
290
291 return $value;
292 }
293
294 private function normalizeName($name)
295 {
296 if (isset($this->normalizedNames[$normalizedName = strtolower($name)])) {
297 $normalizedName = $this->normalizedNames[$normalizedName];
298 if ((string) $name !== $normalizedName) {
299 @trigger_error(sprintf('Parameter names will be made case sensitive in Symfony 4.0. Using "%s" instead of "%s" is deprecated since Symfony 3.4.', $name, $normalizedName), \E_USER_DEPRECATED);
300 }
301 } else {
302 $normalizedName = $this->normalizedNames[$normalizedName] = (string) $name;
303 }
304
305 return $normalizedName;
306 }
307 }
308