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