[ Index ] |
PHP Cross Reference of phpBB-3.1.12-deutsch |
[Summary view] [Print] [Text view]
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\DependencyInjection\ParameterBag; 13 14 use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; 15 use Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException; 16 use Symfony\Component\DependencyInjection\Exception\RuntimeException; 17 18 /** 19 * Holds parameters. 20 * 21 * @author Fabien Potencier <fabien@symfony.com> 22 */ 23 class ParameterBag implements ParameterBagInterface 24 { 25 protected $parameters; 26 protected $resolved; 27 28 /** 29 * @param array $parameters An array of parameters 30 */ 31 public function __construct(array $parameters = array()) 32 { 33 $this->parameters = array(); 34 $this->add($parameters); 35 $this->resolved = false; 36 } 37 38 /** 39 * Clears all parameters. 40 */ 41 public function clear() 42 { 43 $this->parameters = array(); 44 } 45 46 /** 47 * Adds parameters to the service container parameters. 48 * 49 * @param array $parameters An array of parameters 50 */ 51 public function add(array $parameters) 52 { 53 foreach ($parameters as $key => $value) { 54 $this->parameters[strtolower($key)] = $value; 55 } 56 } 57 58 /** 59 * {@inheritdoc} 60 */ 61 public function all() 62 { 63 return $this->parameters; 64 } 65 66 /** 67 * {@inheritdoc} 68 */ 69 public function get($name) 70 { 71 $name = strtolower($name); 72 73 if (!array_key_exists($name, $this->parameters)) { 74 if (!$name) { 75 throw new ParameterNotFoundException($name); 76 } 77 78 $alternatives = array(); 79 foreach ($this->parameters as $key => $parameterValue) { 80 $lev = levenshtein($name, $key); 81 if ($lev <= strlen($name) / 3 || false !== strpos($key, $name)) { 82 $alternatives[] = $key; 83 } 84 } 85 86 throw new ParameterNotFoundException($name, null, null, null, $alternatives); 87 } 88 89 return $this->parameters[$name]; 90 } 91 92 /** 93 * Sets a service container parameter. 94 * 95 * @param string $name The parameter name 96 * @param mixed $value The parameter value 97 */ 98 public function set($name, $value) 99 { 100 $this->parameters[strtolower($name)] = $value; 101 } 102 103 /** 104 * {@inheritdoc} 105 */ 106 public function has($name) 107 { 108 return array_key_exists(strtolower($name), $this->parameters); 109 } 110 111 /** 112 * Removes a parameter. 113 * 114 * @param string $name The parameter name 115 */ 116 public function remove($name) 117 { 118 unset($this->parameters[strtolower($name)]); 119 } 120 121 /** 122 * {@inheritdoc} 123 */ 124 public function resolve() 125 { 126 if ($this->resolved) { 127 return; 128 } 129 130 $parameters = array(); 131 foreach ($this->parameters as $key => $value) { 132 try { 133 $value = $this->resolveValue($value); 134 $parameters[$key] = $this->unescapeValue($value); 135 } catch (ParameterNotFoundException $e) { 136 $e->setSourceKey($key); 137 138 throw $e; 139 } 140 } 141 142 $this->parameters = $parameters; 143 $this->resolved = true; 144 } 145 146 /** 147 * Replaces parameter placeholders (%name%) by their values. 148 * 149 * @param mixed $value A value 150 * @param array $resolving An array of keys that are being resolved (used internally to detect circular references) 151 * 152 * @return mixed The resolved value 153 * 154 * @throws ParameterNotFoundException if a placeholder references a parameter that does not exist 155 * @throws ParameterCircularReferenceException if a circular reference if detected 156 * @throws RuntimeException when a given parameter has a type problem. 157 */ 158 public function resolveValue($value, array $resolving = array()) 159 { 160 if (is_array($value)) { 161 $args = array(); 162 foreach ($value as $k => $v) { 163 $args[$this->resolveValue($k, $resolving)] = $this->resolveValue($v, $resolving); 164 } 165 166 return $args; 167 } 168 169 if (!is_string($value)) { 170 return $value; 171 } 172 173 return $this->resolveString($value, $resolving); 174 } 175 176 /** 177 * Resolves parameters inside a string. 178 * 179 * @param string $value The string to resolve 180 * @param array $resolving An array of keys that are being resolved (used internally to detect circular references) 181 * 182 * @return string The resolved string 183 * 184 * @throws ParameterNotFoundException if a placeholder references a parameter that does not exist 185 * @throws ParameterCircularReferenceException if a circular reference if detected 186 * @throws RuntimeException when a given parameter has a type problem. 187 */ 188 public function resolveString($value, array $resolving = array()) 189 { 190 // we do this to deal with non string values (Boolean, integer, ...) 191 // as the preg_replace_callback throw an exception when trying 192 // a non-string in a parameter value 193 if (preg_match('/^%([^%\s]+)%$/', $value, $match)) { 194 $key = strtolower($match[1]); 195 196 if (isset($resolving[$key])) { 197 throw new ParameterCircularReferenceException(array_keys($resolving)); 198 } 199 200 $resolving[$key] = true; 201 202 return $this->resolved ? $this->get($key) : $this->resolveValue($this->get($key), $resolving); 203 } 204 205 $self = $this; 206 207 return preg_replace_callback('/%%|%([^%\s]+)%/', function ($match) use ($self, $resolving, $value) { 208 // skip %% 209 if (!isset($match[1])) { 210 return '%%'; 211 } 212 213 $key = strtolower($match[1]); 214 if (isset($resolving[$key])) { 215 throw new ParameterCircularReferenceException(array_keys($resolving)); 216 } 217 218 $resolved = $self->get($key); 219 220 if (!is_string($resolved) && !is_numeric($resolved)) { 221 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)); 222 } 223 224 $resolved = (string) $resolved; 225 $resolving[$key] = true; 226 227 return $self->isResolved() ? $resolved : $self->resolveString($resolved, $resolving); 228 }, $value); 229 } 230 231 public function isResolved() 232 { 233 return $this->resolved; 234 } 235 236 /** 237 * {@inheritdoc} 238 */ 239 public function escapeValue($value) 240 { 241 if (is_string($value)) { 242 return str_replace('%', '%%', $value); 243 } 244 245 if (is_array($value)) { 246 $result = array(); 247 foreach ($value as $k => $v) { 248 $result[$k] = $this->escapeValue($v); 249 } 250 251 return $result; 252 } 253 254 return $value; 255 } 256 257 /** 258 * {@inheritdoc} 259 */ 260 public function unescapeValue($value) 261 { 262 if (is_string($value)) { 263 return str_replace('%%', '%', $value); 264 } 265 266 if (is_array($value)) { 267 $result = array(); 268 foreach ($value as $k => $v) { 269 $result[$k] = $this->unescapeValue($v); 270 } 271 272 return $result; 273 } 274 275 return $value; 276 } 277 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Jan 11 00:25:41 2018 | Cross-referenced by PHPXref 0.7.1 |