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.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

ParameterBag.php

Zuletzt modifiziert: 09.10.2024, 12:58 - Dateigröße: 8.08 KiB


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