| 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 |
public function __construct(array $parameters = []) |
| 26 |
{ |
| 27 |
$this->add($parameters); |
| 28 |
} |
| 29 |
/** |
| 30 |
* {@inheritdoc} |
| 31 |
*/ |
| 32 |
public function clear() |
| 33 |
{ |
| 34 |
$this->parameters = []; |
| 35 |
} |
| 36 |
/** |
| 37 |
* {@inheritdoc} |
| 38 |
*/ |
| 39 |
public function add(array $parameters) |
| 40 |
{ |
| 41 |
foreach ($parameters as $key => $value) { |
| 42 |
$this->set($key, $value); |
| 43 |
} |
| 44 |
} |
| 45 |
/** |
| 46 |
* {@inheritdoc} |
| 47 |
*/ |
| 48 |
public function all() |
| 49 |
{ |
| 50 |
return $this->parameters; |
| 51 |
} |
| 52 |
/** |
| 53 |
* {@inheritdoc} |
| 54 |
*/ |
| 55 |
public function get(string $name) |
| 56 |
{ |
| 57 |
if (!\array_key_exists($name, $this->parameters)) { |
| 58 |
if (!$name) { |
| 59 |
throw new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException($name); |
| 60 |
} |
| 61 |
$alternatives = []; |
| 62 |
foreach ($this->parameters as $key => $parameterValue) { |
| 63 |
$lev = \levenshtein($name, $key); |
| 64 |
if ($lev <= \strlen($name) / 3 || \str_contains($key, $name)) { |
| 65 |
$alternatives[] = $key; |
| 66 |
} |
| 67 |
} |
| 68 |
$nonNestedAlternative = null; |
| 69 |
if (!\count($alternatives) && \str_contains($name, '.')) { |
| 70 |
$namePartsLength = \array_map('strlen', \explode('.', $name)); |
| 71 |
$key = \substr($name, 0, -1 * (1 + \array_pop($namePartsLength))); |
| 72 |
while (\count($namePartsLength)) { |
| 73 |
if ($this->has($key)) { |
| 74 |
if (\is_array($this->get($key))) { |
| 75 |
$nonNestedAlternative = $key; |
| 76 |
} |
| 77 |
break; |
| 78 |
} |
| 79 |
$key = \substr($key, 0, -1 * (1 + \array_pop($namePartsLength))); |
| 80 |
} |
| 81 |
} |
| 82 |
throw new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException($name, null, null, null, $alternatives, $nonNestedAlternative); |
| 83 |
} |
| 84 |
return $this->parameters[$name]; |
| 85 |
} |
| 86 |
/** |
| 87 |
* {@inheritdoc} |
| 88 |
*/ |
| 89 |
public function set(string $name, $value) |
| 90 |
{ |
| 91 |
$this->parameters[$name] = $value; |
| 92 |
} |
| 93 |
/** |
| 94 |
* {@inheritdoc} |
| 95 |
*/ |
| 96 |
public function has(string $name) |
| 97 |
{ |
| 98 |
return \array_key_exists($name, $this->parameters); |
| 99 |
} |
| 100 |
/** |
| 101 |
* {@inheritdoc} |
| 102 |
*/ |
| 103 |
public function remove(string $name) |
| 104 |
{ |
| 105 |
unset($this->parameters[$name]); |
| 106 |
} |
| 107 |
/** |
| 108 |
* {@inheritdoc} |
| 109 |
*/ |
| 110 |
public function resolve() |
| 111 |
{ |
| 112 |
if ($this->resolved) { |
| 113 |
return; |
| 114 |
} |
| 115 |
$parameters = []; |
| 116 |
foreach ($this->parameters as $key => $value) { |
| 117 |
try { |
| 118 |
$value = $this->resolveValue($value); |
| 119 |
$parameters[$key] = $this->unescapeValue($value); |
| 120 |
} catch (\YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException $e) { |
| 121 |
$e->setSourceKey($key); |
| 122 |
throw $e; |
| 123 |
} |
| 124 |
} |
| 125 |
$this->parameters = $parameters; |
| 126 |
$this->resolved = \true; |
| 127 |
} |
| 128 |
/** |
| 129 |
* Replaces parameter placeholders (%name%) by their values. |
| 130 |
* |
| 131 |
* @param mixed $value A value |
| 132 |
* @param array $resolving An array of keys that are being resolved (used internally to detect circular references) |
| 133 |
* |
| 134 |
* @return mixed |
| 135 |
* |
| 136 |
* @throws ParameterNotFoundException if a placeholder references a parameter that does not exist |
| 137 |
* @throws ParameterCircularReferenceException if a circular reference if detected |
| 138 |
* @throws RuntimeException when a given parameter has a type problem |
| 139 |
*/ |
| 140 |
public function resolveValue($value, array $resolving = []) |
| 141 |
{ |
| 142 |
if (\is_array($value)) { |
| 143 |
$args = []; |
| 144 |
foreach ($value as $k => $v) { |
| 145 |
$args[\is_string($k) ? $this->resolveValue($k, $resolving) : $k] = $this->resolveValue($v, $resolving); |
| 146 |
} |
| 147 |
return $args; |
| 148 |
} |
| 149 |
if (!\is_string($value) || 2 > \strlen($value)) { |
| 150 |
return $value; |
| 151 |
} |
| 152 |
return $this->resolveString($value, $resolving); |
| 153 |
} |
| 154 |
/** |
| 155 |
* Resolves parameters inside a string. |
| 156 |
* |
| 157 |
* @param array $resolving An array of keys that are being resolved (used internally to detect circular references) |
| 158 |
* |
| 159 |
* @return mixed |
| 160 |
* |
| 161 |
* @throws ParameterNotFoundException if a placeholder references a parameter that does not exist |
| 162 |
* @throws ParameterCircularReferenceException if a circular reference if detected |
| 163 |
* @throws RuntimeException when a given parameter has a type problem |
| 164 |
*/ |
| 165 |
public function resolveString(string $value, array $resolving = []) |
| 166 |
{ |
| 167 |
// we do this to deal with non string values (Boolean, integer, ...) |
| 168 |
// as the preg_replace_callback throw an exception when trying |
| 169 |
// a non-string in a parameter value |
| 170 |
if (\preg_match('/^%([^%\\s]+)%$/', $value, $match)) { |
| 171 |
$key = $match[1]; |
| 172 |
if (isset($resolving[$key])) { |
| 173 |
throw new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException(\array_keys($resolving)); |
| 174 |
} |
| 175 |
$resolving[$key] = \true; |
| 176 |
return $this->resolved ? $this->get($key) : $this->resolveValue($this->get($key), $resolving); |
| 177 |
} |
| 178 |
return \preg_replace_callback('/%%|%([^%\\s]+)%/', function ($match) use($resolving, $value) { |
| 179 |
// skip %% |
| 180 |
if (!isset($match[1])) { |
| 181 |
return '%%'; |
| 182 |
} |
| 183 |
$key = $match[1]; |
| 184 |
if (isset($resolving[$key])) { |
| 185 |
throw new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException(\array_keys($resolving)); |
| 186 |
} |
| 187 |
$resolved = $this->get($key); |
| 188 |
if (!\is_string($resolved) && !\is_numeric($resolved)) { |
| 189 |
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, \get_debug_type($resolved), $value)); |
| 190 |
} |
| 191 |
$resolved = (string) $resolved; |
| 192 |
$resolving[$key] = \true; |
| 193 |
return $this->isResolved() ? $resolved : $this->resolveString($resolved, $resolving); |
| 194 |
}, $value); |
| 195 |
} |
| 196 |
public function isResolved() |
| 197 |
{ |
| 198 |
return $this->resolved; |
| 199 |
} |
| 200 |
/** |
| 201 |
* {@inheritdoc} |
| 202 |
*/ |
| 203 |
public function escapeValue($value) |
| 204 |
{ |
| 205 |
if (\is_string($value)) { |
| 206 |
return \str_replace('%', '%%', $value); |
| 207 |
} |
| 208 |
if (\is_array($value)) { |
| 209 |
$result = []; |
| 210 |
foreach ($value as $k => $v) { |
| 211 |
$result[$k] = $this->escapeValue($v); |
| 212 |
} |
| 213 |
return $result; |
| 214 |
} |
| 215 |
return $value; |
| 216 |
} |
| 217 |
/** |
| 218 |
* {@inheritdoc} |
| 219 |
*/ |
| 220 |
public function unescapeValue($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->unescapeValue($v); |
| 229 |
} |
| 230 |
return $result; |
| 231 |
} |
| 232 |
return $value; |
| 233 |
} |
| 234 |
} |
| 235 |
|