Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

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